Question about nested looping.

Our teacher ask us to program a nested looping this is the question. Please help me I need this within 2 days.T_T

display a row of stars using stars (*)


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



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



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


Thanks if you help me....!!
that's one of the common practice when teaching students how to deal with loops - i remember i had to make a christmas tree once :)

You have to realise that all the "figures" consist of "*" and spaces " "

Let's say we have to print a square (that wont be a square of course), dimension a x a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include "stdafx.h"

int main()
{
	int a;
	std::cout<<"Type a: ";
	std::cin>>a; //a=5

        for(int i=0;i<a;i++)
	{
		for(int j=0;j<a;j++)
			{
				if(i==0 || i==a-1)       //upper and lower edge all filed with "*"
					std::cout<<"*";		
				else if(j==0 || j==a-1)  
					std::cout<<"*";  //the middle edges, most right and most left edge
				else
					std::cout<<" ";   //so the output is *___*; _ = space
			}
		std::cout<<"\n";   //we go to the next "level"	
	}
        return 0;
}


This should produce output like this:
1
2
3
4
5
*****
*   *
*   *
*   *
*****


And as hamsterman pointed out ... at first try to do something by yourself, bring some (not-working) code here etc

I'm newbie at c++ too and many kind people here aready helped me several times, but the forum is the last place i go if i have any problem with my program :)

gl and cheers,
JK
Topic archived. No new replies allowed.