Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++! Can someone explain me the output of the following code. Suppose that the c

ID: 3811668 • Letter: C

Question

C++! Can someone explain me the output of the following code.

Suppose that the content of the input file is: 14 13 26 8 -11 36 0 -1. What is the output of the following code?

ifstream myInfile;

myInfile.open("input.txt");

int value;

int sum = 0;

for (int x = 0; x < 8; x++)

{

     myInfile >> value;

     if (value % 2 == 0)

          continue;

     sum = sum + value;

}

cout << sum << endl;

2) Suppose that the content of the input file is: 14 13 26 8 -11 36 0 -1. What is the output of the following code?

ifstream myInfile;

myInfile.open("input.txt");

int value;

int sum = 0;

for (int x = 0; x < 8; x++)

{

     myInfile >> value;

     if (value % 2 == 0)

          sum = sum + value;

}

cout << sum << endl;

Explanation / Answer

ifstream myInfile;
myInfile.open("input.txt");
int value;
int sum = 0;
for (int x = 0; x < 8; x++)
{
     myInfile >> value;
     if (value % 2 == 0)
          continue;
     sum = sum + value;
}
cout << sum << endl;

Input: 14 13 26 8 -11 36 0 -1.


The output of above code is 1.

The continue statement forces the next iteration of the loop and skips the code in between.

So,to find the output of the above code lets see the iterations:

In first iteration value=14

value%2==0 i.e 14%2=0 which is true
So,now the continue statement will skip the next statement (sum = sum + value;)and move to second iteration and value of sum remains 0.

In second iteration value=13 and 13%2=1 i.e the if statement is not true and the continue statement will not be executed.
So, sum=sum+value (i.e. 0+13) value=13

In third iteration value=26 and 26%2=0
The continue statement will skip the next statement (sum = sum + value;)and move to second iteration and value of sum remains 13.

In fourth iteration value=8 and 8%2=0
The continue statement will skip the next statement (sum = sum + value;)and move to second iteration and value of sum remains 13.

In fifth iteration value=-11 and -11%2 is not 0

So, sum=sum+value (13+(-11)) i.e sum=2

In sixth iteraion value=36 and 36%2=0
So, value of sum remains same

In seventh iteration value=0 and 0%0 is not 0
so, sum=sum+value (2+0=2)

In eighth itertion value=-1 and -1%2 is not 0
So, sum=2+(-1)=1

So, the final value of sum is 1.


2. ifstream myInfile;
myInfile.open("input.txt");
int value;
int sum = 0;
for (int x = 0; x < 8; x++)
{
     myInfile >> value;
     if (value % 2 == 0)
          sum = sum + value;
}
cout << sum << endl;

Input: 14 13 26 8 -11 36 0 -1.

The output of above code is 84.

In first iteration value=14 and 14%2=0
So sum=0+14=14

Second iteration value=13 and 13%2 is not 0 (i.e the condition is not satisfied and the next iteration will take place)
so sum remains 14

Third iteration value=26 and 26%2=0
sum=14+26=40

Fourth iteration value=8 and 8%2=0
sum=40+8=48

Fifth iteration value=-11 and -11%2 is not 0
so sum remains same i.e. 48

Sixth iteration value=36 and 36%2=0
sum=48+36=84

seventh iteration value=0 and 0%0 is not 0
so value of sm remains same

Eigth iteration value=-1 and -1%2 is not 0
So, value of sum remains same

So, the final output is 84

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote