Saturday, November 23
Hollow Square & Hollow diamond & Hollow Triangle
Hollow Square & Hollow diamond & Hollow Triangle
It's basically a request from a student as we go further in loops and functions we need to know more tricks and practise more for these things which eventually leads to good programming;
Basic concept to understand;
- Notice if any line of the shape is going to match original filled shape; i.e. in square the first line and last line are filled!
- Make sure you know the ration of spaces for hollow part to the number of characters in the corresponding filled space i.e. if your square is of the size of 5x5 so it is going to be 5 characters in a single line; but in hollow shape you are going to print only 2 characters ; the first one and last one no matter how much long line is this; but here in 5 character line the total spaces would be countable as spaces=one side length - 2;
Hence by mixing these points you can make logic of this king of shapes.
Basic Hollow looping;
In this kind of looping you have to make sure that ration of lines is safely described to make appropriate loop
Square concept:
- int main()
- {
- int size; //suppose it is 5
- cin>>size;
- for (int i = 0 ; i < size ; i++ ) //runs 5 times
- {
- cout << "*" ; //first char
- for (int j=2;j<size;j++) //runs 5-2 times
- {
- cout<<" ";
- }
- cout << "*" ; //last char
- }
- return 0;
- }
And for first and last line here is simple method:
- if ( line 1 || last line ) //mean i==1 or i==size-1 according to loop iteration
- {
- for ( int a=0;a<size;a++ )
- {
- cout<< "*" ;
- }
- cout << endl ; continue ;
- }
i guess it is enough for the basic concept and you will be able to make assignment ;; I'll give complete codes after exams;; thanks for reading and for feedback or any questions:
Join the group on facebook for constant news about new posts..
Very Good Explanation ,,,,, Usman Thumbs Up for You (y)
ReplyDeleteFar better code:
ReplyDeletehttp://www.easycppcodes.com/2014/12/generate-hollow-diamond-using-for-loop.html
Hollow diamond shape program with explanation
ReplyDelete