Explaining Code Reading Problem: for (int i=0; i<=9; i++)

  • Thread starter Thread starter chmate
  • Start date Start date
  • Tags Tags
    Code Reading
Click For Summary

Discussion Overview

The discussion revolves around understanding a specific piece of code that utilizes nested for loops to produce a pattern of stars and spaces in the console. Participants are exploring the output of the code and clarifying the mechanics of the loops involved.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant requests an explanation of the code's functionality, specifically what the nested loops are doing.
  • Another participant questions the original poster's understanding of for loops and suggests writing out the code on paper to trace it.
  • A participant interprets the output as producing 9 lines with an increasing number of spaces and a decreasing number of stars, but expresses uncertainty about the visual representation.
  • One participant provides a detailed description of the expected output, indicating that it forms a right-aligned triangle of stars, while also noting syntax errors in the original code.
  • Another participant mentions that the appearance of the output may vary depending on the font used, suggesting the use of graph paper for visualization.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the code, with some interpretations of the output being shared. There is no consensus on the best way to approach tracing the code or the exact nature of the output.

Contextual Notes

There are unresolved issues regarding the syntax of the code, including a missing semicolon and potential variable naming conflicts in the nested loops. These factors may affect the execution and output of the code.

Who May Find This Useful

This discussion may be useful for individuals learning about nested loops in programming, those interested in code tracing techniques, or anyone seeking to understand console output formatting in programming languages.

chmate
Messages
37
Reaction score
0
Hi!

Code:
for (int i=0; i<=9; i++)
{
   for (int j=0; j<i; j++)
       cout << " ";
   for (int j=i; j<=9; j++)
        cout << "*"
   cout << endl;
}

Can anyone explain me what these lines are doing?

Thank you!
 
Technology news on Phys.org
what do you think the are doing?
Do you know how to trace through code like that?
...write it all out on bpaper.

If you don't know what a for loop does then i can't help ya.
the "count<<endl" is just a new line.

EDIT: oh yeah you also count ean error in your j-for loops
 
Last edited:
I don't know. It seems like it's printing 9 lines, each line having an increasing number of spaces and decreasing number of stars. But i can't figure out what something like that can possibly look like.
 
It does this:
**********
*********
********
*******
******
*****
****
***
**
*
Press any key to continue
When you don't try to redfine j in:
for (int j=i; j<=9; j++).. (I used x)
and correct the syntax error by adding a semi-colon to:
count << "*"
Sorry for butting in...:blushing:
 
What it will look like depends on the font you use.
But, most things like this are ment for a fixed spacing font.
So take some graph paper and plot it out.
 

Similar threads

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