Question #59488

Number Analysis Program 


Write a program that reads data from file numbers.txt and process it. The file contains a series of numbers, each written on a separate line. The program should read the contents of the file into an array and then display the lowest, highest, total, and average of the numbers in the array. The program should have the following functions: 


readNumbers. This function reads numbers from file and stores them in the array.

getLowest. This function finds the lowest value in the array and returns that value.

getHighest. This function finds the highest value in the array and returns that value.

getSum. This function calculates and returns the sum of the values in the array.

getAverage. This function calculates and returns the average of the values in the array.
1

Expert's answer

2016-04-27T10:52:05-0400

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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS