# Matrix user input number

Page 1 of 1

## 8 Replies - 13710 Views - Last Post: 03 October 2012 - 11:26 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294000&amp;s=edbf2599a9f608b90cd2ec321cce217f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kiwi101

Reputation: 0
• Posts: 22
• Joined: 04-May 12

# Matrix user input number

Posted 02 October 2012 - 12:21 PM

Hey guys so I have this assignment and its basically that I have to make this matrix and find the transpose of it. But the tricky part is that the user has to input the matrix and it can be of any size. So like 2 by 3 matrix but the max limit is 10 rows and 10 columns. I've made this so far and Im completely stuck at the user can enter any size matrix.
Thanks!
```#include<stdio.h>
int main(void)
{
int mat[10][10],i,j,cols,rows;
// printf("Enter the number of columns");
// scanf("%d", cols);

// scanf("%d", rows);

for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
scanf("%d",&mat[i][j]);
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{

printf("%d ",mat[i][j]);
}
printf("\n");
}
printf("The transpose is:\n");

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
//mat[i][j]= mat[j][i];
// printf("The transpose is:\n");
printf("%d ", mat[j][i]);
}
printf("\n");
}

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Matrix user input number

### #2 jimblumberg

Reputation: 5652
• Posts: 17,354
• Joined: 25-December 09

## Re: Matrix user input number

Posted 02 October 2012 - 12:29 PM

I would first worry about getting your matrix entered before you worry about transposing it. You have two input statements commented out, if you use those to let the user enter the number of columns and rows. Then use these numbers in your for loop condition statement, instead of the constant (2), you may be halfway there. But in your entry loop you may want to let the user know what they are actually entering, by placing a printf() asking them for the proper input.

Jim

### #3 kiwi101

Reputation: 0
• Posts: 22
• Joined: 04-May 12

## Re: Matrix user input number

Posted 02 October 2012 - 07:26 PM

In this assignment the user has to input a matrix and then the program has to show that matrix and the transpose of it. I've got that part running. My teacher added a tweak. Basically when the user inputs the matrix the program has to only ask the number of columns. And then from there when the user has to input the matrix in matrix form. like for example i tell the program there are 2 columns then i write 12 23
4 5
so then it understands there are 2 rows. I have NO IDEA! here is my code.
```#include<stdio.h>

main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];

printf("Enter the number of rows and columns of matrix ");
scanf("%d%d",&m, &n);
printf("Enter the elements of matrix \n");

for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
scanf("%d",&matrix[c][d]);
}
}

for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{

printf("%d ",matrix[c][d]);
}
printf("\n");
}

for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
transpose[d][c] = matrix[c][d];
}
}

printf("Transpose of entered matrix :-\n");

for( c = 0 ; c < n ; c++ )
{
for( d = 0 ; d < m ; d++ )
{
printf("%d\t",transpose[c][d]);
}
printf("\n");
}

return 0;
}

```

sorry real code
```#include<stdio.h>

main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];

printf("Enter the number of rows and columns of matrix ");
scanf("%d%d",&m, &n);
printf("Enter the elements of matrix \n");

for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
scanf("%d",&matrix[c][d]);
}
}

for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{

printf("%d ",matrix[c][d]);
}
printf("\n");
}

for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
transpose[d][c] = matrix[c][d];
}
}

printf("Transpose of entered matrix :-\n");

for( c = 0 ; c < n ; c++ )
{
for( d = 0 ; d < m ; d++ )
{
printf("%d\t",transpose[c][d]);
}
printf("\n");
}

return 0;
}

```

This post has been edited by Skydiver: 02 October 2012 - 07:56 PM
Reason for edit:: Fixed code tags.

### #4 Skydiver

• Code herder

Reputation: 6675
• Posts: 22,781
• Joined: 05-May 12

## Re: Matrix user input number

Posted 02 October 2012 - 07:59 PM

It should be int main() not just main()

### #5 Skydiver

• Code herder

Reputation: 6675
• Posts: 22,781
• Joined: 05-May 12

## Re: Matrix user input number

Posted 02 October 2012 - 08:08 PM

As for getting input the way your teacher requested, you can do the following pseudo code:
```get the number of rows
for(row = 0; row < rowCount; ++row)
{
get a line of text

columnCount = 0;
while (not end of line)
{
while (whitespace in line and not end of line)
skip forward

while (digits in line and not end of line)
store digits in temporary string

if (temporary string is not empty)
{
value = atoi(temporary string);
matrix[row][columnCount] = value;
columnCount++;
}
}
}

```

An alternative, is to read the documentation of sscanf() very carefully and take advantage of the return value of that function as well as the fact that you have an upper limit on the number of columns:
http://www.cplusplus.../cstdio/sscanf/

### #6 kiwi101

Reputation: 0
• Posts: 22
• Joined: 04-May 12

## Re: Matrix user input number

Posted 03 October 2012 - 04:28 AM

what part of the code do I even add this too??

### #7 jimblumberg

Reputation: 5652
• Posts: 17,354
• Joined: 25-December 09

## Re: Matrix user input number

Posted 03 October 2012 - 06:47 AM

Please don't open two topics for the same question, topics merged.

Jim

### #8 kiwi101

Reputation: 0
• Posts: 22
• Joined: 04-May 12

## Re: Matrix user input number

Posted 03 October 2012 - 06:14 PM

In this program I only input the number of columns and the entries of the matrix. The program then determines the number of rows. The program works perfectly for the correct number of entries like 4 entries for a 2 by 2 matrix. However it doesnt work for 3 entries for a 2 by 2 matrix.
```#include<stdio.h>
#include<stdlib.h>

int  main()
{
int m,x, n, c = 0, d,k, matrix[10][10], transpose[10][10], product[10][10];

printf("Enter the number of columns of matrix ");
scanf("%d",&m);
if(m<=0){
printf("You entered a invalid value.");
exit(0);
}
else{
printf("Enter the elements of matrix \n");

for( c = 0 ; c < 10 ; c++ )
{
for( d = 0 ; d < m ; d++ )
{
scanf("%d",&matrix[c][d]);
if (matrix[c][d] == 99) // 'x' is character variable I declared to use as a break
break;
// c = c+1;

}
if (matrix[c][d] == 99)
break;
}
}

int i;

for(i=0;i<c;i++)
{
for(d=0;d<m;d++)
{
/* if( matrix[c][d] <0){
printf("Not possible");
exit(0);
}
else{*/

printf("%3d ",matrix[i][d]);
}
printf("\n");
}

```

```#include<stdio.h>
#include<stdlib.h>

int  main()
{
int m,x, n, c = 0, d,k, matrix[10][10], transpose[10][10], product[10][10];

printf("Enter the number of columns of matrix ");
scanf("%d",&m);
if(m<=0){
printf("You entered a invalid value.");
exit(0);
}
else{
printf("Enter the elements of matrix \n");

for( c = 0 ; c < 10 ; c++ )
{
for( d = 0 ; d < m ; d++ )
{
scanf("%d",&matrix[c][d]);
if (matrix[c][d] == 99) // 'x' is character variable I declared to use as a break
break;
// c = c+1;

}
if (matrix[c][d] == 99)
break;
}
}

int i;

for(i=0;i<c;i++)
{
for(d=0;d<m;d++)
{
/* if( matrix[c][d] <0){
printf("Not possible");
exit(0);
}
else{*/

printf("%3d ",matrix[i][d]);
}
printf("\n");
}

```

This post has been edited by Skydiver: 03 October 2012 - 11:24 PM
Reason for edit:: Fixed code tags with incorrect backslash instead of forward slash.

### #9 Skydiver

• Code herder

Reputation: 6675
• Posts: 22,781
• Joined: 05-May 12

## Re: Matrix user input number

Posted 03 October 2012 - 11:26 PM

Please don't open new topics for an existing topic. You already have one here: http://www.dreaminco...r-input-number/

Edit after: I've merged in the new topic started with this existing topic.

This post has been edited by Skydiver: 04 October 2012 - 12:49 AM