Thread: Sum of elements of row/column of a matrix

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    44

    Sum of elements of row/column of a matrix

    Hello, I have the following problem:
    1.Write a program which gets into an array elements of matrix A sized as n and k, but size of matrix must agree with n, k < 20. The program also must:


    a)calculate and show sum of elements of each row of a matrix


    b)calculate and show sum of elements of each column of a matrix


    My code is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    
        int k, n, i, j;
    
        do {
            printf("Specify the size of the matrix (nxk)\n");
            scanf("%dx%d", &n, &k);
        } while(n>=20||n<0||k>=20||k<0);
    
        int tab[n][k];
    
        for (i=0; i<n; i++) {
            for (j=0; j<k; j++) {
                printf("Specify the element (%d,%d)", i, j);
                scanf("%d", &tab[i][j]);
            }
        }
        int sum_row[n], sum_column[k];
        for (i=0; i<n; i++) {
            for (j=0; j<k; j++) {
                sum_row[i]+=tab[i][j];
                sum_column[j]+=tab[i][j];
            }
        }
    
        for (i=0; i<n; i++) {
            for (j=0; j<k; j++) printf("%d  ", tab[i][j]);
            printf("\n");
        }
        for (i=0; i<n; i++) printf("sum of row %d equals: %d\n", i, sum_row[i]);
        for (j=0; j<k; j++) printf("sum of column %d equals: %d\n", j, sum_column[j]);
    
        system("PAUSE");
        return 0;
    However it doesn't calculate the sum properly, just some random, big numbers.
    What is done wrong?

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you receiving no errors or warnings from your compiler?
    Last edited by Adak; 12-29-2011 at 05:11 AM.

  3. #3
    Registered User Ali3nSVK's Avatar
    Join Date
    Nov 2011
    Posts
    6
    Code:
    intsum_row[n], sum_column[k];
    Make sure you first initialise these arrays to zeros, since uninitialised, they contain random values and you use:

    Code:
    sum_row[i]+=tab[i][j];
    sum_column[j]+=tab[i][j];

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    44
    Quote Originally Posted by Ali3nSVK View Post
    Make sure you first initialise these arrays to zeros, since uninitialised, they contain random values and you use:
    Any hints how to do this?
    Sorry I'm a complete newbie
    As you can imagine, I only depend on copied code

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Use a loop similar to the ones you use to print out sum_row and sum_column, but instead of a printf with the element, set the element to zero. Also, your variable names are not very readable. Something like rowSize, colSize, row, and col would be better.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > As you can imagine, I only depend on copied code
    And that is why nobody cares whether you pass or fail.

    You can't learn this stuff by osmosis you know, you have to continually try (and fail) at every little thing until you succeed.
    You can be pretty damn sure that all the experts here got to where they are through effort and determination - not sitting around saying "gimmetehcodez".

    >> Make sure you first initialise these arrays to zeros, since uninitialised, they contain random values and you use:
    > Any hints how to do this?
    Yes, you go back to your C book, you look up "initialisation" in the index and you start reading.
    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.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Pole View Post
    Any hints how to do this?
    Sorry I'm a complete newbie
    As you can imagine, I only depend on copied code
    Code:
    int array[20] = {0};

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    44
    Hi again, spent all that time on reading.
    I have created brand new code, however I want to add false data rejection, eg. if sb enters char/string because now the program only checks if the int is proper.
    How to do that?
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    int main()
    {
        int n,i,j,k;
        float S;
    
    do
    {printf("How many rows?\n");
    scanf("%d", &n);
    }while(n<=0);
    
    do
    {printf("How many columns?\n");
    scanf("%d", &k);
    }while(k<=0);
    
    printf("\n");
    
    float tab[n][k];
    
    for(j=0; j<n; j++)
    {
    for(i=0; i<k; i++)
    {printf("Element [%d][%d]: ", j+1,i+1);
    scanf("%f", &tab[j][i]);
    }
    }
    
    printf("\n");
    
    for(j=0; j<n; j++)
    {
    S=0;
    for(i=0; i<k; i++)
    {
    S+=tab[j][i];
    }
    printf("The sum of the elements in row %d: %f\n", j+1, S);
    }
    
    printf("\n");
    
    for(i=0; i<k; i++)
    {
    S=0;
    for(j=0; j<n; j++)
    {
    S+=tab[j][i];
    }
    printf("The sum of the elements in column  %d: %f\n", i+1, S);
    }
    
    printf("\n");
    
    system("PAUSE");
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    44
    I've invented a way to do this, however this loop doesn't return to beginning if the condition is false
    How to do that?
    Code:
    printf("How many rows?\n");
    
    if (scanf("%d", &n) != 1)
    printf("You entered wrong number\n");
    else if (n>=20||n<0)
    printf("You entered wrong number\n");
    else {
    printf("You entered correct number\n");}
    
    do
    {printf("How many columns?\n");
    scanf("%d", &k);
    }while(k>=20||k<0);

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... now you need to add 1 more very important feature...
    Proper code indentation.

    Yes it's a bit more typing but when properly indented, your code is going to be a whole lot easier to follow and debug.

    Indent style - Wikipedia, the free encyclopedia

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    44
    Ok, I'd do the indentation after it is finished.
    Thanks for advice !
    But how to make the loop from my previous post return to the beginning if inputted letter/number is not correct

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Pole View Post
    As you can imagine, I only depend on copied code
    And how's that working out for you?

    Seriously... you won't ever learn programming or C if you are copying other people's code.

    Just look where you are... You are on a forum begging help with simple code you don't even understand well enough to indent properly... What's that tell you about your progress in learning C?
    Last edited by CommonTater; 12-31-2011 at 11:49 AM.

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Pole View Post
    Ok, I'd do the indentation after it is finished.
    Indentation is not just to make it look good for others, it also helps you to clarify your code structure to yourself. It's important to indent properly all the time.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Pole View Post
    Ok, I'd do the indentation after it is finished.
    Person A: What are you doing?
    Person B: I'm waxing the car.
    Person A: But that rag you're using is totally covered in dog poop!
    Person B: I know, I'll clean the rag when I'm finished waxing the car.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by oogabooga View Post
    Indentation is not just to make it look good for others, it also helps you to clarify your code structure to yourself. It's important to indent properly all the time.
    ... As you enter your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change column in matrix
    By plasticstone in forum C Programming
    Replies: 2
    Last Post: 10-22-2011, 10:19 AM
  2. calculate column-wise sum of a matrix in C
    By Raymond2010 in forum C Programming
    Replies: 4
    Last Post: 06-21-2011, 11:21 PM
  3. understanding projection matrix elements
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 07-26-2009, 03:35 PM
  4. sort elements of 2D matrix by 1st column reference
    By cfdprogrammer in forum C Programming
    Replies: 12
    Last Post: 05-04-2009, 03:26 PM
  5. column-major matrix Q....
    By pxleyes in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 08:26 PM