My teacher wants us to modify the code we already had. This is the modification:
ID: 3530520 • Letter: M
Question
My teacher wants us to modify the code we already had. This is the modification: The 2-dimensional array SHOULD be read from a file: name the file 09.txt, the arrays maximum size is 20x20. Example of contents of such a file: 5 6 7 8 1 22 3 9 8 7 1 13 I'm not so sure how to modify my code into what she wants. Please assist. #include #include #define NUM_ROWS 20 #define NUM_COLS 20 using namespace std; //Functions int getTotal(int[] [NUM_COLS]); double getAverage(int); int getRowTotal(int[] [NUM_COLS], int); int getColumnTotal(int[] [NUM_COLS], int); double getAverage(int); int getHighestInRow(int[] [NUM_COLS], int); int getLowestInRow(int[] [NUM_COLS], int); int main() { int i, j, value, ch; int num[NUM_ROWS] [NUM_COLS]={10, 20, 30, 40}; cout<<"The Matrix Elements are: "<<endl; for(i=0;i<<num[i][j]<<" "<<endl; } for(;;) { cout<<"Your choices are: "<<endl; cout<<"1 get Total: "; cout<<"2 get Average: "; cout<<"3 get Row Total: "; cout<<"4 get Column Total: "; cout<<"5 get Highest Value In a Row: "; cout<<"6 get the Lowest Value In a Row: "; cout<<"7 get Exit: "; cout<<"Enter your choice: "; cin>>ch; switch(ch) { case 1: cout<<"Total of all values= " <<getTotal(num)<<endl; break; case 2: value=getTotal(num); cout<<"The Average= "<<getAverage(value)<<endl; break; case 3: cout<<"Enter row number: "; cin>>i; if(i<0||i>NUM_ROWS) { cout<<"Error: Must be between 0 and "<<NUM_ROWS <<"Please try again: "; break; } else cout<<"Row "<<i<<" total value=" <<getRowTotal(num,i)<<endl; break; case 4: cout<<"Enter column number: "; cin>>i; if(i<0||i>NUM_COLS) { cout<<"Error-Must be between 0 and" <<NUM_COLS<<" try again. "; break; } else cout<<"Column "<<i<<" total value= " <<getColumnTotal(num,i)<<endl; break; case 5: cout<<"Enter row number: "; cin>>i; if(i<0||i>NUM_ROWS) { cout<<"Error-Must be between 0 and" <<NUM_ROWS<<" try again. "; break; } else cout<<"Highest element in row "<<i<<"= " <<getHighestInRow(num, i)<<endl; break; case 6: cout<<"Enter row number: "; cin>>i; if(i<0||i>NUM_ROWS) { cout<<"Error-Must be between 0 and" <<NUM_ROWS<<" try again. "; break; } else cout<<"Lowest element in row "<<i<<"= " <<getLowestInRow(num, i)<<endl; break; case 7: system("pause"); return 0; default: cout<<"Error! Please Try Again "; } } } //function definitions //this function returns the total of all values in array int getTotal(int row[][NUM_COLS]) { int i, j, value=0; for(i=0;ivalue) value=num[n][i]; return value; } //this function returns lowest value in a specific row int getLowestInRow(int num[] [NUM_COLS], int n) { int i, value=0; value=num[n][0]; for(i=1;ivalue) value=num[n][i]; return value; }Explanation / Answer
//debugged to run
#include <iostream>
#include<cstdlib>
#include <fstream>
#define NUM_ROWS 20
#define NUM_COLS 20
using namespace std;
//Functions
int getTotal(int[] [NUM_COLS],int,int);
double getAverage(int,int,int);
int getRowTotal(int[] [NUM_COLS], int,int);
int getColumnTotal(int[] [NUM_COLS], int,int);
double getAverage(int,int,int);
int getHighestInRow(int[] [NUM_COLS], int,int);
int getLowestInRow(int[] [NUM_COLS], int,int);
int main()
{
int i, j, value, ch;
int num[NUM_ROWS] [NUM_COLS];
int rows, cols;//the actual dimensions used
ifstream infile("file 07_16.txt"); //create input stream from file
if(!infile){//exit if file failed to open
cout<<"Input file failed to open... ";
exit(1);
}
infile>>rows>>cols;//let the first two input dictate the size of expected array
//make sure rows and cols do not exceed NUM_ROWS and NUM_COL respectively
if(rows>NUM_ROWS){rows=NUM_ROWS;}
if(cols>NUM_COLS){cols=NUM_COLS;}
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
infile>>num[i][j];
if(infile.fail())//of infile fails,
num[i][j]=0; //set element to 0
}
}
cout<<"The Matrix Elements are: "<<endl;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++)
cout<<num[i][j]<<" ";
cout<<endl;
}
while(true){
cout<<"Your choices are: "<<endl;
cout<<"1 get Total ";
cout<<"2 get Average ";
cout<<"3 get Row Total ";
cout<<"4 get Column Total ";
cout<<"5 get Highest Value In a Row ";
cout<<"6 get the Lowest Value In a Row ";
cout<<"7 Exit ";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch){
case 1:
cout<<"Total of all values= "<<getTotal(num,rows,cols)<<endl;
break;
case 2:
value=getTotal(num,rows,cols);
cout<<"The Average= "<<getAverage(value,rows,cols)<<endl;
break;
case 3:
cout<<"Enter row number: ";
cin>>i;
if(i<0||i>rows){
cout<<"Error: Must be between 0 and "<<rows
<<"Please try again: ";
break;
}
else
cout<<"Row "<<i<<" total value="<<getRowTotal(num,i,cols)<<endl;
break;
case 4:
cout<<"Enter column number: ";
cin>>i;
if(i<0||i>cols){
cout<<"Error-Must be between 0 and"
<<cols<<" try again. ";
break;
}
else
cout<<"Column "<<i<<" total value= "<<getColumnTotal(num,i,rows)<<endl;
break;
case 5:
cout<<"Enter row number: ";
cin>>i;
if(i<0||i>rows){
cout<<"Error-Must be between 0 and"<<rows<<" try again. ";
break;
}
else
cout<<"Highest element in row "<<i<<"= "<<getHighestInRow(num, i,cols)<<endl;
break;
case 6:
cout<<"Enter row number: ";
cin>>i;
if(i<0||i>rows){
cout<<"Error-Must be between 0 and"<<rows<<" try again. ";
break;
}
else
cout<<"Lowest element in row "<<i<<"= "<<getLowestInRow(num, i,cols)<<endl;
break;
case 7:
system("pause");
return 0;
default:
cout<<"Error! Please Try Again ";
}//end switch
}//end while
}//end main
//function definitions
//this function returns the total of all values in array
int getTotal(int row[][NUM_COLS],int rows,int cols)
{
int i, j, value=0;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++)
value+=row[i][j];
}
return value;
}
//this function returns average
double getAverage(int totalValues,int rows,int cols)
{
double avg;
avg=totalValues/(double)(rows*cols);
return avg;
}
//this function returns total values in specific row
int getRowTotal(int num[] [NUM_COLS], int n,int cols){
int i, value=0;
for(i=0;i<cols;i++)
value+=num[n][i];
return value;
}
//this function returns the total of values in a specific colum
int getColumnTotal(int num[] [NUM_COLS], int n,int rows){
int i, value=0;
for(i=0;i<rows;i++)
value+=num[i][n];
return value;
}
//this function returns the highest value in a specific row
int getHighestInRow(int num[] [NUM_COLS], int n,int cols){
int i, value=0;
value=num[n][0];
for(i=1;i<cols;i++){
if(num[n][i]>value)
value=num[n][i];
}
return value;
}
//this function returns lowest value in a specific row
int getLowestInRow(int num[] [NUM_COLS], int n,int cols)
{
int i, value=0;
value=num[n][0];
for(i=1;i<cols;i++)
if(num[n][i]<value)
value=num[n][i];
return value;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.