Augustinas Žiliukas Augustinas Žiliukas - 24 days ago 5
C Question

C two dimensional array input

My code for inputing pairs of numbers,inputs numbers in strange order,can't figure out whats wrong.I added aditional for loop to look at inputed values.n is the number of pairs user should enter.

int main(){ int i,j,n,pairs[100][1];
printf("Select the number of pairs you want to enter(no more than 100):");
scanf("%d",&n);
printf("Enter the pairs:");
for(i=0; i<n; i++ ) {
for( j=0; j<2; j++ ) {
scanf("%d", &pairs[i][j]);
}
}
for ( i = 0; i < n; i++ ) {
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %d\n", i,j, pairs[i][j] );
}
}
return 0;
}


Lets say i choose the number of pairs 2,then i input 4space6enter 7space9enter,my array should look like:

pairs[0][0]=4
pairs[0][1]=6
pairs[1][0]=7
pairs[1][1]=9


But now i get:

pairs[0][0]=4
pairs[0][1]=7
pairs[1][0]=7
pairs[1][1]=9

Answer

int pairs[100][1];

A "pair" usually requires two elements. Try

int pairs[100][2];

Comments