Can't figure out what's wrong

For class I have to create a program that fills a 2d array and gets the sum of each row and column and places them into two different arrays and displays the totals. This is the code I have but when I run it the numbers don't add up at all. I would appreciate any help. Thanks!

#include <stdlib.h>
#include <iomanip>
#include <iostream>

using namespace std;

void fill2Darray(int A[][5], int numRows);

void computeSales(int A[][5], int qtrlySum[], int numQtrs, int branchSum[], int numBranches);

int main()
{
const int r = 4;
const int c = 5;
int array[r][c];
int rowSum[r];
int colSum[c];

fill2Darray(array, r);

computeSales(array, rowSum, r, colSum, c);

return (0);
}


void fill2Darray(int A[][5], int numRows)
{
for (int r = 0; r < numRows; r++)
{
cout << endl << "Enter numbers by row:" << endl;
for (int c = 0; c < 5; c++)
cin >> A[r][c];
}

}

void computeSales(int A[][5], int qtrlySum[], int numQtrs, int branchSum[], int numBranches)
{
qtrlySum[numQtrs] = {0};

for (int r = 0; r < numQtrs; r++)
{
for (int c = 0; c < numBranches; c++)
qtrlySum[r] += A[r][c];

}

branchSum[5] = {0};
for (int c = 0; c < numBranches; c++)
{
for (int r = 0; r < numQtrs; r++)
branchSum[c] += A[r][c];
}

int total = 0;
for (int i = 0; i < numQtrs; i++)
{
total += qtrlySum[i];
}

int total2 = 0;
for (int i = 0; i < numBranches; i++)
{
total2 += branchSum[i];
}

for (int i = 0; i < numQtrs; i++)
{
cout << endl <<"Quarter-" << (i+1) << " total sales:\t" << qtrlySum[i];
}

for (int i = 0; i < numBranches; i++)
{
cout << endl << "Branch-" << (i+1) << " total sales:\t" << branchSum[i] << endl;
}

cout << endl << "Total for the year:\t" << (total + total2);



}

Last edited on
You need to initializate your arrays at the point of declaration.
int rowSum[r] = {0};

When you do qtrlySum[numQtrs] = {0}; inside the `computeSales()' function it is interpreted as access the element at index `numQtrs' and assign it 0. So you are setting just one element to 0 (and it is out of bounds)


Same for columns.
Thank you so much! Really appreciate the help!
#include <stdlib.h>
#include <iomanip>
#include <iostream>

int main()
{
int a[2][2],i,j,k=0;
int r[2]={0};
int c[2]={0};
printf("enter values in 2d array of 2*2\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
a[i][j]=i+j;
}
}


//print the values of array
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n");


//adding row and storin in r[2];
for (i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
k+=a[i][j];

}
r[i]=k;
k=0;
}

//print row.... r[2]
printf("row addition is...\n");
for(i=0;i<2;i++)
{
printf("%d\t",r[i]);

}
printf("\n");
k=0;

//adding coloumn and storing in c[2]
for(j=0;j<2;j++)
{
for (i=0;i<2;i++)
{
k+=a[i][j];

}
c[j]=k;
k=0;
}


//printing colm ...c[2]
printf("colm addition is...\n");
for(i=0;i<2;i++)
{
printf("%d\t",c[i]);

}
printf("\n");

//printing additio of c[2],r[2]...
k=0;
for(i=0;i<2;i++)
{
k+=(r[i]+c[i]);
}
printf(" rows and colmnaddition is...\n");
printf("%d",k);





while(1);
return 0;
}




i tried this.......see if it helps u... if.
#include <stdlib.h>
#include <iomanip>
#include <iostream>

using namespace std;

void fill2Darray(int A[][2], int numRows);

void computeSales(int A[][2], int qtrlySum[], int numQtrs, int branchSum[], int numBranches);

int main()
{
const int r = 2;
const int c = 2;
int array[r][c];
int rowSum[r];
int colSum[c];

fill2Darray(array, r);

computeSales(array, rowSum, r, colSum, c);
while(1);
return (0);
}


void fill2Darray(int A[][2], int numRows)
{
cout << endl << "Enter values in array:" << endl;
for (int r = 0; r < numRows; r++)
{
//
for (int c = 0; c < 2; c++)
cin >> A[r][c];
}

}

void computeSales(int A[][2], int qtrlySum[], int numQtrs, int branchSum[], int numBranches)
{
int a=0;//qtrlySum[numQtrs] = {0,0,0,0};

for (int r = 0; r < numQtrs; r++)
{
for (int c = 0; c < numBranches; c++)
{
a+= A[r][c];
}
qtrlySum[r]=a;

}


for (int c = 0; c < numBranches; c++)
{
int a=0;
for (int r = 0; r < numQtrs; r++)
{
a+= A[r][c];
}
branchSum[c]=a;

}


int total = 0;
for (int i = 0; i < numQtrs; i++)
{
total += qtrlySum[i];
}

int total2 = 0;
for (int i = 0; i < numBranches; i++)
{
total2 += branchSum[i];
}

for (int i = 0; i < numQtrs; i++)
{
cout << endl <<"Quarter-" << (i+1) << " total sales:\t" << qtrlySum[i];
}

for (int i = 0; i < numBranches; i++)
{
cout << endl << "Branch-" << (i+1) << " total sales:\t" << branchSum[i] << endl;
}

cout << endl << "Total for the year:\t" << (total + total2);



}


i think this can be helpful to u.......
Topic archived. No new replies allowed.