C++ program:Help. Write a complete program that records the number of miles for
ID: 3678214 • Letter: C
Question
C++ program:Help. Write a complete program that records the number of miles for a runner over the past 10 days. Store the miles in a single dimensional array. Computer the average miles, the highest number of miles and the lowest number of miles. To receive full credit, please complete the following (50 points): l. Read from an input file and print to an output file 2. Use functions to: Read information in the input file Print to an output file Calculate the average Determine the highest number of miles Determine the lowest number of milesExplanation / Answer
/* c++ code that records the number of miles run and stores in output file average, lowest and highest mile run */
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
double mile[10]; // array to hold the records of miles run
void input()
{
ifstream file_("input.txt"); // opening input.txt
int i = 0;
double number;
while(file_ >> number)
{
mile[i] = number; // putting integers into array
i++;
}
file_.close(); // closing input.txt
}
void output(double average, double low, double high)
{
ofstream file("output.txt"); // opening file output.txt
file << "Average: ";
file << average;
file << " ";
file << "lowest: " ;
file << low;
file << " ";
file << "highest: ";
file << high;
file.close(); // closing output.txt
}
double average (double a[] , int n) // function to calculate average
{
double sum = 0;
for (int j = 0; j < n; j++)
{
sum = sum + a[j];
}
sum = sum /n;
return sum;
}
double lowest (double a[], int n)
{
double low = a[0];
for (int i = 1; i < n; ++i)
{
if(a[i] < low) low = a[i];
}
return low;
}
double highest ( double a[], int n)
{
double high = a[0];
for (int i = 1; i < n; ++i)
{
if(a[i] > high) high = a[i];
}
return high;
}
int main()
{
input();
double average_of_miles_run = average ( mile, 10); // calling the function to calculate average
double low = lowest( mile, 10); // calling function to calculate lowest
double high = highest( mile, 10); // calling function to calculate highest
output(average_of_miles_run, low, high);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.