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!
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);
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)
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];
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);