Please help me out guys I can't fully understand how this code works but I know some about it and I'm also unsure of such things about how it operates.

#include<iostream.h>
#include<conio.h>

int main()
{
for(int i=1; i<=6; i++)  
{         
for(int f=1; f<=i; f++)  
cout<<"*";
cout<<endl;
}
system ("pause");
return 0;
}

All I know is that the first loop is responsible for the rows and the second loop is for the columns. How does this program with loops work? Please explain guys. Thanks in advance and God Bless you all!

Recommended Answers

All 12 Replies

What's confusing you? Do you understand how loops work in general?

I have a suspicion about what's confusing you and I suspect that part of your answer will come from answering this question: "Where does the variable/value 'i' come from?".

Proper formatting will also help (with some "modernization"):

#include<iostream>

int main()
{
  for(int i=1; i<=6; i++)  
  {         
    for(int f=1; f<=i; f++)  
      std::cout << "*";
    std::cout << std::endl;
  }
  system ("pause");
  return 0;
}

hmm. okay so where did the i come from?

I really do not know how these 2 loops behave, for example if i is 1 and the when it increments, what happens? The same thing with the f. What happens when f increments ?

>>hmm. okay so where did the i come from?
I want you to look at the code and try to answer the question, not simply turn it back on me.

I suspect that you don't understand for loops in general, here is a tut that will help.

These loops in particular are "nested". The outer loop (based on 'i') sets the current "row" of the display and the inner loop (based on 'f') populates the row.

Each time the outer loop iterates (executes) the inner loop runs to its completion. How many times the inner loop iterates before it completes is determined by the current value of 'i' which keeps track of the current row. It's a common construct for displaying and processing of multi-dimentional data.

This is something I just put together, well documented...
I hope you understand this code

#include <iostream>
//#include <cmath>
using namespace std;

int main(){
    //we introduce 3 ints
    //a, iterates to the value (length) of adjacent which is user determined
    //c, the length of adjacent
    //b, makes space for hypothenus
    //int k;
    
    int a, b;
    int c;
    const char dd = '*';    //you can make it display any character
                    //just change the asterisk....
                    
                    
    cout <<"Enter length of the base of the triangle:\n"
         <<"NB:Must be an integer between 1 and 30!\nInput:";
         
    cin >> c;
    //For display purposes, we limit the input amount
    if( (c < 1) || (c > 70) ){ cout <<"ERROR!";  return -1; }
    
    ////Start Drawing         
    cout <<"\nDrawing Triangle...\n\n";
    
    cout << dd << endl;                    //initial char
    for(a=0; a<c; a++){                    //////FIRST LOOP//////
             cout << dd ;                   //FIRST char, vertical
             for(b=0; b<a; b++) cout <<" ";//spacer
             cout << dd ;                   //SECOND char, diagonal
             cout << endl;                 //next line
             }                             //End of loop
             
    //we use pythagoras theorem to find the horizontal. BUT LEAVE THAT FOR NOW
    //the <cmath> header, int k.... and
    //this  --->   k = (sqrt(c)) * (sqrt(c));
    //have been commented out... cause no use
    
    for(int i=0; i<c; i++) cout << dd ; //displays the horizontal character
    cout << dd << dd;                       //display correction;
    
    //End
    //////For Pausing the console//////
    cin.ignore(100, '\n');             //flush cin buffer by a 100 keystrokes/inputs
    cout <<"\nPress Enter to Exit...";
    cin.get();
    
    return 0;
    
}

Run the code, it should work.
try harder things like a square, circle etc; after you've understood this one;

commented: Do GIVE answers to people when others are trying to help. You waste their time and effort and the OP doesn't learn as much. -3

@WhiZTiM: thanks for the code I am studying it right now.
sir can you help me with this part of my source code(the highlighted loops):

for(int i=1; i<=6; i++)  
{         
for(int f=1; f<=i; f++)  
cout<<"*";
cout<<endl;
}

so when the i increments it will produce a "*" then it will go to the second loop and the second loop will also produce a "*" too then will go to endl to go to a new line(second line) and then produces another "*" but this time two "*" will be produced, the same thing with the second... right? But what I don't get is how these "*" are positioned... can you make things clearer for me please I'm telling you I'm working hard to understand all of these loops I don't just ask and wait for the answer I really study please help.

Are you using pencil and paper to "run" your program and see what happens? It's only 4 lines of code. Certainly you can follow that much just to see what the code does.

okay WaltP thanks. But was I correct with what I've mentioned above, did I get how the 2 loops work and operate or did I misunderstood it? please help...

No. What happened when you checked it using paper? When did you write down the *? Inside which loop?

Also, at your level of programming, you need to use more { and }. Put them everywhere -- around all loops, all ifs etc. Even the loops in the code you posted. You can then see for sure what's happening.

commented: Stop spammong same message -1

>>Also, at your level of programming, you need to use more { and }. Put them everywhere -- around all loops, all ifs etc. Even the loops in the code you posted. You can then see for sure what's happening.
Proper use of whitespace will help too...:

for(int i=1; i<=6; i++)  
{         
  for(int f=1; f<=i; f++)  
    cout<<"*";
  cout<<endl;
}

>>hmm. okay so where did the i come from?
I want you to look at the code and try to answer the question, not simply turn it back on me.

I suspect that you don't understand for loops in general, here is a tut that will help.

These loops in particular are "nested". The outer loop (based on 'i') sets the current "row" of the display and the inner loop (based on 'f') populates the row.

Each time the outer loop iterates (executes) the inner loop runs to its completion. How many times the inner loop iterates before it completes is determined by the current value of 'i' which keeps track of the current row. It's a common construct for displaying and processing of multi-dimentional data.

Thank you very much for this clear explanation. You've explained it very clearly for me. I really appreciate it thank you sir! God Bless!

>>hmm. okay so where did the i come from?
I want you to look at the code and try to answer the question, not simply turn it back on me.

I suspect that you don't understand for loops in general, here is a tut that will help.

These loops in particular are "nested". The outer loop (based on 'i') sets the current "row" of the display and the inner loop (based on 'f') populates the row.

Each time the outer loop iterates (executes) the inner loop runs to its completion. How many times the inner loop iterates before it completes is determined by the current value of 'i' which keeps track of the current row. It's a common construct for displaying and processing of multi-dimentional data.

Thanks sir. So there's really no loops for columns existing in this program code?

Thanks sir. So there's really no loops for columns existing in this program code?

Not directly. Without the use of special commands you can only directly reference the rows. You "access" the columns by manipulating the contents and formatting of your output.

The outer loop "accesses" the row. The inner loop controls the formatting and content of the row.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.