Answer on Question #59488
Main.cpp
#include <iostream>
#include <fstream>
#define ARRAY_SIZE 10000
using namespace std;
//---
// Function prototypes
//---
void readNumbers(int *numberArray, int *counter);
int getLowest(int *numberArray, int count);
int getHighest(int *numberArray, int count);
int getSum(int *numberArray, int count);
double getAverage(int *numberArray, int count);
int main()
{
int numberArray[ARRAY_SIZE];
int count = 0;
readNumbers(numberArray, &count);
cout << numberArray[3] << "\n" << count << endl;
cout << "Lowest: " << getLowest(numberArray, count) << endl;
cout << "Highest: " << getHighest(numberArray, count) << endl;
cout << "Sum: " << getSum(numberArray, count) << endl;
cout << "Average: " << getAverage(numberArray, count) << endl;
system("pause");
return 0;
}
// ---
// Read numbers from file and save it in array
//---
void readNumbers(int *numberArray, int *counter)
{
int number = 0;
ifstream readfile("numbers.txt");
if (readfile.is_open())
{
readfile >> numberArray[(*counter)++];
cout << numberArray[(*counter) - 1] << endl;
numberArray[1] = number;
while (!readfile.eof())
{
readfile >> numberArray[(*counter)++];
}
}
cout << numberArray[(*counter) - 1] << endl;
readfile.close();
}// Search and return the lowest value in array
int getLowest(int *numberArray, int count)
{
int min = numberArray[0];
for (int i = 1; i < count; i++)
{
if (numberArray[i] < min)
{
min = numberArray[i];
}
}
return min;
}// Search and return the highest value in array
int getHighest(int *numberArray, int count)
{
int max = numberArray[0];
for (int i = 1; i < count; i++)
{
if (numberArray[i] > max)
{
max = numberArray[i];
}
}
return max;
}// Calculate sum of the all elements in array
int getSum(int *numberArray, int count)
{
int sum = 0;
for (int i = 0; i < count; i++)
{
sum += numberArray[i];
}
return sum;
}// Calculate the average value of the all elements in array
http://www.AssignmentExpert.com/
double getAverage(int *numberArray, int count)
{
double sum = 0;
for (int i = 0; i < count; i++)
{
sum += numberArray[i];
}
return sum/count;
}numbers.txt
12
32
43
2
3
1
-1
213
23
18
22
53
34
Comments