Hello, i'm learning to work with loops and it was fine untill it came to nesting loops. In an exersize, i was told to use nested loops to display a pattern of asterisks like this:
(2 asterisks, 4 asterisks, 6 asterisks, 8 asterisks, 10 asterisks, 12 asterisks)

**
****
******
********
**********
************

I tried everything in my knowlege, i tried to draw up an algorithm, sketch a flowchart, but i always got stuck at one point. I've made out that the first loop should control the lines (6) but i dont know how to control the asterisks per line. I coded many a algorithm but the result was bizzare designs, and an infinite loop that just kept typing asterisks :-\ .

Could someone just explain me through this and atleast just give me an algorithm so that i can code it myself. Coding the algorithm will make it clearer to me :)

Recommended Answers

All 3 Replies

Its better if you post the code which you have tried so that we can believe you have actually worked on it.But you have asked just for the algorithm here you have.

1)Run outer loop (say for loop) n number of times where in n=number of lines.
2)Assume a variable no_of_asterisks which holds 2 initially.(Because you are starting with printing two asterisks)
3)Within the loop stated in (1) write another loop which loops no_of_asterisks number of times and prints a asterisk in every loop.
4)After step (3) go to a new line and then increment the value of no_of_asterisk variable to the value you want to increment it with(here say +2).

Keep on looping this.

commented: Well put. +33

Or, have the outer loop go by steps of 2, and use its current value as the limit for the inner loop that prints the *s.

1)Run outer loop (say for loop) n number of times where in n=number of lines.
2)Assume a variable no_of_asterisks which holds 2 initially.(Because you are starting with printing two asterisks)
3)Within the loop stated in (1) write another loop which loops no_of_asterisks number of times and prints a asterisk in every loop.
4)After step (3) go to a new line and then increment the value of no_of_asterisk variable to the value you want to increment it with(here say +2).

Or, have the outer loop go by steps of 2, and use its current value as the limit for the inner loop that prints the *s.

Thank you all so much :) . I tried yesterday but it was night and I was drowsy and stupid :P . I came back after a hearty breakfast and managed to do it after a couple of tries. Desk-checking really helps, unlike my previous opinion of it. This was my final code, done with accordance to CSurfers algorithm

#include <stdafx.h>
#include <iostream>

using namespace std;

int main(){
	int lines = 0;
	int ast = 2;
	const char asterisk = '*';

	for(lines = 1; lines <= 6; lines++){
		for(int i = 1; i <= ast; i++){
			cout<<asterisk;
		}
		cout<<endl;
		ast = ast + 2;
	}
	return 0;
}

Once again, thank you. :)

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.