# short matrix problem

Printable View

Show 80 post(s) from this thread on one page
Page 2 of 6 First 123456 Last
• 10-03-2012
kiwi101
Yes you are correct
Letme show u an example

Enter the elements of matrix
1 2 3 4
99

Here is your matrix:
1 2
3 4

BUT:
Enter the elements of matrix
1 2 3
99

Here is your matrix:
1 2
• 10-03-2012
kiwi101
instead of displaying just 1 2 i want the program to say "invald # of entries" and then exit

could you please tell me how to do that?
• 10-03-2012
oogabooga
So you want to ensure that the user enters complete rows.

Two possibilities:

1. Check that the value of d is 0 (i.e., they entered 99 as the first value in a column).

2. Write it so entering the magic number (99) only works if it's the first value in a column.
Code:

```for (c = 0; c < 10; c++) {     scanf("%d", &val);     if (val == 99)         break;     matrix[c][0] = val;     for (d = 1; d < m; d++)  // note that d starts at 1 here     {         scanf("%d", &matrix[c][d]);     } }```
• 10-03-2012
kiwi101
when I enter 1,2,3,4,99 this is what I get back from the code
Here is your matrix:
1 2
3 4
BUT

when i enter 1,2,3,99 this is what i get back
Here is your matrix:
1 2
• 10-03-2012
Adak
Code:

```    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 to use as a break               break;                       }                 if (matrix[c][d] == 99)           break;       }       }   if(d<m)     printf("\nArray data is missing.\n");```
Detects short rows, (last row only), but won't detect if a full row is missing.
• 10-03-2012
kiwi101
Im so grateful to the two of you but both codes didnt work for me
The problem i have with oogaboogas code is if they enter 99 first how will the program know when the user has finished entering all entries
and the problem with adaks code is for some reason it prints the statement everytime
• 10-03-2012
kiwi101
Is there anyway you guys can write this statement in code and tellme where to place it it might work
Code:

```if( #ofentries != c*m) printf("Invalid number of entries); exit(0); else{ print```
• 10-03-2012
oogabooga
How about:
Code:

```if (d > 0) {     printf("...");     exit(1); }```
You don't need the else part, since the if part exits.
• 10-03-2012
Neko Tan
Quote:

Originally Posted by 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?

maybe this happen only on my compiler... it seem like stuck on 2nd scanf (if without fflush)...
• 10-03-2012
kiwi101
OMGSH thankyou soo muchit worked
but yeah i was just reading about fflush online too cuz mine also got stuck on the 2nd scanf
• 10-03-2012
kiwi101
i had one last question sorry to annoy you so much today
i want the program to exit if the user inputs a value in the matrix less than 0. i plan on writing it like
Code:

```if(matrix[c][d] < 0){ printf("not valid number"); exit(1);}```
but ive been trying this for a while but i cant seem to understand where exactly in the code i should put it
ive tired numerous times but nothing has worked out
• 10-03-2012
Neko Tan
Quote:

Originally Posted by kiwi101
i had one last question sorry to annoy you so much today
i want the program to exit if the user inputs a value in the matrix less than 0. i plan on writing it like
Code:

```if(matrix[c][d] < 0){ printf("not valid number"); exit(1);}```
but ive been trying this for a while but i cant seem to understand where exactly in the code i should put it
ive tired numerous times but nothing has worked out

Code:

``` 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] < 0){           printf ("not valid number");           getch();           exit(1);}               }         if (matrix[c][d] == 99)           break;       }   }```
check everytime after scanf. sry, im new here... im have no idea how to edit with color on ur code...
• 10-03-2012
kiwi101
Thanks!!!! Its working!

Could you tell me why my multiplication of this matrix and its transpose is coming wrong. Its usually wrong if its not a square matrix.

Code:

``` for( i = 0 ; i < c ; i++ )     {       for( d = 0 ; d < m ; d++ )         {           transpose[d][i] = matrix[i][d];         }     }   printf("Transpose of entered matrix :-\n");   for( i = 0 ; i < m ; i++ )     {       for( d = 0 ; d < c ; d++ )         {           printf("%3d\t",transpose[i][d]);         }       printf("\n");     }   // printf("The product of matrix and transpose is %d:");   for( i = 0 ; i < c ; i++ )     {       for( d = 0 ; d < m ; d++ )         {           product[i][d]=0;           for(k=0;k<c;k++) {             product[i][d] = product[i][d] + matrix[i][k] * transpose[k][d];           }         }     }   printf("\nThe Product Of The Two Matrices Is:\n\n");   for(i=0; i<c; i++) {     for(d=0; d<k; d++) {       printf(" %3d ",product[i][d]);     }     printf("\n");   }   return 0;```
• 10-03-2012
kiwi101
The transpose is right its just the multiplication of the matrix and transpose sometimes comes wrong
• 10-03-2012
kiwi101
sorry about the indentation :(
Show 80 post(s) from this thread on one page
Page 2 of 6 First 123456 Last