Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

the question: Number Analysis Program Write a program that asks the user for a f

ID: 3770798 • Letter: T

Question

the question:

Number Analysis Program Write a program that asks the user for a file name. Assume 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 following data: • The lowest number in the array • The highest number in the array • The total of the numbers in the array • The average of the numbers in the array If you have downloaded this book’s source code from the companion Web site, you will find a file named numbers.txt in the Chapter 07folder. You can use the file to test the program. (The companion Web site is at www.pearsonhighered.com/gaddis .)

what i have so far...

int getHighest(int values[], int size)

int getSum(int values[], int size)

double getAverage(int values[], int size)

The int getLowest(int values[], int size) and void readNumbers(string, int [], int) and the main functions are already written for you.

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

// Global constant for the array size

const int ARRAY_SIZE = 12;

// Function prototypes

void readNumbers(string, int [], int);

int getLowest(int [], int);

int getHighest(int [], int);

int getSum(int [], int);

double getAverage(int [], int);

int main()

{

      

       // Array to hold the numbers

       int numbers[ARRAY_SIZE];

       // Read the numbers into the array.

       readNumbers("numbers.txt", numbers, ARRAY_SIZE);

       // Display data about the numbers.

       cout << "The highest value is "

             << getHighest(numbers, ARRAY_SIZE)

             << endl;

      

       cout << "The lowest value is "

             << getLowest(numbers, ARRAY_SIZE)

             << endl;

       cout << "The sum of the numbers is "

             << getSum(numbers, ARRAY_SIZE)

             << endl;

       cout << "The average of the numbers is "

             << getAverage(numbers, ARRAY_SIZE)

             << endl;

            

       system("pause");

       return 0;

}

// ********************************************************

// The readNumbers function reads numbers from FileName and *

// stores them in the numbers array.                      *

// ********************************************************

void readNumbers(string FileName, int numbers[], int size)

{

   ifstream inFile;

   inFile.open(FileName);

   // Test for errors.

       if (!inFile)

       {

             cout << "Error opening the file. ";

             exit(1);

       }

   for (int i = 0; i < size; i++)

      inFile >> numbers[i];

   inFile.close();

}

// ********************************************************

// The getLowest function finds the lowest value in the   *

// array and returns that value. The size parameter is    *

// the number of elements in the array.                  *

// ********************************************************

int getLowest(int values[], int size)

{

       int lowest = values[0];

       for (int i = 1; i < size; i++)

       {

             if (values[i] < lowest)

                    lowest = values[i];

       }

       return lowest;

}

// ********************************************************

// The getHighest function finds the highest value in the *

// array and returns that value. The size parameter is    *

// the number of elements in the array.                   *

// ********************************************************

int getHighest(int values[], int size)

{

      

      

}

// ********************************************************

// The getSum function calculates and returns the sum of *

// the values stored in the array.                        *

// ********************************************************

int getSum(int values[], int size)

{

      

}

// ********************************************************

// The getAverage function calculates and returns the     *

// average of the values stored in the array.             *

// ********************************************************

double getAverage(int values[], int size)

{

       //use the getSum function here to get the sum

      

}

Explanation / Answer

#include <stdio.h>
#include <math.h>

int main(int argc, char*argv[])
{
    double average, num = 0, min = 0, max = 0, sum = 0, N, std_dev, sum_sqs = 0.0;

    if (argc <= 1)
    {
        fprintf(stderr, "Usage: %s file ", argv[0]);
        return 1;
    }

    FILE *pFile = fopen(argv[1], "r");
    if (pFile == 0)
    {
        fprintf(stderr, "%s: failed to open file %s ", argv[0], argv[1]);
        return 1;
    }
    if (fscanf(pFile, "%lf", &N) == 1)
    {
        for (int i = 0; i < N; i++)
        {
            if (fscanf(pFile, "%lF", &num) != 1)
            {
                fprintf(stderr, "%s: failed to read number ", argv[0]);
                return 1;
            }

            if (num < min || i == 0)
                min = num;
            if (num > max || i == 0)
                max = num;
            sum += num;
            sum_sqs += (num*num);
        }
    }

    fclose(pFile);
    average = sum/N;
    std_dev = sqrt((sum_sqs/N)-(average*average));

    printf("Smallest: %7.2lf ", min);
    printf("Largest: %7.2lf ", max);
    printf("Average: %7.2lf ", average);
       return(0);
}


OUTPUT: -

Given data file data:

5  
4.34 23.4 18.92 -78.3 17.9

The result of running the program is:

Smallest: -78.30
Largest:   23.40
Average:   -2.75