Question

In: Computer Science

Please, explain your answer clearly (step-by-step), so I can trace the output without using the Visual...

Please, explain your answer clearly (step-by-step), so I can trace the output without using the Visual Studio Program.

Please, clearly describe how the mechanism of the loop works.

The answers in bold.

  1. What is printed by the following code snippet?

for (int i = 0; i < 5; i++)

{

   for (int j = 0; j <= i; j++)

   {

      cout << i << " ";

   }

}

    1. 0 1 2 3 4 5
    2. 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4
    3. 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
    4. The code will not run due to a syntax error.
    5. 0 1 1 2 2 2 3 3 3 3 4 4 4 4 4
  1. What is the output of the following code snippet?
  2. int my_fun(int A[], int size)
  3. {

    int result = 0;

    for (int i = 0; i < size; i++)

    {

       result = result + A[i];

    }

    return result;

    }

    int main()

    {

      int myarr[10] = { 2, 4, 10, 1, 2, 4, 8, 1, 2, 1 };

      for (int i = 0; i < 3; i++)

      {

       cout << my_fun(myarr, myarr[i]);

      }

    }

    1. 026
    2. 6435
    3. 61735

Solutions

Expert Solution

Code Snippet 1:

OUTPUT:

0 1 1 2 2 2 3 3 3 3 4 4 4 4 4

Explanation:

The line by line description of the source code is given below:

//outer for loop
//this loop will be executed five time for i= 0, 1, 2, 3, 4

for (int i = 0; i < 5; i++)
{
//inner for loop
//the repetition of this loop depend upon the outer for loop
//for i = 0, this loop will execute for one time and print 0 with space
//for i = 1, this loop will execute for two times and print 1 with space
//for i = 2, this loop will execute for three times and print 2 with space
//for i = 3, this loop will execute for four times and print 3 with space
//for i = 4, this loop will execute for five times and print 4 with space

for (int j = 0; j <= i; j++)
{
//print the value of variable 'i' and print the space at the last
cout << i << " ";

//end inner for loop
}
  
//end outer for loop
}

Code Snippet 2:

OUTPUT:

61735

Explanation:

The line by line description of the source code is given below:

//function to calculate the sum of an array of a given size
int my_fun(int A[], int size)
{
//variable declaration and initialization
int result = 0;
  
//the repetiotion of the for loop will depend upon the size
for (int i = 0; i < size; i++)
{
//the variable result will be increment array element value of index i
result = result + A[i];
}
  
//return the sum of array
return result;
}

int main()
{
//array declaration and initialization
int myarr[10] = { 2, 4, 10, 1, 2, 4, 8, 1, 2, 1 };
  
//this for loop will execute three times for i = 0, 1, and 2
for (int i = 0; i < 3; i++)
{
//This statement will display the return value of the called function
//the first argument is the address of the array
//the second argument is the size of the array
//For the first iteration, the second argument will pass the value of array first element
//The array first element is 2, so it will pass 2 as a second argument
//the called function returns the sum of the first two elements
//For the second iteration, the second argument will pass the value of array second element
//The array second element is 4, so it will pass 4 as a second argument
//the called function returns the sum of the first four elements
//For the third iteration, the second argument will pass the value of array third element
//The array third element is 10, so it will pass 10 as a second argument
//the called function returns the sum of first 10 elements

cout << my_fun(myarr, myarr[i])<<" ";
}
}


Related Solutions

#6 Please Show step by step, so I can solve similar problems, thanks! Give your answer...
#6 Please Show step by step, so I can solve similar problems, thanks! Give your answer as a whole number but do not round your calculations until you get to the final answer. Jole uses the high-low method of estimating costs. Jole had total costs of $12,373 at its lowest level of activity, when 2,597 units were sold. When, at its highest level of activity, sales equaled 6,091 units, total costs were $28,081. Jole would estimate fixed costs as: _______
please I want it to step by step and in word posted so I can read...
please I want it to step by step and in word posted so I can read them. Q: If the average realized return of a portfolio is 27.5% per year, the standard deviation of returns is 50%, the portfolio beta is 1.25, the average return of Treasury bills over the same period is 2.5% per year, and the average return on the market is 12.5% per year Calculate i) the Sharpe; ii) Treynor and iii) Jensen    
Please show that answer step by step and explain clearly, thx!!!! 5. Does the accuracy of...
Please show that answer step by step and explain clearly, thx!!!! 5. Does the accuracy of a kNN classifier using the Euclidean distance change if you (a) translate the data (b) scale the data (i.e., multiply the all the points by a constant), or (c) rotate the data? Explain. Answer the same for a kNN classifier using Manhattan distance.
Please try to type your solution for this question, so I can read it without a...
Please try to type your solution for this question, so I can read it without a problem. I truly appreciate you for typing in advance. The Question: An FBI survey shows that about 80% of all property crimes go unsolved. Suppose that in your town 3 such crimes are committed and they are each deemed independent of each other. X is the number of crimes will be solved in your town. Complete the table below for the probability mass function...
Please try to type your solution for this question, so I can read it without a...
Please try to type your solution for this question, so I can read it without a problem. I truly appreciate you for typing in advance. The Question: For some Mechanical Engineer students, data for 224 student’s GPAs in their first 4 semesters of college were compared using several predictors, namely HSM, HSS, HSE, SATM, and SATV. Here significant level α is 0.05. Then, the researcher fit a regression model and got the following results (intercept is included in the model):...
Please explain clearly which line means what and how can I get the answer. (correct answer...
Please explain clearly which line means what and how can I get the answer. (correct answer 2 2) public class Checker { public static int count = 0; public int number = 0; public Checker(){ count++; number = count; } public int getCount() { return count; } public int getNumber() { return number; } } What is output from the code fragment below? Checker one = new Checker(); Checker two = new Checker(); System.out.println(one.getCount() + " " + two.getCount());
I need derivation of fermi's golden rule please write clearly so I can read it
I need derivation of fermi's golden rule please write clearly so I can read it
Please show all work step by step so i can understand Find and classify each critical...
Please show all work step by step so i can understand Find and classify each critical point (as relative maximum, relative minimum, or saddle point) of f(x,y)=2x^3+3x^2+y^2-36x+8y+1
Please print so I can read clearly. Thank you 1. Describe the effects of aging on...
Please print so I can read clearly. Thank you 1. Describe the effects of aging on fluid and electrolyte regulation. 2. Electrolytes to investigate: (Sodium, Potassium, Calcium, Magnesium). Include causation, clinical manifestations, and nursing interventions.
Can someone please explain step by step the following solution. i am going to post the...
Can someone please explain step by step the following solution. i am going to post the solution i just need someone to please explain why each step is taking and what formula is being used. please explain it to me thank tyou Suppose A is a set and {Bi | i ∈ I} is an indexed family of sets with I ≠∅. Prove that A \ ( ∪i∈I Bi ) = ∩i∈I (A \ Bi). A be a set Bi...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT