Question about nested loops.

Our teacher ask us to program a nested loops. And I have solve one of it.Can you help me solve the 2 questions given to our teacher.?

display a row of stars using stars (*)


This is the question I solve.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main(){
  int x,y;
 for(x=0;x<5;x++){
 for(y=0;y<5;y++){
   if(x==0)
 cout<<"*";
   else if(y==5-1)
 cout<<"*";
   else
 cout<<" ";
}
   if(x==4)
 for(x=0;x<4;x++){
 cout<<"*";
}
 cout<<endl;
}
return 0;
}



This should produce output like this:

1
2
3
4
5
*****
    *
    *
    *
    *****





Can you help me with this 2 questions.?pls...?

1.

1
2
3
4
5
*   *
 * *
  *
 * *
*   *



2.

1
2
3
4
5
*     *
* * * *
*  *  *
*     *
*     *


I need this program very much...im just a begginer of c++ but I want to learn more about c++...Thanks if you help me..!!!


It is easy if you use a bunch of if...else statements, work it out yourself.

If you want a better solution, first ask yourself what pattern do you see in the figures.
For example in the "X", the position of the asterisks in the i-th row are at the i-th column and the (6-i)th column.
what is the meaning of the i-th row are at the i-th column and the (6-i)th column..?can you please give me an example of it...?
Last edited on
In your code x is the row number and y is the column number.

The stars in the i-th row are at the i-th position and the (6-i)th position.
e.g. the stars in the 2nd row are at the 2nd and the 6-2=4th positions.
thanks dude now i know now....
Topic archived. No new replies allowed.