C++ gurus and learners, we program ourselves :P all work is CC licensed

Wednesday, November 6

Special Diamonds Program Set

21:22 By

A post by Usman of BESE 19 at MCS.



The simple program logic for Diamond printing program:

1.       We could simply use four nested loop sets for printing this shape as in part 1;
but it would take about 8 loops for printing a sum of 4 shapes structure [ in diamond 4 triangles are summed up to make its structure ]
so to make it a bit simple [ 4 + 2 loop in which 4 loops are for triangles and 2 loops join them ] we divide it into 2 parts instead of 4 like in part 1, this is described in part 2;
2.       Part 2 has simple loop nesting by dividing main shape into 2 parts as Upper half and Lower half;

Part 1;

This will help you understanding loops for printing shape’s basic structure which is triangle in this topic;
a.       Make simple triangle of 2 loops as a set is very basic;

int i,j,rows,size;
cin>>rows;     //take input
size = rows;     //give the initial size of shape which is decreasing
size = 1;      //if shape is increasing then size is 1
do      //it will run row’s * time
{ j=size;      //it will change size of rows constantly
           do     //this loop is always as this format whatever shape is decreasing or increasing as it has to print a line of given width anywhere required
 {
                        cout<<”required character”; // do not put endl here as it is a continues line
                               j--;
               } while (j < size);
size--;     //if size of shape is decreasing downwards
size++;    //if size of shape is increasing downwards
cout<<endl; 
}while (i<given rows);

Please visit next part at Special Diamonds Part 2

1 comments :

  1. sorry for garbled text, author might get online later to get it working :P

    ReplyDelete