Thread: exit function in matrix

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    210

    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;
    }
    Last edited by kiwi101; 10-03-2012 at 06:10 AM.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    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. #3
    Registered User
    Join Date
    May 2012
    Posts
    210
    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. #4
    Registered User
    Join Date
    May 2012
    Posts
    210
    #include<stdlib.h> comes on a different line

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    History reference for the regulars -> Entering A Matrix - C And C++ | Dream.In.Code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    210
    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. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by kiwi101 View Post
    ... 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. #8
    Registered User
    Join Date
    May 2012
    Posts
    210
    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. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Your matrix multiplication code doesn't appear correct.
    Notice that the multiplication of a matrix and its transpose is a square matrix.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    210
    oh i just noticed.
    the multiplication works right only for square matrices but not for any other type
    what should i do?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    210
    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. #13
    Registered User
    Join Date
    May 2012
    Posts
    210
    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. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Resume here -> short matrix problem
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. implementaion of exit function
    By msenthil in forum C Programming
    Replies: 4
    Last Post: 05-16-2007, 09:33 AM
  2. Exit Function
    By the pooper in forum C Programming
    Replies: 3
    Last Post: 09-03-2005, 06:48 PM
  3. about exit() function
    By deian in forum C Programming
    Replies: 1
    Last Post: 10-20-2004, 12:34 PM
  4. Exit a function
    By MasterGBC in forum C Programming
    Replies: 3
    Last Post: 04-10-2003, 10:46 PM
  5. the exit() - function
    By tim_w in forum C Programming
    Replies: 10
    Last Post: 02-13-2002, 11:14 PM