# short matrix problem

Show 80 post(s) from this thread on one page
Page 1 of 6 123456 Last
• 10-03-2012
kiwi101
short matrix problem
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. Please reply fast
Code:

Code:

```#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) // 99 is  variable I declared to use as a break           break;           }         if (matrix[c][d] == 99)           break;       }   }   printf("\nHere is your matrix:\n");   int i;   for(i=0;i<c;i++)     {       for(d=0;d<m;d++)         {  printf("%3d ",matrix[i][d]);         } printf("\n");     }```
• 10-03-2012
laserlight
Quote:

Originally Posted by kiwi101

fast

(If I wanted to be nasty, I would have let my reply stop here, but I'll be kind. Next time, don't insist that people "reply fast".)

Quote:

Originally Posted by kiwi101
However it doesnt work for 3 entries for a 2 by 2 matrix.

How does it not work? Also, you should indent your code properly to make it easier to read.
• 10-03-2012
TheBigH
Do you want an m x m, or a 10 x m matrix?
• 10-03-2012
Neko Tan
u miss fflush()... u need to clear ur buffer rite after input from user else ur next scanf will still take ur previous entered number as input(in the case that next scanf very close together)

question for u: are u on a open book test or something? :cool:
• 10-03-2012
kiwi101
sorry :(
i was just getting tensed since ive bin working on this for ages and i cant get any ideas

it does work if i put 3 entries but not the way it shoud like itll give me garbage values.
• 10-03-2012
kiwi101
no im not i just have this due in a couple of hours.

Could u elaborate? ive neve rused fflush
• 10-03-2012
kiwi101
Okay I have an idea I just dont know how to quite put it down. If the user doesnt enter m*n entries then the program should just exit
kindof like this
Code:

``` if(m*c%m != 0){             printf("NO");             exit(0);           }             else{ print```
• 10-03-2012
oogabooga
Why haven't you answered laserlight's or TheBigH's questions?

Neko Tan doesn't know what it's talking about. fflush is not needed.

Indent your code properly. Look at post #7. Is that really how you mean your code to look?
• 10-03-2012
kiwi101
Look im just trying to brainstorm i want the matrix to be a m*n matrix.
i want my code to exit if the user doesnt enter m*n values.
Could you please give me some direction?
Im waitng here
• 10-03-2012
oogabooga
If it's supposed to be m x n why don't you set n to anything? You're not even using it.
• 10-03-2012
kiwi101
m = # of rows
in this program my # of rows is called c

n= # of columns
in this program my # of colums is called m

so a c*m matrix
• 10-03-2012
kiwi101
Could you guys please not make fun of me and help me plz
• 10-03-2012
Yes, I see your stressed out, so I deleted my funny post.

Quickly, what's the problem?
• 10-03-2012
oogabooga
Your variable names are terrible. Why is n called m and m called c???

Anyway, I think I'm beginning to understand what you're up to here. You want the user to enter the number of columns (but NOT the number of rows). Then the user inputs rows of data until they decide to stop by entering the number 99.

Is that correct?

Can you show an exact example of what doesn't work?
• 10-03-2012
kiwi101
Thanks :)
let me explain it with an example please tell me if you get it
when the user enters 2 as the # of columns. And then enter 1,2,3,4 as the input. The program automatically displays a 2 by 2 matrix with 1,2,3,4 entries. However my problem is that if that same user accidentally only entered 1,2,3 then I want the program to exit using the exit function. But I don't know how to write it properly could you please tell me how i should write it.
Show 80 post(s) from this thread on one page
Page 1 of 6 123456 Last