Hi there!
I wrote this program as my self teaching exercise, to print a diamond pattern. It works fine, but I've got a feeling that there must be a better way to do this, and to optimise the code. Could someone show me, please? Many thanks in anticipation.
Code://print diamond using * #include <iostream.h> #include <conio.h> int main() { int t, b, s, n; do { clrscr(); cout << "Enter integer number: "; cin >> n; n += 1; n /= 2; //top of diamond or pyramid for (t = 1; t <= n; t++) { for (b = n; b >= t; b--) cout << " "; for (s = 1; s <= (t * 2 - 1); s++) cout << "*"; cout << endl; } //bottom of diamond for (t = 1; t <= n - 1; t++) { for (b = 1; b <= t + 1; b++) cout << " "; for (s = 1; s <= n * 2 - (t * 2 + 1); s++) cout << "*"; cout << endl; } cout << "Any key again, Esc to quit."; } while (getch()!=27); return 0; }



LinkBack URL
About LinkBacks


