The 2-dimensional array SHOULD be read from a file: name the file 07_16.txt, the
ID: 3530529 • Letter: T
Question
The 2-dimensional array SHOULD be read from a file: name the file 07_16.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
#include <iostream>
#include <fstream>
#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_ROWS;i++)
{
for(j=0;j<NUM_COLS;j++)
cout<<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;i<NUM_ROWS;i++)
for(j=0;j<NUM_COLS;j++)
value+=row[i][j];
return value;
}
//this function returns average
double getAverage(int totalValues)
{
double avg;
avg=totalValues/(NUM_ROWS*NUM_COLS);
return avg;
}
//this function returns total values in specific row
int getRowTotal(int num[] [NUM_COLS], int n)
{
int i, value=0;
for(i=0;i<NUM_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 i, value=0;
for(i=0;i<NUM_COLS;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 i, value=0;
value=num[n][0];
for(i=1;i<NUM_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 i, value=0;
value=num[n][0];
for(i=1;i<NUM_COLS;i++)
if(num[n][i]>value)
value=num[n][i];
return value;
}
Explanation / Answer
....
int main()
{
int i, j, value, ch;
int num[NUM_ROWS] [NUM_COLS];
int rows, cols;
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
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;
}
....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.