Diamond shape

Hello , i was doing an exercise in book which asked to draw a diamond shape by displaying '*' using nested for loops. It took me quite a while to come up with a way to reverse the pyramid , and complete this exercise. So i was hoping some one can take a look at this code and let me know what they would have done different. Thanks. Again sorry for posting on multiple forums i just value responses i get and hopefully learn something. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//draws a diamond shape ex
/* 

     *
    ***
   *****
    ***
     *  
*/


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


using namespace std;

int main(){

	int nRow, nColumn, nDiamond = 8, nCenter, nColumnSize;
	
	nCenter = nDiamond / 2;

	for ( nRow = 0; nRow <= nDiamond; nRow++ ){

		nColumnSize = nRow <= nCenter ? nRow + nCenter : nColumnSize - (nCenter / nCenter);

		for ( nColumn = 0; nColumn <= nColumnSize; nColumn++ ){ 

			//checking if we at center 
			if ( nRow <= nCenter ) {

				if ( nCenter - nRow > nColumn ) 
					cout << ' ';
				else
					cout << '*';
			//reversing 
			}else if ( nRow - nCenter > nColumn )
				cout << ' ';
			else
				cout << '*';
		}// end columns

		cout << endl;

	} //end rows

	return 0;
}
.
Last edited on
Topic archived. No new replies allowed.