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

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    C++ Code
In summary, the code in question will display the values of 100 and 200 on the screen once compiled and executed. The loop body will execute 100 times, and the variables i and counter will be incremented 200 times in total. The final value of counter will be 200.
  • #1
ineedhelpnow
651
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
  • #2
First, how may times would you say the loop body executes?
 
  • #3
MarkFL said:
First, how may times would you say the loop body executes?

99.
 
  • #4
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:

\(\displaystyle 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?
 
  • #5
50? :confused:
 
  • #6
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?
 
  • #7
200?
 
  • #8
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?
 
  • #9
199
 
  • #10
ineedhelpnow said:
199

Think of the formula:

\(\displaystyle 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)
 

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

What is C++?

C++ is a high-level, general-purpose programming language that was developed in the early 1980s by Bjarne Stroustrup. It is an extension of the C programming language and is known for its efficiency and flexibility.

What is the purpose of displaying C++ code?

The purpose of displaying C++ code is to understand the logic and syntax of a program, as well as to identify any errors or bugs that may be present. It also allows for easier collaboration and sharing of code among programmers.

How do you read and interpret C++ code?

C++ code is read from top to bottom, following the flow of the program. Each line of code is interpreted according to the rules and syntax of the C++ language. It is important to pay attention to details, such as variable names and punctuation, in order to accurately understand the code.

How can C++ code display help in understanding exam-related questions?

By displaying C++ code, students can better understand the logic and steps involved in solving a problem. It also allows them to practice writing and analyzing code, which is a crucial skill for programming exams.

Are there any resources available for understanding C++ code display?

Yes, there are many online tutorials, forums, and books available that can help individuals understand C++ code display. It is also helpful to practice writing and analyzing code on a regular basis to improve understanding and proficiency.

Similar threads

Replies
22
Views
3K
Replies
1
Views
1K
Replies
8
Views
2K
Replies
13
Views
1K
Replies
23
Views
2K
Replies
7
Views
1K
Replies
6
Views
10K
Replies
5
Views
2K
Replies
40
Views
3K
Back
Top