Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program using two-dimensional arrays that computes the sum of data in rows of the 3x3 (three by three) array variable n[3][3].
Sample input/output dialogue:
Enter numbers: 5 9 8 3 8 2 4 3 9
Output:
5   9   8 = 12
3   8   2 = 13
4   3   9 = 16


What I have tried:

How to add computes
C
#include<stdio.h>
 int main()
    { 
    /* 2D array declaration*/
     int disp[3][3];
     /*Counter variables for the loop*/ 
    int i, j; 
    for(i=0; i<3; i++)
     { 
        for(j=0;j<3;j++)
         { 
printf("Enter value for disp[%d][%d]:", i, j); 
       scanf("%d", &disp[i][j]);
             } 
        } 
    //Displaying array elements
     printf("Two Dimensional array elements:\n");
     for(i=0; i<3; i++)
     { 
        for(j=0;j<3;j++)
         {
             printf("%d ", disp[i][j]); 
            if(j==2)
                {
                 printf("\n");
                 } 
            } 
        } 
    return 0; 
    }
Posted
Updated 28-Mar-21 21:19pm
v2

Try
C
#include <stdio.h>
  
void input (int disp[][3]);
void show (int disp[][3]);

int main()
{
  int disp[3][3]; // the uninitialised matrix
  input(disp); // gather user inputs
  show(disp); // display the matrix and the row sums
  return 0;
}

void input ( int disp[][3] )
{
  for(int i=0; i<3; i++)
  {
    for(int j=0;j<3;j++)
    {
      printf("Enter value for disp[%d][%d]:", i, j);
      scanf("%d", &disp[i][j]);
    }
  }
}
void show( int disp[][3])
{

  printf("Two Dimensional array elements:\n");
  for(int i=0; i<3; i++)
  {
    int sum = 0;
    for(int j=0;j<3;j++)
    {
      sum += disp[i][j];
      printf("%d ", disp[i][j]);
    }
    printf(" = %d\n", sum);
  }
}
 
Share this answer
 
You have written most of the code correctly. Only sum is missing.

Will modify your code by adding 2 new lines and modify two lines. That's it... Problem will be solved.

All changes were in bold for your reference

C
//In the begining declare one more variable called "sum"
int i, j, sum;


//Taken your existing code and added one new line (sum = 0) and modified your printf

//Displaying array elements
     printf("Two Dimensional array elements:\n");
     for(i=0; i<3; i++)
     { 
        sum = 0;
        for(j=0;j<3;j++)
         {
             printf("%d ", disp[i][j]); 
             sum += disp[i][j];
             // Removed that (j == 2) condition
         }

          printf("= %d\n", sum);
        } 
    return 0; 
    }
}
return 0;
}
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900