Using classes and a .txt find the sum of the rows and columns The txt file looks
ID: 3548538 • Letter: U
Question
Using classes and a .txt find the sum of the rows and columns
The txt file looks like this
100 101 102 103 104 105
106 107 108 109 110 111
112 113 114 115 116 117
118 119 120 121 122 123
124 125 126 127 128 131
Class Specifications
template<class T>
class Prob3Table
{
protected:
int rows; //Number of rows in the table
int cols; //Number of cols in the table
T *rowSum; //RowSum array
T *colSum; //ColSum array
T *table; //Table array
T grandTotal; //Grand total
void calcTable(void); //Calculate all the sums
public:
Prob3Table(char *,int,int); //Constructor then Destructor
~Prob3Table(){delete [] table;delete [] rowSum;delete [] colSum;};
const T *getTable(void){return table;};
const T *getRowSum(void){return rowSum;};
const T *getColSum(void){return colSum;};
T getGrandTotal(void){return grandTotal;};
};
template<class T>
class Prob3TableInherited:public Prob3Table<T>
{
protected:
T *augTable; //Augmented Table with sums
public:
Prob3TableInherited(char *,int,int); //Constructor
~Prob3TableInherited(){delete [] augTable;}; //Destructor
const T *getAugTable(void){return augTable;};
};
Driver code
cout<<"Entering problem number 3"<<endl;
int rows=5;
int cols=6;
Prob3TableInherited<int> tab("Problem3.txt",rows,cols);
const int *naugT=tab.getTable();
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
cout<<naugT[i*cols+j]<<" ";
}
cout<<endl;
}
cout<<endl;
const int *augT=tab.getAugTable();
for(int i=0;i<=rows;i++)
{
for(int j=0;j<=cols;j++)
{
cout<<augT[i*(cols+1)+j]<<" ";
}
cout<<endl;
}
Example Input Table
100 101 102 103 104 105
106 107 108 109 110 111
112 113 114 115 116 117
118 119 120 121 122 123
124 125 126 127 128 131
Example Output Table with rows summed,
columns summed and the grand total printed.
100 101 102 103 104 105 615
106 107 108 109 110 111 651
112 113 114 115 116 117 687
118 119 120 121 122 123 723
124 125 126 127 128 131 761
560 565 570 575 580 587 3437
Explanation / Answer
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void call(int array[5][6], const int i){
int colRowSum[6] = {0};
int colSumRow[5] = {0};
//sum to column row
for(int i = 0; i < 5; i++){
for( int j = 0; j < 6; j++){
colRowSum[j] += array[i][j];
colSumRow[i] += array[i][j];
}
}
//print array
for(int i = 0; i != 5; ++i){
for(int j = 0; j != 6; ++j){
cout.width(10);
cout << array[i][j] << " ";
}
cout.width(10);
cout << colSumRow[i];
cout << endl;
}
//print result
for(int n = 0; n != 6; ++n){
cout.width(10);
cout << colRowSum[n] << " ";
}
for (int s = 0; s != 5; ++s){
cout.width(10);
cout <<colSumRow[s]<<"";
}
cout << endl;
}
int main(){
int array[ 5 ][ 6 ] = { { 141, 567, 434, 100, 269, 324, },
{ 578, 458, 562, 564, 305, 245, },
{ 381, 427, 561, 591, 595, 542, },
{ 427, 536, 491, 204, 502, 253, },
{ 392, 482, 521, 316, 318, 495, } };
call (array,5);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.