Question

int array_with_loop(vector data) { int i; data.push_back(5); data.push_back(7); data.push_back(3); data.push_back(-8); data.push_back(-2); int result = data[0]; for...

int array_with_loop(vector data) { int i; data.push_back(5); data.push_back(7); data.push_back(3); data.push_back(-8); data.push_back(-2); int result = data[0]; for ( i =0; i < data.size(); i++ ) { if ( data[i] < result ) result = data[i]; } return result;

Homework Answers

Answer #1

array_with_loop return lowest value in data vector .

Result is -8

inside the loop of:

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

{

if (data[i] < result)

result = data[i];

}

initial result is 5

loop 1: data[i] < result => 5 < 5 false

loop 2: data[i] < result => 7 < 5 false

loop 3: data[i] < result => 3 < 5 true, then result is assigned with 3

loop 4: data[i] < result => -8 < 3 true, then result is assigned with -8

loop 5: data[i] < result => -2 < -8 false

return result -8

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
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 .
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]; }
Which is an invalid access for the vector? vector<int> numsList(5); int x = 3; Group of...
Which is an invalid access for the vector? vector<int> numsList(5); int x = 3; Group of answer choices numsList.at(x+2) numsList.at(x-3) numsList.at((2*x) - x) numsList.at(0) Program: C++ Single choice
exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6...
exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6 1 5 6 8 2 4 6 7 3 4 5 9 10 5 8 9 4 7 9 6 7 8 6 How can I detect when 'cin' starts reading from a new line. The amount of numbers in each row is unknown. I need them in type 'int' to use the data. Also, it will be executed like so, "./a.out < sampleInput.txt".
int i,sum=0;   int array[8]={//You decide this};   int *a,*b,*c;   a = array;   b = &array[3];   c =...
int i,sum=0;   int array[8]={//You decide this};   int *a,*b,*c;   a = array;   b = &array[3];   c = &array[5];   c[-1] = 2;   array[1] = b[1]-1;   *(c+1) = (b[-1]=5) + 2;   sum += *(a+2) + *(b+2) + *(c+2);   printf("%d %d %d %d\n", sum, array[b-a], array[c-a], *a-*b); array[8]={ ?? }; what is array[8]?
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.
Matrix A= -2 1 0 2 -3 4 5 -6 7 vector u= 1 2 1...
Matrix A= -2 1 0 2 -3 4 5 -6 7 vector u= 1 2 1 a) Is the vector u in Null(A) Explain in detail why b) Is the vectro u in Col( A) Explain in detail why
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
Let u = (−3, 2, 1, 0), v = (4, 7, −3, 2), w = (5,...
Let u = (−3, 2, 1, 0), v = (4, 7, −3, 2), w = (5, −2, 8, 1). Find the vector x that satisfies 2u − ||v||v = 3(w − 2x).
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8...
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8 13 21 34 55 ....... and also need add code complexity time if you want! here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[ n + 1 ]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for ( i = 2; i < n; i++ ) { fib[ i ] = fib[ i -...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT