Question

int array_with_loop(vector<int> data) {   int i;   data.push_back(6); data.push_back(9); data.push_back(2); data.push_back(-7); data.push_back(-4); int result = data[0]; for...

int array_with_loop(vector<int> data)

{

  int i;

  data.push_back(6);

data.push_back(9);

data.push_back(2);

data.push_back(-7);

data.push_back(-4);

int result = data[0];

for ( i =0; i < data.size(); i++ )

{

if ( data[i] < result )

result = data[i];

}

return result;

}

Hand execute the following code on paper for array data, variables i and result, and then indicate the returned value of array_with_loop function .

Homework Answers

Answer #1

data.push_back(6);

// array status: 6

data.push_back(9);

// array status: 6| 9

data.push_back(2);

// array status: 6| 9| 2

data.push_back(-7);

// array status: 6| 9| 2| -7

data.push_back(-4);

// array status: 6| 9 |2| -7| -4

int result = data[0];

result = 6

for ( i =0; i < data.size(); i++ )

{

if ( data[i] < result )

result = data[i];

}

i = 0

data[0] < result

6 < 6 false

i = 1

data[1] < result

9 < 6 false

i = 2

data[2] < result

2 < 6

result = data[2]

result = 2

i = 3

data[3] < result

-7 < 2 true

result = -7

i = 4

data[4] < result

-4 < -7 false

i =5

i > data.size() loop stops

so, variable i = 5

result = -7 ans

/* PLEASE UPVOTE */

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
public class Mystery { public static String mystery(String str, int input) { String result = "";...
public class Mystery { public static String mystery(String str, int input) { String result = ""; for (int i = 0; i < str.length() - 1; i++) { if (input == 0) { str = ""; result = str; } if (input == -2) { result = str.substring(2, 4); } if (input == 1) { result = str.substring(0, 1); } if (input == 2) { result = str.substring(0, 2); } if (input == 3) { result = str.substring(2, 3); }...
In Java is there a way I can put a value in a linkList = array[...
In Java is there a way I can put a value in a linkList = array[ ] string (I don't know how to word it here what I have so far with my code) * Example object 51 I want to equal Array [0]; // Project code below public String getCardRank() { String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; return "RRR"; } // this part is in another .java file private...
C++ What is the value of average after the following code executes? double average; average =...
C++ What is the value of average after the following code executes? double average; average = 4.0 + 3.0 * 2.0 + 1.0; Consider the following function: int compute(int n) { int x = 1; for (int i = 1; i <= n; i++) { x *= i; } return x; } What is the returned value for the following function call? int value = compute(4); What is the value of number after the following statements execute? int number; number...
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]...
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] W X D T P N R Q K M E If the array was being sorted using the SHELL sort and the halving method, and sorting into ASCENDING order as demonstrated in the course content, list the letters in the resulting array, in order AFTER the FIRST pass. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
C program. Given the following code: int add_and_double(int a, int b) { int result = (a...
C program. Given the following code: int add_and_double(int a, int b) { int result = (a + b) * 2; return result; } int main() {   int val; val = add_and_double(12, 40); return 0; } where the function main() (i.e. the caller) calls the function add_and_double() (i.e. the callee). Which of the following actions are taken when main() calls the function add_and_double()?  Select all that apply. a. Once the callee returns, the caller frees the stack memory for the callee's function...
Stem Leaf 2 6 3 0, 7 4 0, 2, 6 5 1, 5, 7, 9,...
Stem Leaf 2 6 3 0, 7 4 0, 2, 6 5 1, 5, 7, 9, 9 6 0, 2, 5, 8 1) standard deviation: 2)Find Five-number summary of the data set 3). Draw a box plot that represents the data set
C D 3 2 6 7 8 5 9 4 1 0 3 4 Using the...
C D 3 2 6 7 8 5 9 4 1 0 3 4 Using the predict function in R, find each of the following predicted values of D, for the given value of C regardless of whether or not it is appropriate to do so. In real life you should not calculate for predictions when it is not appropriate, but this is just for practice. Find when C = 6 Find when C = 4 Find when C =...
int main() { int i, j, sum = 0; for(i=0;i<3;i++) { for(j=0;j<4;j++) { if(j > 2)...
int main() { int i, j, sum = 0; for(i=0;i<3;i++) { for(j=0;j<4;j++) { if(j > 2) break; sum++; } } } What is value of sum, explain.
Considering the following code fragment: #include<stdio.h> #include <stdlib.h> int base = 0; static int bonus; int*...
Considering the following code fragment: #include<stdio.h> #include <stdlib.h> int base = 0; static int bonus; int* demage (int dmg) { int* ptr = (int*) malloc(sizeof(int)); static int constant = 1;   *ptr = dmg + constant; return ptr; } int main() { int HP = 0; int* dm; base = 30; bonus = 20; for(int i=1; i<4; i++) { HP += i * base; } dm = demage(10); HP += bonus - *dm; printf("Total HP: %i", HP); return 0; } Please...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT