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

  • Thread starter Thread starter chmate
  • Start date Start date
  • Tags Tags
    Code Reading
AI Thread Summary
The discussion revolves around a piece of C++ code that generates a right-aligned triangle of asterisks. The code uses nested for loops to print spaces and asterisks, creating a pattern where each line has an increasing number of leading spaces and a decreasing number of asterisks. Participants point out a syntax error in the code, specifically the missing semicolon after the "cout << "*"" statement. They also suggest that the output will depend on the font used, recommending the use of graph paper to visualize the pattern accurately. Overall, the code is intended to produce a visual representation of a right-aligned triangle with asterisks, but it requires some corrections for proper execution.
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 "cout<<endl" is just a new line.

EDIT: oh yeah you also hav 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:
cout << "*"
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top