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
This is a discussion on short matrix problem within the C Programming forums, part of the General Programming Boards category; Yes you are correct Letme show u an example Enter the elements of matrix 1 2 3 4 99 Here ...
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
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?
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]); } }
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
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
Detects short rows, (last row only), but won't detect if a full row is missing.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");
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
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
How about:
You don't need the else part, since the if part exits.Code:if (d > 0) { printf("..."); exit(1); }
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
OMGSH thankyou soo muchit worked
but yeah i was just reading about fflush online too cuz mine also got stuck on the 2nd scanf
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
but ive been trying this for a while but i cant seem to understand where exactly in the code i should put itCode:if(matrix[c][d] < 0){ printf("not valid number"); exit(1);}
ive tired numerous times but nothing has worked out
check everytime after scanf. sry, im new here... im have no idea how to edit with color on ur code...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; } }
Last edited by Neko Tan; 10-03-2012 at 10:55 PM.
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;
The transpose is right its just the multiplication of the matrix and transpose sometimes comes wrong
sorry about the indentation