Overview For this activity, you will write a C++ program that will read a series
ID: 3779919 • Letter: O
Question
Overview
For this activity, you will write a C++ program that will read a series of real values representing the yearly growth of pine trees, average the values, and then print the values and their difference from the average. You must use an array of doubles to hold the values.
Input Specification
The input will contain a series of real (double) values read from a text file. Create a new text file named arrays.txt. Your program should handle up to but no more than 10 values. It will print error messages if there are no values. Any input beyond 10 values should be ignored. (It would be better to alert the user that you are ignoring values, but the goal of this assignment is to use arrays.) Your program should read until end-of-file or 10 values have been read. You should not prompt for input or echo the values as you read them. It is a good idea to define a global constant for the maximum number of values (10).
Output Specification
The first output is a count of how many values are included in the calculations. The second output is the average. Then, for each value, you will print the tree's index in the array, its growth value, and that value's difference from the average. Print each real value with one digit after the decimal point. An output line looks like:
Tree 3 grew 12.4 which is -2.3 from the average
If there are no values, you should print an error message. Do not divide by zero, do not print any data, and do not compute or print the average. Just print an error message and stop.
If the program inputs more than the allowed number of values, ignore those extra values. You may print an alert message, but you should average and report on the first allowed number of values.
Approaching the Problem
Develop your solution incrementally. This problem naturally uses functions extensively. Remember that each function should have a short comment describing what it does. Writing that comment before writing the code helps you think about what you are writing and why. Be careful to pass the array efficiently and safely. Consider when it should and should not change. Realize that arrays are naturally passed by reference without an & to indicate that it is pass-by-reference. I suggest the following iterations.
Declare an array and fill each element with its index (that is, trees[t] = t) and print the values using a print function. Write that print function, How is the array passed into it? The function will also need to know how many values in the array contain useful data. That is an extra parameter. How is it passed? Save that, compile it, and test it.
Use a function to fill the array with values read from the text file. How is the array passed here? You also need to pass back the number of values read. How is that parameter passed? Print the count in main with a cout statement.
Compute and print the average. A function is good for computing the average. The two parameters don't change. What type of value is returned from this function? Be sure to catch the answer in main and print it.
Write a function that for each element in the array, prints the difference from the average. You should modify your earlier print function.
Add the error conditions. Be sure to test with no data, one value, exactly the maximum number of values, and one more than the maximum number. You may note some rounding in the difference from the average.
Sample Input
6.5 7.8 -1.4 9 22 -2.7 0 17 ^Z
Sample Output
8 values included in calculations
The average is 7.3
Tree 0 grew 6.5 which is -0.8 from the average
Tree 1 grew 7.8 which is 0.5 from the average
Tree 2 grew -1.4 which is -8.7 from the average
Tree 3 grew 9.0 which is 1.7 from the average
Tree 4 grew 22.0 which is 14.7 from the average
Tree 5 grew -2.7 which is -10.0 from the average
Tree 6 grew 0.0 which is -7.3 from the average
Tree 7 grew 17.0 which is 9.7 from the average
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int main() {
double trees[10];
int i=0;
double avg = 0;
string line;
ifstream myfile ("arrays.txt");
if (myfile.is_open())
{
while ( getline (myfile,line,' ') )
{
double num = atof(line.c_str());
trees[i++] = num;
avg += num;
if(i>9)
break;
}
myfile.close();
}
if(i>0 && avg!=0){
avg = avg/i;
cout << "The average is :" << avg << endl;
for(int j=0;j<i;j++){
cout << "Tree " << j << " grew " << trees[j] << " which is " << trees[j]-avg << " from average" << endl;
}
}
else if(i==0){
cout << "No data for trees" << endl;
}
else{
cout << "Average is zero";
}
}
/*
sample output
The average is :7.275
Tree 0 grew 6.5 which is -0.775 from average
Tree 1 grew 7.8 which is 0.525 from average
Tree 2 grew -1.4 which is -8.675 from average
Tree 3 grew 9 which is 1.725 from average
Tree 4 grew 22 which is 14.725 from average
Tree 5 grew -2.7 which is -9.975 from average
Tree 6 grew 0 which is -7.275 from average
Tree 7 grew 17 which is 9.725 from average
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.