• C

# the question is easy (but I don't know what is wrong);please hurry.

I am doing now a spreadsheet using two dimentional arrays I finished it but I can't seem to be able to add totals
for every row and coloum (like this):

1    0    3    4      t : 8

2    7    9    0      t : 18

4    6     4    1     t : 15

and the same for every coloumn . < I have been trying to do it for days but it won't happen> this is the code for the spreadsheet :

#include <math.h>
#include <stdio.h>
#define maxrows 20
#define maxcols  30
#define null 0
FILE *file_pointer;
char filename[]="data.text";
main()
{
int n,m,col,row,r,c,l,x,o,j;
float a[maxrows][maxcols],y ;
printf("how many rows do you want ?");
scanf("%d",&m);
printf("how many cols do you want ?");
scanf("%d",&n);
for (row=0;row<m;++row)
for(col=0;col<n;++col)
a[row][col] = 0.0;
for (row=0;row<m;++row)
{
printf(" \n \n");
for(col=0;col<n;++col)
printf("  %f  ",a[row][col]);
}
x=1 ;
while (x!=2)
{
scanf("%d",&l);
if(l<6){
if(l==5)break ;
printf("\n select the row of the cell : \n");
scanf("%d",&r);
printf("\n select the col. of the cell : \n");
scanf("%d",&c);
printf("\n select the amount you want to use : \n");
scanf("%f",&y);
c=c-1;
r=r-1;
if(l==1)
{
a[r][c]=y+a[r][c] ;
}
if(l==2){
a[r][c]=a[r][c]-y; }
if(l==3)
{a[r][c]=a[r][c]*y;}
if(l==4)
{a[r][c]=a[r][c]/y;}
}
else{
if(l==6){
file_pointer = fopen("data.text","w");
for( row=0; row< m; row++)
for( col=0; col< n; col++)
fprintf(file_pointer,"%f ", a[row][col]);
fclose(file_pointer);
}
if(l==7)
{
file_pointer=fopen("data.text","r");
for( row=0; row<m ; row++)
for( col=0; col< n; col++)
fscanf(file_pointer,"%f", &a[row][col]);
fclose( file_pointer);
}
}
for (row=0;row<m;++row)
{
printf(" \n \n");
for(col=0;col<n;++col)
printf("  %f ",a[row][col]);
}
}
return 0 ;
}
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Just before you print out the array...

for(row=0;row<m;++row) {
a[row][n] = 0.0;
for(col=0;col<n;++col) {
a[row][n] += a[row][col];
}
}
for(col=0;col<n;++col) {
a[m][col] = 0.0;
for(row=0;row<m;++row) {
a[m][col] += a[row][col];
}
}

And change the print loop to be for (row=0;row<=m;++row) etc so that you print the total row/cols as well

You don't say if the total row/cols need to be read/written .. that's up to you.

0

Experts Exchange Solution brought to you by