A Solution to Lab #5

/////////////////////////////////////////////////////////
// Student Name: Viv Savage
// Student ID  : 123-45-6789
// Course      : CS 161
// Assignment  : #5
// Program File: program5.cpp
// Due Date    : 8-7-97
//
// Description : This program prompts the user to enter a string of 
//               characters which is then analyzed. A table is
//               displayed indicating the count of occurrences of
//               each letter, its frequency in the string, and the
//               total number of letters in the string:
//
//              Character     Occurrences    Frequency
//              ---------------------------------------
//                  A             42           8.42%
//                  B              6           1.20%
//                  C             16           3.21%
//                  .              .            .
//                  .              .            .
//                  .              .            .
//                  Y              5           1.00%
//                  Z              0           0.00%
//
//              Total number of letters: 499
//
/////////////////////////////////////////////////////////

#include <iostream.h> // cin/cout
#include <iomanip.h>  // manipulators
#include <ctype.h>    // toupper(), isalpha()

////////////////////////////////////////////////////////////////////
// Function: intializeArray
//
//  Inputs: The size of the array being passed in
// Outputs: An array whose elements are all 0
//
void initializeArray(int array[], int arraySize);

////////////////////////////////////////////////////////////////////
// Function: getText
//
//  Inputs: The maximum length of input
// Outputs: An array containing the user's input
//
void getText(char text[], int length);

////////////////////////////////////////////////////////////////////
// Function: calculateResults
//
//  Inputs: An array representing the user's input
// Outputs: An array containing the count of each character
//          An array containing the frequency of each character
//
void calculateResults(const char text[], int letters[], float frequencies[]);

////////////////////////////////////////////////////////////////////
// Function: printResults
//
//  Inputs: An array containing the count of each character
//          An array containing the frequency of each character
//          The size of each array (they are the same)
// Outputs: None
//
void printResults(int letters[], float frequencies[], int size);

// largest input of characters
const int MAX_CHARACTERS = 1000;

// number of letters we are counting (A - Z)
const int NUMBER_OF_LETTERS = 26;


/////////////////////////////////////////////////////////////
// Function: main
//
// Call initializeArray with size of array returning array of zeroes
// Call getText with max size returning string containing user's input
// Call calculateResults with input string returning counts, frequencies
// Call printResults with counts, frequencies, and size of arrays
void main(void)
{
  // array to hold the input characters
  char text[MAX_CHARACTERS + 1];

  // array to hold the count for each letter
  int letters[NUMBER_OF_LETTERS];

  // array to hold the frequency of each letter
  float frequencies[NUMBER_OF_LETTERS];

  // set all entries in both arrays to 0
  initializeArray(letters, NUMBER_OF_LETTERS);

  // get input from user
  getText(text, MAX_CHARACTERS);

  // count the letters in the input and calculate frequencies
  calculateResults(text, letters, frequencies);

  // print the results
  printResults(letters, frequencies, NUMBER_OF_LETTERS);
}


/////////////////////////////////////////////////////////////
// Function: getText
//
// Prompt user for string
// Get string (max size is length)
//
void getText(char text[], int length)
{
  cout << "Enter a string of text (1000 chars. max):" << endl;
  cin.get(text, length);
}


/////////////////////////////////////////////////////////////
// Function: calculateResults
//
// Set count to 0
// Set sum to 0
// While the character indexed by count is not the NULL character
//   If indexed character is a letter
//     Convert indexed character to uppercase
//     Increment this character's count by one
//     Increment sum by one
//   End If
//   Increment count by one
// End While
// For i from 0 to NUMBER_OF_LETTERS
//   Calculate frequency of indexed character
//   Set frequency indexed by i to result
// End For
//
void calculateResults(const char text[], int letters[], float frequencies[])
{
  // set count to start at beginning of text
  int count = 0;

  // total number of letters
  int sum = 0;

  // While there are more characters to process
  while (text[count] != 0)
  {
  
    // if it's a letter (A..Z or a..z)
    if (isalpha(text[count]))
    {
      // convert it to uppercase
      char letter = toupper(text[count]);

      // increment this letter in the array
      letters[letter - 65]++;

      // increment total number of letters
      sum++;
    }

    // next character in text
    count++;
  }

  // calculate frequency of each character
  for (int i = 0; i < NUMBER_OF_LETTERS; i++)
    frequencies[i] = float(letters[i]) / sum * 100;

}


/////////////////////////////////////////////////////////////
// Function: printResults
//
// Set total count of letters to 0
// Display header text
// Set output precision and formatting
// For i from 0 to size of arrays
//   Display current letter
//   Display count for current letter
//   Display frequency for current letter
//   Display a new line
//   Add this count to total count of letters
// End For
// Display total count of letters
void printResults(int letters[], float frequencies[], int size)
{
  int i;
  int sum = 0;

  // display header
  cout << "Character     Occurrences    Percentage" << endl;
  cout << "---------------------------------------" << endl;

  // set precision and formatting
  cout << setprecision(2);
  cout.setf(ios::fixed | ios::showpoint);

  // display each character, count, and frequency
  for (i = 0; i < size; i++)
  {
    cout << setw(5)  << char(i + 65);
    cout << setw(15) << letters[i];
    cout << setw(15) << frequencies[i] << "%";
    cout << endl;

    // keep a running total
    sum += letters[i];
  }
  cout << endl;
  cout << "Total characters: " << sum << endl;
}

/////////////////////////////////////////////////////////////
// Function: initializeArray
//
// For i from 0 to size of array
//   Set element indexed by i to 0
// End For
//
void initializeArray(int array[], int arraySize)
{
  // set all elements in the array to 0
  for (int i = 0; i < arraySize; i++)
    array[i] = 0;
}
Back to Outline