Using C The code is written but isnt giving the correct arr
(
Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after the question and data file) The concrete manufacturer has expanded its study to include more formulations and plants. They have automated the data collection so that the compressive strengths are stored in a file containing a table (2-D array). Each row of the table represents a specific formulation and each column represents a specific plant. Your program should have a main function and 4 other functions as described below. The manufacturer has stated that there will not be more than 50 formulations and not more than 12 plants.
One function should open the file and read the first line that contains the number of formulations (the first value) and number of plants (the second value). Using these values, your function should then read the rest of the data and store the values in a 2-dimensional array. As the compressive strengths are being stored, a message should be output if any compressive strength is not in the range of 15 to 75 MPa. The message should output the formulation, plant and compressive strength as the example below.
The value of 14.56 from plant 1 for formulation 8 is suspect.
The compressive strength should still be stored in the array. The number of formulations and number plants should be sent back to the function call
One function should accept the 2-dimensional array, the number of formulations and number of plants then create a 1-dimensional array containing the minimum compressive strengths for each formulation and create a 1-dimensional array containing the maximum compressive strengths for each formulation This function should call the function to output a 1-dimensional array for the minimum and maximum values before sending these arrays back to the function call.
One function should accept the 2-dimensional array and determine the average compressive strength for each formulation. The average for a each formulation should be calculated without including the maximum and minimum compressive strength for that formulation, therefore this function will need to call the function that calculates the minimum and maximum compressive strengths for each formulation. The average compressive strengths for the formulations should be stored in another 1-dimensional array which is sent back to the function call.
One function that will accept a 1-dimensional array, the number of values stored in the array and a description of what is stored in the array, e.g “minimum compressive strength for each formulation”, “ average compressive strength for each formulation”, etc. The function will then output the description and the values. The values should be output to 4 significant digits;
The main function should prompt the user to enter the filename for the data file and call the function to import the data from the file into a 2-dimensional file. Next main should call the function to calculate the averages of the formulations. The main function should then call the function to output a 1-dimensional array sending the array of averages to the function to calculate the 1-dimensional array.
Data File:
19 8
43.16 41.54 33.30 42.07 37.45 35.87 37.43 35.59
43.63 48.47 48.23 48.25 46.01 40.38 46.15 48.89
28.47 30.10 29.18 26.09 27.88 24.64 29.67 23.56
15.72 16.76 8.22 25.75 21.30 16.98 25.48 23.15
59.59 59.03 61.28 61.25 56.54 59.88 59.34 62.51
34.37 34.44 26.55 30.31 25.14 25.55 29.97 36.13
66.92 71.49 69.10 74.47 65.64 70.13 72.21 74.40
61.18 55.34 49.03 52.03 50.75 64.40 52.54 57.99
41.04 46.44 38.18 34.99 34.89 44.44 41.11 41.02
77.10 71.46 70.99 77.94 65.98 73.48 74.18 68.78
29.08 22.17 22.26 28.52 31.61 40.93 24.67 28.94
69.59 60.33 67.81 61.03 74.20 65.56 70.50 67.28
56.21 52.60 56.96 57.96 61.85 54.22 46.45 51.64
65.42 55.71 63.84 60.50 65.75 52.16 59.21 55.17
35.72 29.48 31.14 23.83 25.59 26.18 30.30 26.17
50.10 41.84 46.94 45.53 43.27 49.52 48.85 48.10
55.00 62.38 60.05 58.10 59.07 58.21 53.75 58.14
59.51 59.06 58.53 60.40 55.99 64.89 63.56 61.94
67.90 65.61 71.06 67.60 65.86 72.05 67.33 66.23
Code:
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
void Read_Input(string filename, double thearray[50][12], int &form, int &plant); //function prototype
void Min_Max(double thisarray[50][12], double Max_Formulation[50], double Min_Formulation[50], int numrows, int numcols);
void Average_Form(double newarray[50][12], int numrow, int numcol, double Average[50], double Max[50], double Min[50]);
void Display_1D_Array(double lastarray[50], int size, string description);
int main()
{
//Declare variables
string datafile = \" \";
double Form_Plant[50][12];
double MaxStrength[50];
double MinStrength[50];
int formulations = 0;
int plants = 0;
double AvgStrength[50];
string description = \" \";
double OutputArray[50];
//Get file name
cout << \"Please enter the name of the data file\ \";
cin >> datafile;
//Call function to import data into 2-D array
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 12; j++)
{
Form_Plant[i][j] = 0;
}
}
Read_Input(datafile, Form_Plant, formulations, plants);
//Call function to get maximum and minimum strengths for formulation
Min_Max(Form_Plant, MaxStrength, MinStrength, formulations, plants);
//Call function to determine average strength for formulation
Average_Form(Form_Plant, formulations, plants, AvgStrength, MaxStrength, MinStrength);
//Call function to display maximum, minimum, and average strengths for formulation
cout << \"\ Minimum compressive strength for each formulation.\ \";
Display_1D_Array(MinStrength, formulations, description);
cout << \"\ Maximum compressive strength for each formulation.\ \";
Display_1D_Array(MaxStrength, formulations, description);
cout << \"\ Average compressive strength for each formulation.\ \";
Display_1D_Array(AvgStrength, formulations, description);
return 0;
}
/*
Function to read formulations and plant numbers from data file. The function will read the first line to get
the number of formulations and plants (rows and columns) and store the rest of the data as a 2-D array. The
function will also validate that the values are within the proper range of numerical values (will store data
regardless).
input: filename from main and 2D array as parameters
processing: get file name, open file, use if statement and nested for loop to read the file and fill the 2-D array with
values from the file, if statement to validate data and output notification if data is suspect
output: void function, outputs message to notify any suspect data values for strength
*/
void Read_Input(string filename, double thearray[50][12], int &form, int &plant) //function heading
{
//Declare variables
string infile = \"\";
double input_strength[50][12];
int size[50][12];
ifstream input_values;
int rows, columns = 0;
int i = 0, j = 0;
//Get user file name
infile = filename;
//Open file
input_values.open(filename);
if (input_values.is_open())
{
input_values >> rows >> columns; //Read first line for array size
//cin >> size[rows][columns];
for (int i = 0; i < rows; i++) //Fill array with rest of file data
{
for (int j = 0; j < columns; j++)
{
input_values >> thearray[i][j]; //Validate strength values
if (thearray[i][j] > 75 || thearray[i][j] < 15)
{
cout << \"The value \" << thearray[i][j] << \" for plant \" << i << \" and formulation \" << j << \" is suspect\ \";
}
}
}
}
form = rows;
plant = columns;
}
/*
Function to determine the minimum and maximum compressive strength values
input: 2-D array from file, array size (ie. rows and columns), and 1-D arrays for Max and Min strengths
as parameters
processing: use nested for loops for max and min, output results
output: void function, outputs Min and Max 1-D arrays
*/
void Min_Max(double thisarray[50][12], double Max_Formulation[50], double Min_Formulation[50], int numrows, int numcols) //function heading
{
//Max strength for formulation
for (int i = 0; i < numrows; i++)
{
Max_Formulation[i] = thisarray[i][0];
for (int j = 0; j < numcols; j++)
{
if (thisarray[i][j] > Max_Formulation[i])
{
Max_Formulation[i] = thisarray[i][j];
}
}
}
//Min strength for formulation
for (int k = 0; k < numrows; k++)
{
Min_Formulation[k] = thisarray[k][0];
for (int l = 0; l < numcols; l++)
{
if (thisarray[k][l] < Min_Formulation[k])
{
Min_Formulation[k] = thisarray[k][l];
}
}
}
}
/*
Function to calculate the average compressive strength excluding Min and Max for each formulation
input: 2-D array, array size, 1-D array containing the Average values, 1-D arrays for Max and Min from
previous function as parameters
processing: initialize an array to store the sum, use nested for loop to determine the sum, subtract out
the 1-D arrays with the Max and Min, calculate Average value
output: void function
*/
void Average_Form(double newarray[50][12], int numrow, int numcol, double Average[50], double Max[50], double Min[50]) //function heading
{
double sum[50];
for (int i = 0; i < numrow; i++)
{
sum[i] = 0;
for (int j = 0; j < numcol; j++)
{
sum[i] += newarray[i][j];
}
sum[i] = sum[i] - Max[i] - Min[i];
Average[i] = sum[i] / (numrow - 2);
}
}
/*
Function to output the 1-D arrays containing the min, max, and average strengths per formulation. Output
will be accompanied with a description of which array is displayed
input: 1-D array (either Min, Max, or Average), array size (rows), and description of what array is
processing: use loop to fill array, output description and array
output: number, description, and array
*/
void Display_1D_Array(double lastarray[50], int size, string description) //function heading
{
for (int i = 0; i < size; i++)
{
cout << i << description << lastarray[i] << \"\ \";
}
}
Solution
Please follow the code and comments for description :
CODE :
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
void Read_Input(string filename, double thearray[50][12], int &form, int &plant); //function prototype
void Min_Max(double thisarray[50][12], double Max_Formulation[50], double Min_Formulation[50], int numrows, int numcols);
void Average_Form(double newarray[50][12], int numrow, int numcol, double Average[50], double Max[50], double Min[50]);
void Display_1D_Array(double lastarray[50], int size, string description);
int main()
{
//Declare variables
string datafile = \" \";
double Form_Plant[50][12];
double MaxStrength[50];
double MinStrength[50];
int formulations = 0;
int plants = 0;
double AvgStrength[50];
string description = \" \";
double OutputArray[50];
//Get file name
cout << \"Please enter the name of the data file\ \";
cin >> datafile;
//Call function to import data into 2-D array
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 12; j++)
{
Form_Plant[i][j] = 0;
}
}
Read_Input(datafile, Form_Plant, formulations, plants);
//Call function to get maximum and minimum strengths for formulation
Min_Max(Form_Plant, MaxStrength, MinStrength, formulations, plants);
//Call function to determine average strength for formulation
Average_Form(Form_Plant, formulations, plants, AvgStrength, MaxStrength, MinStrength);
//Call function to display maximum, minimum, and average strengths for formulation
cout << \"\ Minimum compressive strength for each formulation.\ \";
Display_1D_Array(MinStrength, formulations, description);
cout << \"\ Maximum compressive strength for each formulation.\ \";
Display_1D_Array(MaxStrength, formulations, description);
cout << \"\ Average compressive strength for each formulation.\ \";
Display_1D_Array(AvgStrength, formulations, description);
return 0;
}
/*
Function to read formulations and plant numbers from data file. The function will read the first line to get
the number of formulations and plants (rows and columns) and store the rest of the data as a 2-D array. The
function will also validate that the values are within the proper range of numerical values (will store data
regardless).
input: filename from main and 2D array as parameters
processing: get file name, open file, use if statement and nested for loop to read the file and fill the 2-D array with
values from the file, if statement to validate data and output notification if data is suspect
output: void function, outputs message to notify any suspect data values for strength
*/
void Read_Input(string filename, double thearray[50][12], int &form, int &plant) //function heading
{
//Declare variables
string infile = \"\";
double input_strength[50][12];
int size[50][12];
ifstream input_values;
int rows, columns = 0;
int i = 0, j = 0;
//Get user file name
infile = filename;
//Open file
input_values.open(filename);
if (input_values.is_open())
{
input_values >> rows >> columns; //Read first line for array size
//cin >> size[rows][columns];
for (int i = 0; i < rows; i++) //Fill array with rest of file data
{
for (int j = 0; j < columns; j++)
{
input_values >> thearray[i][j]; //Validate strength values
if (thearray[i][j] > 75 || thearray[i][j] < 15)
{
cout << \"The value \" << fixed <<setprecision(2) << thearray[i][j] << \" for plant \" << i << \" and formulation \" << j << \" is suspect\ \";
}
}
}
}
form = rows;
plant = columns;
}
/*
Function to determine the minimum and maximum compressive strength values
input: 2-D array from file, array size (ie. rows and columns), and 1-D arrays for Max and Min strengths
as parameters
processing: use nested for loops for max and min, output results
output: void function, outputs Min and Max 1-D arrays
*/
void Min_Max(double thisarray[50][12], double Max_Formulation[50], double Min_Formulation[50], int numrows, int numcols) //function heading
{
//Max strength for formulation
for (int i = 0; i < numrows; i++)
{
Max_Formulation[i] = thisarray[i][0];
for (int j = 0; j < numcols; j++)
{
if (thisarray[i][j] > Max_Formulation[i])
{
Max_Formulation[i] = thisarray[i][j];
}
}
}
//Min strength for formulation
for (int k = 0; k < numrows; k++)
{
Min_Formulation[k] = thisarray[k][0];
for (int l = 0; l < numcols; l++)
{
if (thisarray[k][l] < Min_Formulation[k])
{
Min_Formulation[k] = thisarray[k][l];
}
}
}
}
/*
Function to calculate the average compressive strength excluding Min and Max for each formulation
input: 2-D array, array size, 1-D array containing the Average values, 1-D arrays for Max and Min from
previous function as parameters
processing: initialize an array to store the sum, use nested for loop to determine the sum, subtract out
the 1-D arrays with the Max and Min, calculate Average value
output: void function
*/
void Average_Form(double newarray[50][12], int numrow, int numcol, double Average[50], double Max[50], double Min[50]) //function heading
{
double sum[50];
for (int i = 0; i < numrow; i++)
{
sum[i] = 0;
for (int j = 0; j < numcol; j++)
{
sum[i] += newarray[i][j];
}
sum[i] = sum[i] - Max[i] - Min[i];
Average[i] = (double)sum[i] / (numcol - 2);
}
}
/*
Function to output the 1-D arrays containing the min, max, and average strengths per formulation. Output
will be accompanied with a description of which array is displayed
input: 1-D array (either Min, Max, or Average), array size (rows), and description of what array is
processing: use loop to fill array, output description and array
output: number, description, and array
*/
void Display_1D_Array(double lastarray[50], int size, string description) //function heading
{
for (int i = 0; i < size; i++)
{
cout << i << fixed <<setprecision(2) << description << lastarray[i] << \"\ \";
}
}
DESCRIPTION :
The line that needs to be calculate the average of the data is divide with respect to the number of rows instead that needs to be calculated based on the number of columns which is 6 after subtracting the minimum and the maximum values. So that has to be updated and the process during the division it is suggested that the numerator needs to be typcasted to double so as to avoid any misconceptions during the executions. The points has been updated and been bolded for clear vision and understanding.
OUTPUT :
Please enter the name of the data file
input.txt
The value 8.22 for plant 3 and formulation 2 is suspect
The value 77.10 for plant 9 and formulation 0 is suspect
The value 77.94 for plant 9 and formulation 3 is suspect
Minimum compressive strength for each formulation.
0 33.30
1 40.38
2 23.56
3 8.22
4 56.54
5 25.14
6 65.64
7 49.03
8 34.89
9 65.98
10 22.17
11 60.33
12 46.45
13 52.16
14 23.83
15 41.84
16 53.75
17 55.99
18 65.61
Maximum compressive strength for each formulation.
0 43.16
1 48.89
2 30.10
3 25.75
4 62.51
5 36.13
6 74.47
7 64.40
8 46.44
9 77.94
10 40.93
11 74.20
12 61.85
13 65.75
14 35.72
15 50.10
16 62.38
17 64.89
18 72.05
Average compressive strength for each formulation.
0 38.32
1 46.79
2 27.66
3 19.90
4 60.06
5 30.20
6 70.71
7 54.97
8 40.13
9 72.67
10 27.51
11 66.96
12 54.93
13 59.98
14 28.14
15 47.04
16 58.09
17 60.50
18 67.66
input.txt :
19 8
43.16 41.54 33.30 42.07 37.45 35.87 37.43 35.59
43.63 48.47 48.23 48.25 46.01 40.38 46.15 48.89
28.47 30.10 29.18 26.09 27.88 24.64 29.67 23.56
15.72 16.76 8.22 25.75 21.30 16.98 25.48 23.15
59.59 59.03 61.28 61.25 56.54 59.88 59.34 62.51
34.37 34.44 26.55 30.31 25.14 25.55 29.97 36.13
66.92 71.49 69.10 74.47 65.64 70.13 72.21 74.40
61.18 55.34 49.03 52.03 50.75 64.40 52.54 57.99
41.04 46.44 38.18 34.99 34.89 44.44 41.11 41.02
77.10 71.46 70.99 77.94 65.98 73.48 74.18 68.78
29.08 22.17 22.26 28.52 31.61 40.93 24.67 28.94
69.59 60.33 67.81 61.03 74.20 65.56 70.50 67.28
56.21 52.60 56.96 57.96 61.85 54.22 46.45 51.64
65.42 55.71 63.84 60.50 65.75 52.16 59.21 55.17
35.72 29.48 31.14 23.83 25.59 26.18 30.30 26.17
50.10 41.84 46.94 45.53 43.27 49.52 48.85 48.10
55.00 62.38 60.05 58.10 59.07 58.21 53.75 58.14
59.51 59.06 58.53 60.40 55.99 64.89 63.56 61.94
67.90 65.61 71.06 67.60 65.86 72.05 67.33 66.23
Hope this is helpful.
![( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th ( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th](/WebImages/40/using-c-the-code-is-written-but-isnt-giving-the-correct-arr-1123056-1761598100-0.webp)
![( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th ( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th](/WebImages/40/using-c-the-code-is-written-but-isnt-giving-the-correct-arr-1123056-1761598100-1.webp)
![( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th ( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th](/WebImages/40/using-c-the-code-is-written-but-isnt-giving-the-correct-arr-1123056-1761598100-2.webp)
![( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th ( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th](/WebImages/40/using-c-the-code-is-written-but-isnt-giving-the-correct-arr-1123056-1761598100-3.webp)
![( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th ( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th](/WebImages/40/using-c-the-code-is-written-but-isnt-giving-the-correct-arr-1123056-1761598100-4.webp)
![( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th ( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th](/WebImages/40/using-c-the-code-is-written-but-isnt-giving-the-correct-arr-1123056-1761598100-5.webp)
![( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th ( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th](/WebImages/40/using-c-the-code-is-written-but-isnt-giving-the-correct-arr-1123056-1761598100-6.webp)
![( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th ( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th](/WebImages/40/using-c-the-code-is-written-but-isnt-giving-the-correct-arr-1123056-1761598100-7.webp)
![( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th ( Using C++ The code is written, but isn\'t giving the correct arrays as outputs [Min, Max, Average] and I\'m not sure how to fix it. The code is found after th](/WebImages/40/using-c-the-code-is-written-but-isnt-giving-the-correct-arr-1123056-1761598100-8.webp)