# Thread: how to get rows in a matrix when only columns are given

1. ## how to get rows in a matrix when only columns are given

The program starts with a matrix with a maximum dimensions of 10 by 10. I have to ask the user to input the number of columns he would like the matrix to be. Once he enters that data he has to enter the values. For example he enters 2, therefore he has a maximum of 20 characters to enter. The problem is what if he enters only 13, or 14, or 6? How would I calculate the number of rows?
I'm planning on putting a break, for example I tell the user to enter 'x' to stop the input. How can I go ahead and do this? 2. Read things in as string rather than as ints, then check whether they typed digits or an x. 3. Thanks, but my main question is how to set up the rows after the number of columns has been defined and the values have been entered 4. So are you asking how to dynamically resize an array? That is what it sounds like, take a look at the realloc() function. If you are allowed to ask how many inputs there will be prior too however, you could always use a variable length array (VLA). 5. It sounds to me like you don't. If they enter a number greater than ten then clearly it wont fit in a 10 by 10 matrix and so an error should be given.

I'd use an if-statement to check the entered value against 'x' and then perhaps a break statement. Really would be best if you just try it and post that code if you have problems. 6. 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. 7. ## Thanks everyone

iMalc> yes I know I have to check for errors. I'm planning on putting a break after the data entry to check if the input is not a number then the program will stop.

Adak, that's exactly what I'm trying to do. But I'm not planning on keeping the zeroes. It's of no help. I just couldn't figure out how to calculate the number of rows. The forst part of your code is great
Code:
```
rowMax = count /colMax
else
rowMax = count / colMax + 1 (handles odd count of numbers```
I'm going to go ahead and keep working on my program.

Thanks again!  8. I was working on my program and found a mistake with the code to fins the number of rows.

The code checks if the number is divisible by 2 and makes the rows equals to the number of items divided by the number of columns. This is not the case for all of them. For example I have 5 columns and 14 items. 14/2= remainder 0. Rows = 14/5 = 2. I need 3 rows to fit all the data.

I added something to the code but it doesn't seem to fix it
Code:
```

if(i%2==0){
if((i%2)%2==0)
rows = (i/cols);
else
rows = (i/cols)+1;
}
else
rows = (i/cols)+1;``` 9. Why do you keep dividing by 2? Why not divide by cols instead? 10. I am assuming you are only dealing with square matrices.

Then columns = rows in all cases.

A matrix can be different dimensions, such as 1x2 2x5 5x2 10x1 1x10 up to a max size of 10x10. A square matrix is when the rows = columns, take a look at Matrix (mathematics) - Wikipedia, the free encyclopedia to see more about matrices.

If you are not dealing with square matrices, I would suggest to have the person enter in the number of columns, then the number of rows. Then set up an array "matrix[Col][Row] (or would it be matrix[Row][Col]??, I'm used to deal with matrices in MATLAB where it is A = [1,2,3;4,5,6;7,8,9] which yields a matrix of
Code:
```A = 1 2 3
4 5 6
7 8 9``` 11. I have some code but I still have a problem with it
this is my code
Code:
```
for(i = 0; i <10; i++){        //10 is the maximum number of rows, since rows are not specified I'm using 10
for(j = 0; j <cols; j++){       //cols is the number of columns entered
scanf("%d", &matrix[i][j]);
}
}

if(i%cols == 0)          /**********************************/
rows = (i/cols);      *this section is to calculate the number*
else                        *after getting columns and the number of*
rows = (i/cols)+1; * items                                              *
/***********************************/

for(i=0; i<rows; i++){        //this section is just to display the matrix
for(j = 0; j<cols; j++){
printf("%3d", matrix[i][j]);
}
printf("\n");
}```

my problem is that even though I terminate the loop to enter the elements in the matrix, the loop keeps counting up to 10. Therefore the number of rows is wrong. This gives me an output of 2 extra rows filled with zeros like this.
Code:
```
1  2  3
4  5  6
0  0  0```
How can I stop the loop from going all the way to 10 if I don't want it to go all the way to 10?

i tried putting a break but that didn't make it. This is what I used
Code:
```for(i = 0; i <10; i++){
for(j = 0; j <cols; j++){
scanf("%d", &matrix[i][j]);
if(scanf("%d", &matrix[i][j]) == x)  'x' is character variable I declared to use as a break
break;
}
}``` 12. I have some code but I still have a problem with it
this is the part of my code for entering the numbers and calculating the rows
Code:
```
for(i = 0; i <10; i++){        //10 is the maximum number of rows, since rows are not specified I'm using 10
for(j = 0; j <cols; j++){       //cols is the number of columns entered
scanf("%d", &matrix[i][j]);
}
}

if(i%cols == 0)          /**********************************/
rows = (i/cols);      *this section is to calculate the number*
else                        *after getting columns and the number of*
rows = (i/cols)+1; * items                                              *
/***********************************/

for(i=0; i<rows; i++){        //this section is just to display the matrix
for(j = 0; j<cols; j++){
printf("%3d", matrix[i][j]);
}
printf("\n");
}```

my problem is that even though I terminate the loop to enter the elements in the matrix, the loop keeps counting up to 10. Therefore the number of rows is wrong. This gives me an output of 2 extra rows filled with zeros. For example I enter 3 columns, then I enter 6 elements it gives me an output like this
Code:
```
1  2  3
4  5  6
0  0  0```
How can I stop the loop from going all the way to 10 if I don't want it to go all the way to 10?

i tried putting a break but that didn't make it. This is what I used
Code:
```for(i = 0; i <10; i++){
for(j = 0; j <cols; j++){
scanf("%d", &matrix[i][j]);
if(scanf("%d", &matrix[i][j]) == x)  'x' is character variable I declared to use as a break
break;
}
}```
I put an printf statement for my i and j in the loop, and i goes from 0 to 9, which shouldn't be if I terminate the loop. Or maybe I'm just doing it wrong Popular pages Recent additions arrays, c programming, matrices 