Question
need help C++ //****************************************************************** // This program calculates the average, high score, low score, // number above the average, and number below the average for
need help 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
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
//*********************************************************************
void OpenFiles(ifstream& text, ofstream& outFile ) { string inFileName; string outFileName; cout > inFileName; text.open(inFileName.c_str()); cout > outFileName; outFile.open(outFileName.c_str()); outFile
//*********************************************************************
void CalculateAverage(GradeStatistics& statistics ) { int sum = 0; // Sum number of grades times index for (int index = 0; index
//*********************************************************************
void InputGrades(GradeStatistics& statistics, ifstream& inData) { int grade; // Zero out the array of counters for (int index = 0; index > 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 CalculateBelowAverage(GradeStatistics& statistics) { int truncatedAverage = (int) (statistics.average); int index; int number = 0; // Sum number of grades below average for (index = 0; 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
Test numbers
2 4 6 8 9 55 88 99 32 12 23 45 55 66 77 88 99 100 99 98 2 4 6 8 9 55 88 99 32 12 23 45 55 66 77 88 99 100 99 98 2 4 6 8 9 55 88 99 32 12 23 45 55 66 77 88 99 100 99 98 2 4 6 8 9 55 88 99 32 12 23 45 55 66 77
style
//****************************************************************** // 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
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; }
Exercise1: 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 will be a counter for the number of times 'A' and 'a' occur; position 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 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started