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):...
explain your answer step by step, i already have the answer but i need the explnations...
explain your answer step by step, i already have the answer but i need the explnations and type your answer, thanks 4 FeS2   +   11 O2    ®    2 Fe2O3    +    8 SO2 1.    How many moles of oxygen are required to react with 3.75 moles of FeS2?        2.    How many moles of Fe2O3 are formed when 2.83 moles of oxygen reacts with        excess FeS2? 3.    How many moles of SO2 are formed when 2.81 moles of Fe2O3 are...
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());
Please show step by step work in a spreadsheet or excel so I can follow along:...
Please show step by step work in a spreadsheet or excel so I can follow along: A federal agency responsible for enforcing laws governing weights and measures routinely inspects packages to determine whether the weight of the contents is at least as great as that advertised on the package. A random sample of 30 containers whose packaging states that the contents weigh 8 ounces was drawn. The weight of each package in ounces can be found in the sheet entitled...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT