0
#include <stdio.h>

int main(int argc, char const *argv[])
{
    int n,m,t[n][m];

    printf("\nEnter the dimensions of the array N x M: ");
    scanf("%d %d",&n,&m);

    printf("\nScanning the array:\n");
    
    for (int i = 0;i<n;i++) {
        for (int j = 0;j<n;j++) {
            scanf("%d",&t[i][j]);
        }
    }

    int sl;

    for (int k=0;k<n;k=k+1) {
        sl = 0;
        for (int p=0;p<m;p++){
            sl = sl + t[k][p];
        }
        printf("\nThe sum of the line number %d is %d",k+1,sl);
    }
    return 0; 
}

and this is the out put of few tests :

#1:

Enter the dimensions of the array N x M: 3 3

Scanning the array:

5 6 7
8 9 10
1 1 1

The sum of the line number 1 is 3
The sum of the line number 2 is 3
The sum of the line number 3 is 3

#2 :

Enter the dimensions of the array N x M: 4 4

Scanning the array:

0 0 0 0
0 1 1 0
1 0 0 1
0 1 0 1

The sum of the line number 1 is 2
The sum of the line number 2 is 2
The sum of the line number 3 is 2
The sum of the line number 4 is 2

4
  • int n,m,t[n][m]; you can't do that in C, you need to allocate the matrix dynamically Commented May 28, 2021 at 20:54
  • "but the result was a bit strange" - ok? Talking to a future fellow programmer - What would your reaction be with that description? Commented May 28, 2021 at 20:55
  • @TedLyngmo i don't think that i will be a programmer in the future because i study mechanical engineering :) Commented May 28, 2021 at 20:58
  • @ColtonWalker Welcome to the dark side :-) Commented May 28, 2021 at 21:12

2 Answers 2

2
#include <stdio.h>

int main(int argc, char const *argv[])
{
    int n,m; // the variables n and m aren't assigned yet so you cant create array with their dimensions

    printf("\nEnter the dimensions of the array N x M: ");
    scanf("%d %d", &n, &m);

    int t[n][m]; // create the array here

    printf("\nScanning the array:\n");
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) { // here you had n instead of m
            scanf("%d", &t[i][j]);
        }
    }

    int sl;

    for (int k = 0; k < n; k++) { // you can use just k++
        sl = 0;
        for (int p = 0; p < m; p++){
            sl = sl + t[k][p];
        }
        printf("\nThe sum of the line number %d is %d", k + 1, sl);
    }
    return 0; 
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a working example of what you were trying to do, I simply modified your code to take into account dynamic allocation (note the #include <stdlib.h> at the top)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int n, m, **t;

    printf("\nEnter the dimensions of the array N x M: ");
    scanf("%d %d",&n,&m);

    t = (int**) malloc(n*sizeof(int*));     // allocating an array of pointers to int
    for (int i=0; i<n; i++){
      t[i] = (int*) malloc(m*sizeof(int));  // allocating an array of integers
    }

    printf("\nScanning the array:\n");
    
    for (int i = 0; i<n; i++) {
        for (int j = 0; j<m; j++) {
            scanf("%d", &t[i][j]);
        }
    }

    int sl;

    for (int k=0; k<n; k++) {
        sl = 0;
        for (int p=0; p<m; p++){
            sl = sl + t[k][p];
        }
        printf("\nThe sum of the line number %d is %d",k+1,sl);
    }

    for (int i=0; i<n; i++){
      free(t[i]); // de-allocating the array of integers
    }
    free(t);  // de-allocating the array of pointers to integer
    return 0; 
}

While it is true that you might not be a programmer in the future, please try to understand what's going on in this code, and if you need clarifications feel free to ask c:

2 Comments

Ah thank you very much, that would absolutely help me learn better way to define an array, i'm new to C programming ;)
I hadn't been using C in a while and I honestly didn't remember that you could initialize arrays using variables inside square brackets like in Bonny's answer, sooo I went with the safe answer using dynamic allocation that is a very powerful knowledge to have and master. For example, using dynamic (re)allocation you could then resize your array to fit more (or less) data than before, without the need to define another array variable, but anyway that's out of the scope of your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.