Print a Diamond shape using array

Previously I have posted a similar post on how to print diamond shape using asterisks in c/c++
Some of you have asked me if there is any other way to solve this diamond shape asterisks problem. Of course there is. You may use array. The array will create a matrix (For an example here we have a 5×5 matrix) .You just need to specify where do you want to put the asterisks that’s all . The code should be look like this-

#include<iostream>


using namespace std;


int main()


{

int i,j;

char arr[5][5];


for(i=0;i<5;i++)

  { for(j=0;j<5;j++)

     {  
arr[i][j]=' ';

 
         }
           }


	arr[0][2]='*';

	arr[1][1]='*';

	arr[1][2]='*';

	arr[1][3]='*';

	arr[2][0]='*';

	arr[2][1]='*';

	arr[2][2]='*';

	arr[2][3]='*';

	arr[2][4]='*';

	arr[3][1]='*';

	arr[3][2]='*';

	arr[3][3]='*';

	arr[4][2]='*';



for(i=0;i<5;i++)

     {  cout<<endl;

        
for( j=0;j<5;j++)

	{   cout<<" ";

	    cout<<arr[i][j];

	     }

               }


	return 0;

}



Just change the position of the asterisks – arr[0][2]=’*'; arr[1][1]=’*’ ….. etc. And create a new shape :D
GOOD LUCK !

Advertisement
This entry was posted in C/C++, programming and tagged , . Bookmark the permalink.

3 Responses to Print a Diamond shape using array

  1. nice post…

    the code segment of ur post is not image….
    how did u make it huh? :)

  2. Amit tyagi says:

    it’s nice.but i want to print my name by ‘*’.if you have anther way to creat it by looping .so plz give me tips about it.thank you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s