Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ //****************************************************************** // This program calculates the average, high score, low score, // number above the average, and number below the average for // a

c++

//****************************************************************** // This program calculates the average, high score, low score, // number above the average, and number below the average for // a file of test scores. // Assumption: File contains at least one non-zero value //****************************************************************** #include #include #include using namespace std; struct GradeStatistics { int numGrades; // Number of grades float average; // Average grade int highest; // Highest grade int lowest; // Lowest grade int aboveAverage; // Number of grades above the average int belowAverage; // Number of grades below the average int grades[101]; // Array of grades };

// Declare function prototypes void CalculateStatistics(GradeStatistics& statistics, ifstream& inData); // Post: Data has been read and statistics calculated. void OpenFiles(ifstream& inData, ofstream& outData); // Post: File names have been prompted for and files are opened. // Input file name has been written on output file void InputGrades(GradeStatistics& statistics, ifstream& inData); // Pre: inData is assigned and not empty // Post: Grades have been read from inData, // numGrades is the number of grades in inData void CalculateAverage(GradeStatistics& statistics); // Post: Average grade has been calculated void CalculateHighest(GradeStatistics& statistics); // Post: Hightest grade has been calculated void CalculateLowest(GradeStatistics& statistics); // Post: Return value is the lowest grade void CalculateAboveAverage(GradeStatistics& statistics); // Post: Number of grades above the average has been calculated void CalculateBelowAverage(GradeStatistics& statistics); // Post: Number of grades below the average has been calculated void PrintResults(GradeStatistics statistics, ofstream& outData); // Pre: outData has been opened // Post: Output has been written on outData int main() { GradeStatistics statistics; // Declare and open files ifstream inData; ofstream outData; OpenFiles(inData, outData); if (!inData || !outData ) { cout > inFileName; text.open(inFileName.c_str()); cout > outFileName; outFile.open(outFileName.c_str()); outFile > grade; // Priming read while (inData) { // Process data statistics.grades[grade]++; statistics.numGrades++; inData >> grade; // Subsequent reads } } //********************************************************************* void CalculateLowest(GradeStatistics& statistics) { // Index of first nonzero grade is the low grade int lowGrade = 0; while (statistics.grades[lowGrade] == 0) lowGrade++; statistics.lowest = lowGrade; } //*********************************************************************

void CalculateHighest(GradeStatistics& statistics) { // Index of first nonzero grade is the low grade int highGrade = 100; while (statistics.grades[highGrade] == 0) highGrade--; statistics.highest = highGrade; } //********************************************************************* void CalculateAboveAverage(GradeStatistics& statistics) { int averagePlus = (int)(statistics.average) + 1; int index; int number = 0; for (index = averagePlus; index

void CalculateStatistics(GradeStatistics& statistics, ifstream& inData) { // Read and process grades InputGrades(statistics, inData); CalculateAverage(statistics); CalculateHighest(statistics); CalculateLowest(statistics); CalculateAboveAverage(statistics); CalculateBelowAverage(statistics); }

//****************************************************************** void PrintResults(GradeStatistics statistics, ofstream& outData) { outData

//****************************************************************** // Style Program // The program calculates number of words, average word length, // number of sentences, average sentence length, number of // uppercase letters, number of lowercase letters, and number of // digits in a file of text. //****************************************************************** #include #include #include #include using namespace std; enum Features {UPPER, LOWER, DIGIT, IGNORE, EOW, EOS};

struct Counters { int uppercase; int lowercase; int digit; int word; int sentence; int ignore; };

// Function prototypes void OpenFile(ifstream&); // This function reads in a file name and opens the file. // If the file is not found, an error code is returned. Features Decode(char character); // This function reads in the names of the input file and the // output file and opens them for processing void IncrementCounters(Counters& counters, char character); // This function increments the appropriate character counter. void PrintTable(Counters counters); // Table is printed void InitializeCounters(Counters& counters); // This function prints the resulting cout int main() { // Prepare files for reading and writing ifstream text; Counters counters; OpenFile(text); if (!text) { cout > inFileName; text.open(inFileName.c_str()); cout

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

void InitializeCounters(Counters& counters) { counters.uppercase = 0; counters.lowercase = 0; counters.digit = 0; counters.word = 0; counters.sentence = 0; counters.ignore = 0; }

image text in transcribed

Exercise 1: Compile and run program Statistics (statistics.cpp), using the data on file testscores.dat. Compare your result with those in the text. Rather than work on program Statistics, you will go back to the original program Style and enhance it using the techniques demonstrated in program Statistics. Exercise 2: Bring program Style up on the screen and refamiliarize yourself with it. In Chapter 10, you added counts for such things as unusual letters, vowels, and consonants. You are going to count the same characteristics using the "indexes with semantic content" technique. You are going to declare a 26-item integer array in which the zeroth position wll be a counter for the number of times 'A' and 'a' occur; position 1 will be a counter for the number of times 'B' and 'b' occur and so forth. Use the following table to count the characters in this exercise description. Ignore all nonalphabetic characters. Index Count IndexCount Index Count 0 0 Exercise 3: Write the code to declare an integer array charCounter with 26 places, declare an integer variable index, and zero out the array. Exercise 1: Compile and run program Statistics (statistics.cpp), using the data on file testscores.dat. Compare your result with those in the text. Rather than work on program Statistics, you will go back to the original program Style and enhance it using the techniques demonstrated in program Statistics. Exercise 2: Bring program Style up on the screen and refamiliarize yourself with it. In Chapter 10, you added counts for such things as unusual letters, vowels, and consonants. You are going to count the same characteristics using the "indexes with semantic content" technique. You are going to declare a 26-item integer array in which the zeroth position wll be a counter for the number of times 'A' and 'a' occur; position 1 will be a counter for the number of times 'B' and 'b' occur and so forth. Use the following table to count the characters in this exercise description. Ignore all nonalphabetic characters. Index Count IndexCount Index Count 0 0 Exercise 3: Write the code to declare an integer array charCounter with 26 places, declare an integer variable index, and zero out the array

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions

Question

3. Explain the forces that influence how people handle conflict

Answered: 1 week ago