Matrices

+1 Alex Carter · April 13, 2015
Hey, guys, I am beginner and I know this isn't big problem, I have an idea in my head, but I can't make a cod because of my stupid teachers in school..So, a program needs to write a matrice and at the and of every row to show the sum of that row, nothing else.
It's a piece of cake, I know :D

Post a Reply

Replies

Oldest  Newest  Rating
0 http://coding.developer.se/   · April 16, 2015
Well, that looks positively cromulent. Why not ask for row and col before declaring matrix like: int matrix
?

Also, don't forget to check the return value of scanf.


#include 

int main(void) {
size_t row, column;

puts("Enter row count.");
if (scanf("%zu", &row) != 1) {
puts("Invalid row count.");
exit(0);
}

puts("Enter column count.");
if (scanf("%zu", &column) != 1) {
puts("Invalid column count.");
exit(0);
}

int matrix[row][column];
/* ... */
}
0 Delagre Delagre · April 15, 2015
Hello Alex, take a look at the next code:


#include

#define MAX 10

int main() {
    int row, col, i, j, matrix[MAX][MAX], endRow = 0, sumRows = 0, sumCols = 0;
    printf("How much rows: \n");
    scanf("%d", &row);
    printf("How much cols \n");
    scanf("%d", &col);
    
    // Enter numbers for your matrix
    
    printf("Enter numbers for your matrix\n");
    for(i = 0; i < row; i++) {
        for(j = 0; j < col; j++) {
            printf("Enter number for [%d][%d] \n", i, j);
            scanf("%d", &matrix[j]);
        }
    }
    
    // Display your matrix
    printf("Your matrix is: \n");
    for(i = 0; i < row; i++) {
        for(j = 0; j < col; j++) {
            printf("%d\t", matrix[j]);
        }
        printf("\n");
    }
    
    printf("\n");
    // Display sum of row in the end of the row
    
    for(i = 0; i < row; i++) {
        for(j = 0; j < col; j++) {
            endRow += matrix[j];
            printf("%d\t", matrix[j]);
        }
        printf("Sum of row #%d is: %d\n", i+1, endRow);
        endRow = 0; // Reset sum becasue we use 'endRow' in for loop
    }
    
    // Let`s calculate total sum of rows and cols for your matrix
    for(i = 0; i < row; i++) {
        for(j = 0; j < col; j++) {
            sumCols += matrix[j];
        }
    }
    
    sumRows = sumCols; // Just initialize sumRows with sumCols
    
    // Display sum of rows
    
    printf("Total sum of matrix rows are: %d\n", sumRows);
    
    // Display sum of cols
    
    printf("Total sum of matrix cols are: %d\n", sumCols);
    
    // This is end. Good luck!
}
0 Clark Alaan · April 14, 2015
That wasn't really my intention... but yea you're probably right to some degree.
I'm a novice myself so I found this exercise fairly entertaining even if it was quite simple. I never do enough math-related programs after all.
But yea, probably won't do this again in the future. It really doesn't help anybody if we assume that everyone is trying to learn on this site.
0 http://coding.developer.se/   · April 14, 2015
Ok, now you have finished my first assignment for me, please do the rest of them so that I can get a job using your credentials, potentially steal your job and money... if only for a month or so before the employer realises that I'm absolute garbage and fires me. Thanks in advance, forum community, for rendering my expensive diploma in IT useless garbage. Now, please do my homework for me...
0 Clark Alaan · April 13, 2015
Are you asking for a source code or are you asking us to teach you?

Edit:
If you're only looking for code then here: http://pastebin.com/E6ZUwQ2H

You can probably learn by studying it too.

Hope this helps.
  • 1

C

107,005 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator