Question: Write a program so that computes and prints the sum of each column of the array. This problem is harder than the previous because some

Write a program so that computes and prints the sum of each column of the array.
This problem is harder than the previous because some rows have more columns than others. You can't just reverse the inner and outer loops of the previous program because this will try to access columns that don't exist in some rows. One solution is to create a second 1D array that holds the column sums. Make a first pass over the data array to determine how long this array of sums should be. Iterate over the array as before adding each array element to the appropriate sum as it is encountered.
TURN IN:
Solve this problem without using a second array -- the logic might be tricky, unless you think in through.
For this assignment, do not use a 2nd array (e.g., solve 'in place').
Note: You will still need a first pass over the array.
Use the following Array for testing:
int [][] data ={{12,24,48,96,128},
{-4,34,22,-10,-256,12},
{14,-64,-88,144},
{16},
{32,64,128,-256}};
NOTES:
1. Don't let the title scare you :).
2. This problem has 'ragged' 1D arrays (different lengths of each row).
3. You must check each row's array length and ensure you do not allow indices to stray out of bounds!
4. If a corresponding array value in a column does not exist, mathematically it is zero, or 'skip' the additions (depending on how you choose to solve the problem).
5. The program must work for any array input, e.g., I may use an alternative 2D array to see how your program handles it.
6. The AP exam in May will not require use of ragged arrays in FRQ, nor give such MCQ problems.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!