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 array: int[] a = { 3, 5, 2, 2, 4, 7, 0, 8,...
Consider the following array: int[] a = { 3, 5, 2, 2, 4, 7, 0, 8, 9, 4 }; What are the contents of the array a after the following loops complete? (show how you arrive at the answer) a) for (int i = 1; i < 10; i++) { a[i] = a[i - 1]; b) for (int i = 9; i > 0; i--) { a[i] = a[i - 1]; }
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); }...
C++ questions 5.Which of the following function prototype(s) are correct. void setValue(int &value); void setValue(int &);...
C++ questions 5.Which of the following function prototype(s) are correct. void setValue(int &value); void setValue(int &); void setValue(int value, int max=100); void setValue(int ++value); 6. T/F Calling an overloaded function can result in multiple values being returned to the caller. 7.A ____ function has ____ return value(s), but can have ____ parameter(s). Otherwise a function can return _____ value(s). 8.T/F Only variables can be passed as reference. 9.T/F When writing a C++ program you must be careful to arrange all...
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...
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
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 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 =...
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...