Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that reads up to 50 measurements from a file into an array,

ID: 3863780 • Letter: W

Question

Write a C++ program that reads up to 50 measurements from a file into an array, determines the minimum measurement, the maximum measurement, and the trimmed mean (the mean of all values except the minimum and maximum) then outputs to the screen a formatted report using output manipulation functions.

1) The input file is named measures.in :

• The first line of the file is a line of text describing the data. You must use this as the title of the report.

• All lines after the first contain one real number, the measurement. You must read these into an array until you have read 50 of them or you reach the end of the file, whichever comes first.

2) You must define and use global constants for:
• the maximum number of measures to be read,
• the number of columns in the output, and
• the width of each column.

3) You must use and define the following six functions; you may define more functions if you find it useful to do so.

(a) getTitle:
• Parameters: an ifstream connected to the input file
• Return: a string, the report title
• This function reads the report title from the file
(b) getMeasures
• Parameters: an ifstream connected to the input file, the array of doubles to store the measures, an int to store the number of measures read
• Return: none
• This function reads the measures from the file into the array and count the number of measures read.
(c) determineMax (NOTE: this behaves differently than findMin)
• Parameters: the array of measures, the number of measures
• Return: a double, the value of the maximum
• This function determines the value of the maximum.
(d) findMin (NOTE: this behaves differently than determineMax)
• Parameters: the array of measures, the number of measures
• Return: an int, the position of the minimum value in the array
• This function finds the position of the minimum value in the array, if the minimum appears more than once the first instance of the value is returned.
(e) calcTrimmedMean
• Parameters: the array of measures, the number of measures
• Return: a double, the trimmed mean
• This function calculates the trimmed mean. The trimmed mean is the mean of all the measures except the minimum and the maximum. This function calls determineMax and findMin.
(f) printMeasures
• Parameters: the array of measures, the number of measures

• Return: none

• This function prints the measures in 10 columns of width 8.

Here is the content of the file:

Blaster Size (Galactic Standard Units) - Sampled from Corscanti Vendors

17.9
24.6
10.3
19.2
6.3
20.6
15.2
22.2
20.2
23.6
7
26.5
26.9
5.7
10.2
10.7
5.4
23.6
17.1
17.8

5.1

24.6
12.3
13.1
23
8.5
12.1
15.7
16.2
22.6
18.6

23.3
18.8
18.5
6.9
16.7
18.4

Here is how the final output must look like:

Note: So far in this semester, I have learnt switch and if statments. Also do-while loops. Plus strings, functions and now arrays. I guess you gotta use most of these to write the code.

Please do not use struct statements while writing the code. I will take that next week.

Blaster Size Galactic Standard Units Sampled from Corscanti Vendors 17.90 24.60 10.30 19.20 6.30 20.60 15.20 22.20 20.20 23.60 7.00 26.50 26.90 5.70 10.20 10.70 5.40 23.60 17.10 17.80 5.10 24.60 12.30 13.10 23.00 8.50 12.10 15.70 16.20 22.60 18.60 23.30 18.80 18.50 6.90 16.70 18.40 The minimum measurement is 5.10 The maximum measurement is 26.90 16.38 The trimmed mean is

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

# define MAX_MEASURES 50
# define MAX_COL 10
# define MAX_WIDTH 8

using namespace std;

string getTitle(ifstream*);
void getMeasures(ifstream*, double[], int*);
double determineMax(double[], int);
int findMin(double[], int);
double trimmed(double[], int);
void printMeasures(double[], int);

int main(){
  
ifstream myfile ("measures.in");
ifstream *x = &myfile;
if(myfile.is_open()){
string header = getTitle(x);
int n=MAX_MEASURES,y;
double measures[n];
getMeasures(x, measures, &y);
printf("%s ", header.c_str());
printf("==================================================================== ");
printMeasures(measures, y);

}
else{
cout<<"unable to open file"<<endl;
}
return 0;
}

string getTitle(ifstream *f){
string line;
getline(*f, line);
return line;
}
void getMeasures(ifstream *f, double arr[], int* n){
string line;
int i=0;
while( getline(*f, line) && i <= MAX_MEASURES ){
double val;
try{
val = atof(line.c_str());
arr[i] = val;
i += 1;
throw line;
}
catch(string line){

}

}
*n = i;
}
double determineMax(double arr[], int n){
double max = arr[0];
for(int i=1;i<n;i++){
if(arr[i] > max){
max = arr[i];
}
}
return max;

}
int findMin(double arr[], int n){
double min = arr[0];
int min_pos = 0;
for(int i=1;i<n;i++){
if(arr[i] < min){
min = arr[i];
min_pos = i;
}
}
return min_pos;
}
double trimmed(double arr[], int n){
double sum=0,mean,max,min;
for(int i=0;i<n;i++){
sum += arr[i];
}
int min_pos = findMin(arr, n);
min = arr[min_pos];
max = determineMax(arr, n);
sum -= (min + max);
mean = sum/(n-2);
return mean;
}
void printMeasures(double arr[], int n){

int i,aug=0;
for(int j=0;j<MAX_WIDTH/2;j++){
for(i=aug;i<n;i++){
printf("%.2f ", arr[i]);
int k = i+1;
if(k % MAX_COL == 0)
break;
}
aug=i+1;
printf(" ");
}
printf(" ");
printf("The minimum measurement is : %.2f ", arr[findMin(arr, n)]);
printf("The maximum measurement is : %.2f ", determineMax(arr, n));
printf("The trimmed mean is : %.2f ", trimmed(arr, n));

}

Here's the sample output:

lokesh1729@lokesh1729-Inspiron-N5050:~/C++$ ./galatic_blaster.out
Blaster Size (Galactic Standard Units) - Sampled from Corscanti Vendors
====================================================================
17.90   24.60   10.30   19.20   6.30   20.60   15.20   22.20   20.20   23.60  
7.00   26.50   26.90   5.70   10.20   10.70   5.40   23.60   17.10   17.80  
5.10   24.60   12.30   13.10   23.00   8.50   12.10   15.70   16.20   22.60  
18.60   23.30   18.80   18.50   6.90   16.70   18.40  

The minimum measurement is : 5.10
The maximum measurement is : 26.90
The trimmed mean is : 16.38

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote