C++ Code Display: Understanding Output of Exam-Related Question

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Code
Click For Summary

Discussion Overview

The discussion revolves around understanding the output of a specific C++ code snippet related to a programming exam question. Participants analyze the behavior of loop iterations and variable increments within the code.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant questions how many times the loop body executes.
  • Another participant suggests that the loop iterates 100 times based on the increment of variable i from 0 to 99.
  • A formula is presented to calculate the number of iterations, concluding that the loop iterates 100 times.
  • Discussion shifts to the increments of the variable counter, with one participant suggesting it is incremented 50 times.
  • Another participant clarifies that counter is incremented twice per loop iteration, leading to a total of 200 increments over 100 iterations.
  • There is a proposal that the final value of counter would be 199, which is later corrected to 200 by another participant.
  • Participants discuss the final output of the code, concluding it would display "100 200".

Areas of Agreement / Disagreement

While there is some agreement on the final output of the code, there are differing views on the number of increments for the variable counter, with initial confusion about its final value. The discussion reflects a mix of agreement and uncertainty regarding the increments.

Contextual Notes

Participants rely on assumptions about loop behavior and variable increments without fully resolving the implications of the increment statements in the for loop.

ineedhelpnow
Messages
649
Reaction score
0
I came across this question while studying for a C++ exam. What is the screen output of the code?

Code:
#include <stdio.h>

int main(){
   int i, counter=0;

   for(i=0;i<100;counter++){
      i++;
      ++counter;
    }
    cout<<i<<' '<<counter<<'\n';

    return 0;
}

What will this code display on the screen once compiled and executed?
 
Technology news on Phys.org
First, how may times would you say the loop body executes?
 
MarkFL said:
First, how may times would you say the loop body executes?

99.
 
ineedhelpnow said:
99.

The variable i begins at 0 and ends at 99 and is incremented by 1, so the loop iterates 100 times. Here is a general formula to determine the number of times a loop will iterate:

$$N=\frac{\text{Ending value}-\text{Initial value}}{\text{Increment}}+1=\frac{99-0}{1}+1=100$$

So, how many times are the two variables incremented?
 
50? :confused:
 
Let's look at the variable [m]i[/m] first. We see it is incremented by 1 every time the loop is iterated. And the loop continues to iterate as long as [m]i < 100[/m]. Once i is incremented to store a value of 100, the loop terminates. So, we can be certain that the ending value of [m]i[/m] is 100.

The variable [m]counter[/m] is a little different. It is incremented in the body of the loop, but it is also incremented in the increment statement of the for loop. The increment statement is performed before the condition is evaluated.

So, if the loop is iterated 100 times, how many times is [m]counter[/m] incremented?
 
200?
 
ineedhelpnow said:
200?

Yes, it is incremented twice per loop iteration, and with 100 iterations, this is 200 increments. So, [m]counter[/m] is initialized to be 0, and is then incremented by 1 200 times...what will its final value be?
 
199
 
  • #10
ineedhelpnow said:
199

Think of the formula:

$$N=0+\sum_{k=1}^n(1)=n$$

So, the final value would be 200.

Or think of you having no money in your hand initially. Then I begin placing one dollar bills into your hand one at a time. I do this 200 times...so you end up with 200 dollars in your hand. :D
 
  • #11
do-me-a-favor-smiley-emoticon.png
Oops. I understand. So the screen would display 100 200?
 
  • #12
ineedhelpnow said:
Oops. I understand. So the screen would display 100 200?

Yes. (Yes)
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
1K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
12
Views
3K