how to do this in array?..
Feb 25, 2011 at 6:07am UTC
The problem is to make this a triangle using array, i already made a triangle using only for loop but i don't know how to do this in array..
here is the code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include<stdio.h>
#include<conio.h>
#define tri 10
main()
{
int x,y,z;
for (x=tri;x>0;x--)
{
printf("\t" );
for (y=(tri-x);y>0;y--)
{
printf(" " );
}
for (z=x;z>0;z--)
{
printf("*" );
printf(" " );
}
printf("\n" );
}
getch();
}
can anyone give me an idea on how to do this using array?.. please help me.. thank you!!..
Feb 25, 2011 at 7:07am UTC
this should set up your array what you would need to do is replace your current code by say giving the value 1 to all triangle points and 0 to all spaces. Then you would need to print out the array.
basically the array is triangle[10][10]; You could use strings instead of doubles if you want to directly print out the array.
1 2 3 4 5 6 7 8 9
int i,j;
double **triangle;
triangle = new double *[tri];
for (i = 0; i <= tri; i++)
{
triangle[i] = new double [tri];
}
i = 0;
j = 0;
Feb 25, 2011 at 7:14am UTC
You mean this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#define tri 10
int main()
{
int xx[2*tri-1][2*tri-1]={0};
for (int i=0;i<10;i++)
{
for (int j=i;j<=2*tri-j-2;j+=2)
{
xx[i][j]=1;
xx[i][2*tri-j-2]=1;
}
}
for (int i=0;i<tri;i++)
{
for (int j=0;j<2*tri-1;j++)
{
std::cout<<xx[i][j];
}
printf("\n" );
}
std::cin.get();
return 0;
}
Last edited on Feb 25, 2011 at 7:15am UTC
Feb 25, 2011 at 8:49am UTC
Not quite, but without answering your problem completely, here's what the input into the array should look like based off your code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
int x,y,z;
int i,j;
string **triangle;
triangle = new string*[tri];
for (i = 0; i <= tri; i++)
{
triangle[i] = new string[tri];
}
i = 0;
j = 0;
for (x=tri;x>0;x--)
{
for (y=(tri-x);y>0;y--)
{
triangle[i][j] = " " ;
j++;
}
for (z=x;z>0;z--)
{
triangle[i][j] = "* " ;
j++;
}
i++;
j=0;
}
Topic archived. No new replies allowed.