Hello! i am beginner in C programming ..can any of you guys can run my program and tell me the error?

+3 votes
asked Jun 14 by A kan (220 points)
//write a program in  two dimensional array  to calculate the sum

#include <stdio.h>
#define Rsize 3
#define Csize 4
//it means matrix will be like a[2][3] mathematically
void main()
{
 //declaring variables
 int sum;

 int r; //rows
 int c; //colomns
 int a[Rsize][Csize ];// positions
 //two dimension array,i  am  using two  nested loops
 // lets use 1st nested loop for input

 for(r=1;r<Rsize;r++) //row  loop

 {
      printf("\n row number is %d",r);
for(c=1;c<Csize;c++)//colomns
    
     //taking input
     {printf("\nkindly Enter any number \n"); //input
     scanf("%d",&a[r][c]);

 } //end nested for
         }//end for
//displaying loop
   for(r=1;r<Rsize;r++)//telling loop about the rows

  {
      printf("\nrow no is %5d:",r);

      //telling loop about the coloums
      for(c=1;c<Csize;c++)
         {

     

     printf("%5d",a[r][c]); //num stored in positions(of array)

    sum+=a[r][c];
    printf("%5d",sum);

      } //   inner for ends

  }

}

2 Answers

–1 vote
answered Jun 14 by gameforcer (2,950 points)
You forgot 1 brace at the end.
commented Jun 15 by Peter Minarik (7,160 points)
No, s/he did not. The problems with the code are not syntax based.
commented Jun 20 by gameforcer (2,950 points)
Oh, you're right, I copied the code wrongly.
+2 votes
answered Jun 15 by Peter Minarik (7,160 points)
edited Jun 17 by Peter Minarik

First of all,

//it means matrix will be like a[2][3] mathematically

is not true. It will generate a 3 x 4 matrix (not a 2 x 3), that is, it has 3 rows and 4 columns. The major difference is that in mathematics you index rows and columns starting from 1 (a.k.a. 1-based indexing), while in the majority of the programming languages (including C/C++) you index from 0 (a.k.a. 0-based indexing).

Therefore, your loops

for(r=1;r<Rsize;r++)
for(c=1;c<Csize;c++)

Should not start from 1, but 0.

Also, you print the sum in every loop iteration per row & column. Probably you should do it only once per row and one in the end, for the sum of the whole matrix (array).

The next thing, a bit subtle, is overflowing. If you add two int types together, the result may be larger than what could fit into an int. So it's worth defining the sum not as int, but long long int. (Note: int in a 64 bit system usually means long int, so the larger storage type hence is long long int)

A few more notes:

  • in the latest C version, you can declare your loop variables in the for loop. No need to extend the scope (visibility) of your variables beyond they actual usage.

  • Macros can be dangerous or annoying, especially in large projects. It's wiser to use constants instead.

So with all of these changes (and a few bits here and there), the code would look something like this:

// Write a program in  two dimensional array  to calculate the sum

#include <stdio.h>

void main()
{
    const int rowSize = 3;
    const int columnSize = 4;
    long long int sum = 0;
    int values[rowSize][columnSize];

    // Reading data:   
    for (int r = 0; r < rowSize; r++)
    {
        for (int c = 0; c < columnSize; c++)
        {
            printf("\nEnter any number for (%d, %d): ", r, c);
            scanf("%d", &values[r][c]);
        }
    }

    // Calulating the sum and printing the array:
    for (int r = 0; r < rowSize; r++)
    {
        long long rowSum = 0;
        printf("\nRow #%d: [", r);
        for (int c = 0; c < columnSize; c++)
        {
            if (c != 0)
                printf(", "); // Separate the values, but we only need this after the first item (not after the 0th item)
            printf("%d", values[r][c]);
            rowSum += values[r][c];
        }
        printf("] Sum of this row: %lli", rowSum); // lli --> long long int
        sum += rowSum;
    }
    printf("\nSum of the whole matrix: %lli", sum); // lli --> long long int
}

Sure, there could be added even more polishing to this (e.g. checking if the input is valid or using unsigned int for the type of the loop variables as they can never be negative), but I leave you to do these, if you want to play around more with this example.

Good luck. :)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...