#include <iostream> #include <cmath> #include <fstream> using namespace std; //
ID: 3553887 • Letter: #
Question
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
// Function prototypes
int getLowest(int [], int);
int getHighest(int [], int);
int getSum(int [], int);
double getAverage(int [], int);
const int ARRAYSIZE = 100; //array size
int main()
{
int number[ARRAYSIZE];
int highest, lowest, sum; //values
double avg; //avg value
int count = 0,i;
ifstream inputFile; //open stored file
inputFile.open("numbers.txt");
if (!inputFile)
{
cout << "Error opening File. ";
}
else
{
while(inputFile >> i)
{
number[count++] = i;
}//end while
inputFile.close();
}
highest = getHighest(number, count); //get highest number and display
lowest = getLowest(number, count); //get lowest number and display
sum = getSum(number,count); //get sum and display
avg = getAverage(number,count); //get avg and display
cout << "The highest value is "<< highest << endl;
cout << "The lowest value is " << lowest << endl;
cout << "The sum of the numbers is " << sum << endl;
cout << "The average of the numbers is " << avg << endl;
return 0;
}
//Functions
int getLowest(int number[], int length) //get Lowest Function
{
int lowest, count;
lowest = number[0];
for (count = 1; count < length; count++)
{
if (number[count] < lowest)
lowest = number[count];
}
return lowest;
}
int getHighest(int number[], int length) //get Highest Function
{
int highest, count;
highest = number[12];
for (count = 1; count < length; count++)
{
if (number[count] > highest)
highest = number[count];
}
return highest;
}
int getSum(int number[], int length) //get Sum Function
{
int total = 0;
for (int count = 0; count < length; count++)
{
total += number[count];
}
return total;
}
double getAverage(int number[], int length) //get average Function
{
double total = 0; // Initialize Accumulator
double average; // To hold the average
for (int count = 0; count < length; count++)
{
total += number[count];
average = total / length;
}
return average;
}
Input file numbers.txt has
19 69 43 15 23 89 19 54 69 4 13 37 66 8 78 67 32 76 13 87 42 23 32 88 21 4 2 61 99 43
question is ;
Write a program that reads in an array of type int. It provides facility to either read an array from the keyboard or from a file, at the user's choice. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 100 entries in this array. Your program determines how many entries there are. In addition, the program should also find the maximum number in the array and calculates the average of the array. You are required to use functions.
if its not working, please fix it.
Explanation / Answer
This program is working
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.