For this program, you are to work with files that have a specific format. The fi
ID: 3566801 • Letter: F
Question
For this program, you are to work with files that have a specific format. The first line of the files has two integers. The first integer specifies the number of (remaining) lines in the file, and the second integer specifies the number of integer values on each subsequent line of the file. For example, a file's contents might look like this: 2 3 1 4 7 92 3 -1 No file will have more than 250 lines nor will it have more than 250 integers per line. Write a program that solicits the name of a file that adheres to this format from the user. Your program will then attempt to open the file or print an error message if unable to do so and exit with an exit condition of -1. Next, your program will read in the first line of the file and verify that it adheres to the line limit and the integers per line limit. If the file does not adhere to the stated limitations, you are to print an appropriate error message, close the file, and terminate the program with an exit condition of -2. If the file adheres to the constraints, your program is to read the remaining entries of the file into a two-dimensional array. After reading in the contents of the file, print out the following attributes: The minimum of the averages of the rows (average all rows and find the minimum of these averages). The maximum of the averages of the columns (average all columns and find the maximum of these averages). The sum of all components. The minimum of all components. The maximum of all components. The average of all components. Print out these attributes in this order each on a dedicated line of your output. Each of the results must be computed by a separate function. That is to say you are required to have at least 6 functions in your program, each taking in the two-dimensional array as an argument and returning an integer value.Explanation / Answer
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<limits.h>
using namespace std;
double minimumOfRows(int **arr, int row, int col){
int min = INT_MAX;
for(int i=0 ;i< row; i++){
int sum =0;
for(int j=0; j<col ;j++){
sum += arr[i][j];
}
double avg = (double)sum/col;
if(avg < min){
min = avg;
}
}
return min;
}
double maximumOfRows(int **arr, int row, int col){
int max = INT_MIN;
for(int i=0 ;i< col; i++){
int sum =0;
for(int j=0; j<row ;j++){
sum += arr[j][i];
}
double avg = (double)sum/row;
if(avg > max){
max = avg;
}
}
return max;
}
int sum(int **arr, int row, int col){
int totalSum =0;
for(int i=0 ;i< row; i++){
for(int j=0; j<col ;j++){
totalSum += arr[i][j];
}
}
return totalSum;
}
int min(int **arr, int row, int col){
int min = INT_MAX;
for(int i=0 ;i< row; i++){
for(int j=0; j<col ;j++){
if(arr[i][j] < min){
min = arr[i][j];
}
}
}
return min;
}
int max(int **arr, int row, int col){
int max = INT_MIN;
for(int i=0 ;i< row; i++){
for(int j=0; j<col ;j++){
if(arr[i][j] > max){
max = arr[i][j];
}
}
}
return max;
}
double avg(int **arr, int row, int col){
int totalSum = sum(arr, row, col);
double average = (double)totalSum/(row*col);
return average;
}
int main(){
char fileName[20];
cout << "Enter the File Name: ";
cin >> fileName;
ifstream infile;
infile.open(fileName);
if(!infile.is_open()){
exit(-1);
}
int no_of_lines;
int no_per_line;
infile >> no_of_lines;
infile >> no_per_line;
if(no_of_lines > 250 || no_per_line > 250){
exit(-2);
}
int **arr = (int **)malloc(no_of_lines*sizeof(int *));
for(int i=0; i< no_of_lines; i++){
arr[i] = (int *)malloc(no_per_line*sizeof(int));
}
for(int i = 0; i<no_of_lines; i++){
for(int j = 0; j<no_per_line; j++){
infile >> arr[i][j];
}
}
double minimum = minimumOfRows(arr, no_of_lines, no_per_line);
double maximum = maximumOfRows(arr, no_of_lines, no_per_line);
int totalSum = sum(arr, no_of_lines, no_per_line);
int mini = min(arr, no_of_lines, no_per_line);
int maxi = max(arr, no_of_lines, no_per_line);
double average = avg(arr, no_of_lines, no_per_line);
cout << "The miniumum of the averages of the rows is " << minimum << endl;
cout << "The maximum of the averages of the columns is " << maximum << endl;
cout << "The sum of all components is " << totalSum << endl;
cout << "The minimum of all components is " << mini << endl;
cout << "The maximum of all components is " << maxi << endl;
cout << "The average of all components is " << average << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.