Solved

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

Posted on 1998-04-13
159 Views
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 ;
}
0
Question by:habibi

LVL 10

Accepted Solution

RONSLOW earned 200 total points
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

Author Comment

Thank you very very much .
0

Featured Post

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.