Thread: homework help! find diagonal sum of matrix

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    23

    Angry homework help! find diagonal sum of matrix

    Hi, I need some help with my code.

    My assignment is to
    Write a C program that finds thesum of the diagonal elements of a matrix. Specifically, the programshould first prompt the user to input the number of rows and columnsof the matrix, then prompt the user to input its elements, then printthe matrix the user entered and finally print the sum of the diagonalelements. You can assume that the maximum number ofrows and columns the user inputs is 10, that the number of rows andcolumns the user enters are always equal, and the elements of thematrix to be integer numbers.

    Implementation Requirements:

    1. Implement a function called “findDiagSum” to find the sum of the diagonal elements. The function should return the sum of the diagonal elements (which would be an integer number). Define the function prototype appropriately in your program. (Carefully think over the arguments of the function)

      2. Implement a function called “print_matrix”, whose prototype is—


      voidprint_matrix(int mat[10][10], int r, int c);”
      Define the functionprototype in your program.

      Code:
      
      
      void print_matrix(int mat[10][10], int r, int c);
      int findDiagSum();
      
      
      void main()
      {
          int mat[10][10], r, c, i, j, ssum;
      
      
      
      
          ssum = findDiagSum();
          printf("The sum of the diagonal elements is &ssum");
      }
      
      
      void print_matrix(int mat[10][10], int r, int c)
      {
          int i, j;
          printf("\nEnter the rows and columns of the matrix: ");
          scanf("%d %d", &r, &c);
      
      
          printf("\nEnter the elements of the matrix: ");
          for(i=0; i<r; i++)
              for(j=0; j<c; j++)
                  scanf("%d", &mat[i][j]);
          printf("\nThe matrix is\n");
      }
      
      
      int findDiagSum()
      {
          int mat[10][10], r, c, i, j, sum=0;
      
      
          for(i=0; i<r; i++)
              for(j=0; j<c; j++)
              {
               if (i==j)
               sum = sum + mat[i][j];
              }
          return sum;
      }
      I am not sure how to call the void print_matrix function into the main function. If somebody could help me with this and also give me some insight to make sure my whole code looks good. Thanks for all your help in advance.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you call a function, you typically remove the return data type, as well as the data types from the parameters. They're already known, from the prototype you have.

    Just use the name of the array, and the name of the variables (for a pass by copy: r for example), or the address of the named variables (for a pass by reference: &r).

    Try that.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    23
    I'm sorry, I am not sure I understand still. If you could explain further or give me an example, it would be much appreciated.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <stdio.h>
    static void get_dimentions(int* width,int* height);
    static void fill_matrix(int mat[][10], int width, int height);
    
    int main(void )/* read FAQ */
    {
        int mat[10][10], width, height;
     
        get_dimentions( &width, &height);
     
        fill_matrix(mat,width, height);
     
        return 0;
    }
     
    static void get_dimentions(int* width,int* height)
    {
        printf("\nEnter the rows and columns of the matrix: ");
        fflush(stdout);
        while(scanf("%d %d", width, height) != 2)
        {
          /* read FAQ how to flush input buffer */
    
          printf("Wrong input - try again: ");
          fflush(stdout);
        }
        return;
    } 
    void fill_matrix(int mat[10][10], int r, int c)
    {
        int i, j;
     
        printf("\nEnter the elements of the matrix: ");
        for(i=0; i<r; i++)
            for(j=0; j<c; j++)
                scanf("%d", &mat[i][j]);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    23
    ok. I got the print matrix part of the program to work, but now the sum of the diagonal part is giving me some trouble, the numbers are not summing to what they should. If anybody could help with this, it would be much appreciated, thank you for all your help i advance.

    Code:
    int main()
    {
        int a, b, ssum;
    
    
        print_matrix(&a, &b);
        ssum = findDiagSum();
        printf("The sum of the diagonal elements is %d", ssum);
    
    
        return 0;
    }
    
    
    void print_matrix(int mat[10][10], int r, int c)
    {
        int i, j;
        printf("\nEnter the rows and columns of the matrix: ");
        scanf("%d %d", &r, &c);
    
    
        printf("\nEnter the elements of the matrix: ");
        for(i=0; i<r; i++)
            for(j=0; j<c; j++)
                scanf("%d", &mat[i][j]);
        printf("\nThe matrix is\n");
        for(i=0;i<r;i++)
          {
          printf("\n");
          for(j=0;j<r;j++)
          {
          printf("%d\t",mat[i][j]);
          }
          }
    }
    
    
    int findDiagSum()
    {
        int mat[10][10], r, c, i, j, sum=0;
    
    
        for(i=0; i<r; i++)
            for(j=0; j<c; j++)
            {
             if (i==j)
             sum = sum + mat[i][j];
            }
        return sum;
    }

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You should be passing a 2d array into your print matrix. Look in main and tell me what you are passing it. You also have a random 2d array in your findSum function, the matrix should be passed in from main.

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    23
    I just wanted to thank everyone for their help. I finally got it!

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    23
    little problem, if anyone could help me with this, it would be much appreciated.
    my program works for matrix size of 1-5, but when trying 6-10, it just crashes, any help would be much appreciated.

    Code:
    int main()
    {
        int a, b, ssum;
    
    
        print_matrix(&a, &b); // prints user input matrix by print_matrix function
        ssum = findDiagSum(&a, &b); // finds sum of diagonal elements by findDiagSum function
        printf("The sum of the diagonal elements is %d", ssum);
    
    
        return 0;
    }
    
    
    void print_matrix(int mat[10][10], int r, int c)
    {
        int i, j;
        printf("\nEnter the rows and columns of the matrix: ");
        scanf("%d %d", &r, &c); // user enters rows and colums of matrix
    
    
        printf("\nEnter the elements of the matrix: ");
        for(i=0; i<r; i++)
            for(j=0; j<c; j++)
                scanf("%d", &mat[i][j]); // user enters elemets of the matirx
        printf("\nThe matrix is\n");
        for(i=0;i<r;i++) // prints matrix with user input elements
          {
          printf("\n");
          for(j=0;j<r;j++)
          {
          printf("%d\t",mat[i][j]);
          }
          }
    }
    
    
    int findDiagSum(int mat[10][10], int r, int c)
    {
        int i, j, sum=0;
        for(i=0; i<r; i++)
            for(j=0; j<c; j++)
            {
             if (i==j) // finds diagonal elements of the matrix
             sum = sum + mat[i][j]; // diagonal elements are summed together
            }
        return sum; // returns the sum of the diagonal elements
    }

  9. #9
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Does it compile?

  10. #10
    Registered User
    Join Date
    Sep 2013
    Posts
    23
    yes, it does and the program also work for matrix sizes 1x1, 2x2, 3x3, 4x4, 5x5. any bigger than that it crashes.

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Take a look at this
    Code:
    samaras@samaras-A15:~$ gcc px.c -Wall -o px
    px.c: In function ‘main’:
    px.c:44:5: warning: passing argument 1 of ‘print_matrix’ from incompatible pointer type [enabled by default]
    px.c:4:6: note: expected ‘int (*)[10]’ but argument is of type ‘int *’
    px.c:44:5: warning: passing argument 2 of ‘print_matrix’ makes integer from pointer without a cast [enabled by default]
    px.c:4:6: note: expected ‘int’ but argument is of type ‘int *’
    px.c:44:5: error: too few arguments to function ‘print_matrix’
    px.c:4:6: note: declared here
    px.c:45:5: warning: passing argument 1 of ‘findDiagSum’ from incompatible pointer type [enabled by default]
    px.c:27:5: note: expected ‘int (*)[10]’ but argument is of type ‘int *’
    px.c:45:5: warning: passing argument 2 of ‘findDiagSum’ makes integer from pointer without a cast [enabled by default]
    px.c:27:5: note: expected ‘int’ but argument is of type ‘int *’
    px.c:45:5: error: too few arguments to function ‘findDiagSum’
    px.c:27:5: note: declared here
    Always use -Wall flag and eliminate everything the compiler says!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The functions you've got aren't consistent (in terms of parameters), so there's no way this is actually your code. Who knows what else you've changed between this version and that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print the matrix in diagonal order - C program
    By saran.geevee in forum C Programming
    Replies: 2
    Last Post: 05-11-2012, 06:02 AM
  2. Sum of the diagonal numbers of a matrix
    By pedrocosta03 in forum C Programming
    Replies: 3
    Last Post: 07-14-2011, 08:48 PM
  3. how to find a sub matrix..
    By transgalactic2 in forum C Programming
    Replies: 44
    Last Post: 12-26-2008, 08:03 PM
  4. how to find a sub matrix..
    By transgalactic2 in forum C Programming
    Replies: 1
    Last Post: 12-26-2008, 04:04 AM
  5. Diagonal Matrix
    By mlupo in forum C Programming
    Replies: 1
    Last Post: 12-04-2002, 10:02 PM