# Thread: exit function in matrix

1. ## exit function in matrix

Hey so Im writing a program for a user to enter a matrix and then I give its transpose and the product of the transpose and the matrix. There are 2 problems I am having. The first is I want to use the exit function. So if the user enter -2 as one of the matrix entries I want the program to say wring value and exit. But for some reason its not working. The second is I am not getting the correct answer for the product of transpose and matrix. PLEASE HELP ME!
Code:
``` #include<stdio.h>

int 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]);
}
}

printf("\nHere is your matrix:\n");
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");
}
printf("The product of matrix and transpose is %d:", matrix[c][d] * transpose[c][d]);

return 0;
}```

2. The function exit() has its prototype in <stdlib.h>. You need to include that system header.

And you might want to review how to multiply matrixes (Wikipedia has a nice article) rather than accessing non-existing elements.

3. Okay so i got the exit function running in one part but for the actual matrix values its not working fully
could you please check it out and tell me
Code:
```#include<stdio.h>#include<stdlib.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);
if(m<=0 ||  n<=0){
printf("You entered a invalid value.");
exit(0);
}
else{
printf("Enter the elements of matrix \n");

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

}
}
}

printf("\nHere is your matrix:\n");

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

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");
}
printf("The product of matrix and transpose is %d:", matrix[c][d] * transpose[c][d]);

return 0;
}```

4. #include<stdlib.h> comes on a different line

5. History reference for the regulars -> Entering A Matrix - C And C++ | Dream.In.Code

6. Im sorry I just had 2 questions kill me if i used another forum

thanks for the article it helped i got the multiplication part down too the only thing left is the exit function if the entered number in the matrix is negative i cant seem to put the exit function in the right part of the matrix. Please help

7. Originally Posted by kiwi101
... i got the multiplication part down ...
... if the entered number in the matrix is negative i cant seem to put the exit function in the right part of the matrix ...
Nice going with the multiplication.
Care to show your code?

The right part of the code to include a call to the exit() function is inside the input loop.
You need an if there too.

8. sure I defined a third array product[10][10] and an integer k
and then
Code:
``` for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
product[c][d]=0;
for(k=0;k< n;k++) {
product[c][d] = product[c][d] + matrix[c][k] * transpose[k][d];
}
}
}
printf("\nThe Product Of The Two Matrices Is:\n\n");

for(c=0;c< m;c++) {
for(d=0;d< n;d++) {
printf(" %d ",product[c][d]);
}
printf("\n");
}```
okay so u want me to write
Code:
```if(matrix[c][d] <0)
{
printf("Negative value");
exit();
else{
//for loop```
i did that right where it says printf("here is your matrix"); and after the for loop but it isnt working

9. Your matrix multiplication code doesn't appear correct.
Notice that the multiplication of a matrix and its transpose is a square matrix.

10. oh i just noticed.
the multiplication works right only for square matrices but not for any other type
what should i do?

11. > what should i do?
Lookup on google what the general approach for an X by Y matrix is, and then write the code for it.
Matrix multiplication - Wikipedia, the free encyclopedia

Mostly, it would seem to be just about being careful about choosing your loop limits.

12. So if I use this loop I end up with the right answer but the dimensions arent right so there are like 2 garbage values in the matrix
Code:
``` for( i = 0 ; i < c ; i++ )
{
for( d = 0 ; d < m ; d++ )

{
product[i][d]=0;
for(k=0;k<m;k++) {
product[i][d] = product[i][d] + matrix[i][d] * transpose[i][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");
}```

13. just to remind my matrix is c*m matrix
c= number of rows ; m= number of columns

Could you please tell mewhat im doing wrong

14. Perhaps you should be more consistent with your choice of loop variables, and loop limits.
Code:
``` for( i = 0 ; i < c ; i++ )
for( d = 0 ; d < m ; d++ )
// vs
for(i=0; i<c; i++)
for(d=0; d<k; d++)  //!! what? not m?```
Having more meaningful variable names would help, like for example numRows and numColumns

Single letters are fine for loop subscripts, but everything else should have a better name.

15. Resume here -> short matrix problem

Popular pages Recent additions