Click here to Skip to main content
15,868,104 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
I am new in C programming. Pls help me solve this. my coding have alots of error.
the question:

Write a program to calculate the sum of elements in two tables of integers. Your program should receive input values from the keyboard and print all the values in each row and column and finally display a table of the summation value.

OUTPUT
How many rows? 2
How many columns? 3
First table:
Enter data for row no. 1
1 2 3
Enter data for row no. 2
4 5 6
Second table:
Enter data for row no. 1
7 8 9
Enter data for row no. 2
10 11 12
Sums of the elements:
8 10 12
14 16 18

My Coding:
C#
#include<stdio.h>

void printArray( const int a[][] );

int main(void)
...{
int rows,column;
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r;

printf("How many rows?:\n");
scanf("%d",&rows);
printf("How many columns?\n");
scanf("%d",&column);

printf("First table: \n");
printf("Enter data for row no.1:\n");
scanf("%d,d,d",&a,b,c);
printf("Enter data for row no.2:\n");
scanf("%d,d,d",&d,e,f);

printf("Second table: \n");
printf("Enter data for row no.1:\n");
scanf("%d,d,d",&g,h,i);
printf("Enter data for row no.2:\n");
scanf("%d,d,d",&j,k,l);

m=a+g
n=b+h
o=c+i
p=d+j
q=e+k
r=f+l

int array1[ rows ][ column ] = { a,b,c,d,e,f };
int array2[ rows ][ column ] = { g,h,i,j,k,l };
int arraysum[ rows ][ column ] = { m,n,o,p,q,r, };

printf("Sum of the elements: \n");
printf("m,n,o"\n);
printf("p,q,r\n");

}
Posted

First off, you need to learn about loops. They are probably in your lecture notes, but here is a brief recap:
1) Declare a variable to control the loop
int i;
2) Set up a loop:
for (i = 0; i < rows; i++)
This says: "i" starts at zero. Go around the loop all the time while "i" is less than the variable "rows", adding one to "i" each time you have been round the loop.
That means that if rows is (say) 3; it will go round the loop 3 times: when is zero, when i is one, and when i is two.
3) Put some code in the loop body:
{
//Code here
}
This is the code that will be executed each time it goes round the loop.
Now, you can get a row of information at a time from the user:
printf("How many rows?:\n");
scanf("%d",&rows);
printf("How many columns?\n");
scanf("%d",&columns);
int arrayOfData[100][100];
int x, y, currentValue;
for (y = 0; y < rows; y++)
   {
   printf("Enter data for row %d:\n", y);
   for (x = 0; x < columns; x++)
      {
      printf("   Enter value of column %d:", x + 1);
      scanf("%d", ¤tValue);
      arrayOfData[x][y] = currentValue;
      }
   }
Once you have got the hang of this, you can look at using loops to sum your rows, and so forth.

Disclaimer: This code is untested, uncompiled, and may give errors - I don't have a C compiler to hand...

(And for the purists, yes, they are magic numbers, no there are no range checks: He's just getting started, we can cover that later... He has much bigger things to worry about at the moment!)
 
Share this answer
 
This is not a good programming.
What you would do if i enter 10 rows and 10 columns, are you going to define 100 variables? and then how are you planning to handle those 100 variables?
Use arrays instead for storing values.
 
Share this answer
 
Comments
Nitty2011 21-Apr-11 0:06am    
Good!
One more thing - these lines :

C++
scanf("%d,d,d",&a,b,c);


should be :

C++
scanf("%d,%d,%d",&a,&b,&c);


Note the "%" on each format token and the "&" on each variable address.
 
Share this answer
 

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