Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function called column_summer that flexibly works with 2-D arrays. Its p

ID: 3547992 • Letter: W

Question

Write a function called column_summer that flexibly works with 2-D arrays. Its purpose is to sum any desired column in a passed array.

Four parameters are passed to the function:

1.       The address of a two dimensional array of double values (the array should be completely filled with values).

2.       An integer indicating the number of rows in the passed array.

3.       An integer indicating the number of columns in the passed array.

4.       An integer indicating the subscript of the column in the passed array to sum.

The function should use pointer math to calculate and return the sum of the desired column in the passed array (i.e. you should use pointers with an offset to perform the summing operation in the function).

The function prototype is:

double column_summer (const double *xptr, int num_rows, int num_cols, int col_to_sum);

For example, if y is a 5 by 6 array of double values defined and filled in the main function, then the statement in main:

cout<<

Explanation / Answer

Did in C++ since no language was mentioned


#include <iostream>

using namespace std;
double column_summer (const double *xptr, int num_rows, int num_cols, int col_to_sum);

int main()
{
    double y[5][6] = {1,2,3,4,5,6,
                      2,3,4,5,6,7,
                      3,4,5,6,7,8,
                      4,5,6,7,8,9,
                      5,6,7,8,9,10};
    double x[2][4] = {1.1, 2.2, 0.7, 0.2,
                      1, 3.1, 12.5,0.5};
    cout << "The sum of the column with subscript 3 in the y array is " << column_summer(&y[0][0], 5, 6, 2) << endl;
    cout << "The sum of the column with subscript 4 in the y array is " << column_summer(&y[0][0], 5, 6, 4) << endl;
    cout << "The sum of the column with subscript 0 in the x array is " << column_summer(&x[0][0], 2, 4, 0) << endl;
    cout << "The sum of the column with subscript 3 in the x array is " << column_summer(&x[0][0], 2, 4, 3) << endl;
    system("pause"); //Works on some OS, remove if causing errors
    return 0;
}

double column_summer (const double *xptr, int num_rows, int num_cols, int col_to_sum){
       double sum = 0;
       for(int i = 0; i < num_rows; i++){
       sum += *(xptr+col_to_sum+(i*num_cols));
       }
       return sum;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote