Originally Posted by
banny
Hi,
I am writing a code where I need a 11 cross 11 matrix (M) with all the elements zero, except
M[5][5] = 1. I wrote the following code
Code:
#include<stdio.h>
main()
{
int i,j;
int M[10][10];
for(i=0;i<=10;i++)
{
for(j=0;j<=10;j++)
{
M[i][j]=0;
}
}
M[5][5] = 1;
}
The problem is that the last line, M[5][5]=1 changes the all the elements in the 6th column to 1. I want only the 6th element in the 6th column to be 1. Can somebody tell me how to correct this mistake ?
Thanks,
banny
If you need a matrix 11 X 11, then make it 11 X 11, not 10 X 10. Your elements will then number from M[0][0] to M[10][10], so 11 rows, and 11 columns.
Code:
int M[11][11] = { { 0 } }; sets all array to 0
M[5][5] = 1; //assigns one to the 6th row, the sixth column, of M[][]
Your code looks much better wrapped in code tags and indented. Thanks, banny.