Thursday, July 30, 2009

Help me with a C program...?

I wrote this C program but it doesn't work...


It is about arrays...


I would appreciate your help...


main ()


{


int Array a1[2][3]={{1,2,3},(4,5,6),(7,8,9)};


int Array a2[2][3]={(9,8,7),(6,5,4),(3,2,1)};


int Array a3[2][3];


int i,j;


for (i=0; i%26lt;2;i++)


{


for (j=0; j%26lt;3; j++)


a3[i][j]=a1[2][3]+a2[2][3];


}


for (i=0; i%26lt;2;i++)


{


for (j=0; j%26lt;3; j++)


printf ("%5d",a3[2][3]);


}


getch();


}





P.S I'm not sure about the arrays syntax...


I use turbo c++ 3.0 compiler

Help me with a C program...?
Your array declarations have a few problems. The word Array is both unnecessary and incorrect. You also are declaring a 2x3 multi-dimensional array, but you are initializing it with a 3x3 multi-dimensional array. The initialization is also using parentheses instead of curly braces. The correct declaration for array a1 would be:


int a1[2][3] = {{1,2,3}, {4,5,6}};





I corrected what I found as errors and ended up with the following code:





#include %26lt;stdio.h%26gt;





int main ()


{


int a1[2][3] = {{1,2,3},{4,5,6}};


int a2[2][3] = {{6,5,4},{3,2,1}};


int a3[2][3];


int i,j;


for (i=0; i%26lt;2; i++)


{


for (j=0; j%26lt;3; j++)


a3[i][j] = a1[i][j] + a2[i][j];


}


for (i=0; i%26lt;2; i++)


{


for (j=0; j%26lt;3; j++)


printf("%5d",a3[i][j]);


printf("\n");


}





return 0;


}





Copying and pasting should show you the correct indentation. Hope this helps.
Reply:replace this:


int Array a1[2][3]={{1,2,3},(4,5,6),(7,8,9)};


int Array a2[2][3]={(9,8,7),(6,5,4),(3,2,1)};


int Array a3[2][3];





by this:


int a1[3][3]={{1,2,3},{4,5,6},{7,8,9}};


int a2[3][3]={{9,8,7},{6,5,4},{3,2,1}};


int a3[3][3];








also printf might need this line at the beginning:


#include %26lt;stdio.h%26gt;





and getch might need also some header file, not sure which one (I'm not having turbo c++ right now), maybe:


#include %26lt;conio.h%26gt;


No comments:

Post a Comment