If the user chooses 2 columns, and then enters 6 numbers, you want to enter it into the matrix like this?
Code:
5 2 0 0 0 0 0 0 0 0
6 8 0 0 0 0 0 0 0 0
1 4 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
You can keep your 10 x 10 matrix size if you want, just set a column max int variable (colMax = 2), and use your count of entered numbers to calculate the rowMax you'll need:
Code:
if(count % 2 == 0)
rowMax = count /colMax
else
rowMax = count / colMax + 1 (handles odd count of numbers
//then a run through the data could be:
for(r = 0, count = 0;r < rowMax;r++) { //dataMax = number of data values entered
for(c = 0;c < colMax;c++) {
printf("%d", matrix[r][c]);
if(++count == dataMax) //break out of the column loop
break;
}
if(count == dataMax) //break out of the row loop
break;
}
Another way would be to first, set all the matrix elements to a value that is completely impossible for your data (-99 perhaps, for positive data). The data can be scanned through by using that "out of bounds" value.
Code:
#define OB -99
//(Just below your include file list. OB = out of bounds)
for(row = 0;row < 10; row++) {
for(col = 0; col < 10; col++) {
if(matrix[row][col] == OB)
break;
printf("%d", matrix[row][col]);
}
}
If you need to work with the data a great deal, creating a matrix just the right size with malloc or calloc, might well be a better way to do this.