Wednesday, February 1, 2017

Programming Challenge 7.7 - Number Analysis Program

Example File: Numbers.txt

/* Number Analysis Program - This program asks the user for a file name.
   The file contains a series of numbers, each written on a separate line.
   The program reads the contents of the file into an array and then
   displays 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 */

#include "Utility.h"

/* Global constant variable: Max array size */
const int G_CONST_MAX_SIZE = 100;

void displayIntro();
void processFile();
void processNumbers(int []);

int main()
{
   /* Call: processFile */
   displayIntro();
   processFile();

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: displayIntro

   This function displays a short program introduction.
   ********************************************************** */

void displayIntro()
{
   /* Display: Introduction */
   cout << "\t\tFile Processing Demonstration\n\n"
        << "This program allows you to provide a file containing\n"
        << "a set of numbers. Once you enter the filename, the file\n"
        << "will be processed, and the following information will be\n"
        << "displayed:\n\n"
        << " * The numbers contained in the file\n"
        << " * The sum of the numbers\n"
        << " * The highest and the lowest numbers\n"
        << " * The average number of all numbers\n\n";

}

/* **********************************************************
   Definition: processFile

   This function opens and processes a file, after the user
   has entered a valid filename. The content of the file is
   then stored read into an array.
   ********************************************************** */

void processFile()
{
   /* Array variable: Get numbers */
   int getNumbers[G_CONST_MAX_SIZE] = {};

   /* Create: File stream object */
   ifstream userNumbers;

   /* Variables: Get filename, Numbers, Count (loop counter) */
   string getFilename = " ";
   int count = 0;

   /* Get the filename */
   cout << "Please enter the filename: ";
   cin >> getFilename;

   /* Open the file */
   userNumbers.open(getFilename);

   /* If the file was opened successfully, the file content will
      be read into the array, and processNumbers is called to
      further process the data in the file. In case the file can't
      be opened or processed, an error message is displayed. */
   if (userNumbers)
   {
      while (count < G_CONST_MAX_SIZE && userNumbers >> getNumbers[count])
             count++;

      /* Call: processNumbers */
      processNumbers(getNumbers);
   }
   else
   {
      cout << "\nFile open error: The filename entered was either wrong,\n"
           << "or the file is damaged and can't be opened. Please\n"
           << "make sure that you spelled the filename correctly, and the\n"
           << "file is not otherwise damaged or has been moved from the\n"
           << "program directory.\n\n"
           << "This program will now exit ...\n"
           << "(Press Enter to close the window)\n";
   }

   /* Close file */
   userNumbers.close();
}

/* **********************************************************
   Definition: processFile

   This function accepts the following array as argument:

      * processNumbers[]

   It determines the highest and lowest numbers stored in the
   array, the total number of numbers, the sum of the numbers
   and the average of all numbers in the array. After these
   operations are carried out successfully, the results are
   displayed.
   ********************************************************** */

void processNumbers(int numbers[])
{
   /* Variables: Highest number, Lowest number, Sum total (accumulator),
                 Average, Count (counter variable) */
   int highestNum = numbers[0],
       lowestNum = numbers[0],
       sumTotal = 0,
       average = 0,
       count = 0;

   /* The ternary operators find the highest and lowest number in the array,
      and sumTotal accumulates the numbers contained in the array. */
   for (count = 0; count < numbers[count]; count++)
   {
      numbers[count] > highestNum ? highestNum = numbers[count] : numbers[count]; 
      numbers[count] < lowestNum ? lowestNum = numbers[count] : numbers[count];

      sumTotal += numbers[count];
   }

   /* Calculate the average */
   average = sumTotal / count;

   /* Display the processed file information */
   cout << "\nThe following numbers are stored in this file:\n";
   for (count = 0; count < numbers[count]; count++)
   {
      cout << numbers[count] << " ";     
   }

   cout << "\n\nThere are " << count << " numbers in this file in total.\n\n";
   cout << "The sum of all numbers is: "
        << setw(8) << sumTotal;
   cout << "\nThe average of all numbers is: "
        << setw(4) << right << average;
   cout << "\nThe highest number found is: "
        << setw(6) << right << highestNum << " ";
   cout << "\nThe lowest number found is: "
        << setw(7) << right << lowestNum << " ";
}

Example Output: 





No comments:

Post a Comment