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
GOOD LUCK !
Advertisement
nice post…
the code segment of ur post is not image….
how did u make it huh?
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
For other method you may check this.