Question

In: Computer Science

C++ Text file contains numbers 92 87 65 49 92 100 100 100 82 75 64...

C++

Text file contains numbers 92 87 65 49 92 100 100 100 82 75 64 55 100 98 -99

Modify your program from Exercise 1 so that it reads the information from the gradfile.txt file, reading until the end of file is encountered. You will need to first retrieve this file from the Lab 7 folder and place it in the same folder as your C++ source code. Run the program

#include <iostream>
using namespace std;


typedef int GradeType[100]; // declares a new data type:
float findAverage (const GradeType, int); // finds average of all grades
int findHighest (const GradeType, int); // finds highest of all grades
int findLowest (const GradeType, int); // finds lowest of all grades

int main()
{
GradeType grades; // the array holding the grades.
int numberOfGrades; // the number of grades read.
int pos; // index to the array.
float avgOfGrades; // contains the average of the grades.
int highestGrade; // contains the highest grade.
int lowestGrade; // contains the lowest grade.
// Read in the values into the array
pos = 0;
cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
cin >> grades[pos];

while (grades[pos] != -99)
{
pos++;
cin >> grades[pos];
}
numberOfGrades = pos--;
// call to the function to find average
avgOfGrades = findAverage(grades, numberOfGrades);
cout << endl << "The average of all the grades is " << avgOfGrades << endl;
  
highestGrade = findHighest(grades, numberOfGrades);
cout << endl << "The highest grade is " << highestGrade << endl;
  
lowestGrade = findLowest(grades, numberOfGrades);
cout << "The lowest grade is " << lowestGrade << endl;

return 0;
}
float findAverage (const GradeType array, int size)
{
float sum = 0; // holds the sum of all the numbers
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];
return (sum / size); //returns the average
}
int findHighest (const GradeType array, int size)
{
int highest = array[0];
for (int pos = 0; pos < size; pos++)
{
if ( highest < array[pos])
{
highest = array[pos];
}
}
return highest;
}
int findLowest (const GradeType array, int size)
{
int lowest = array[0];
for (int pos = 0; pos < size; pos++)
{
if ( lowest > array[pos])
{
lowest = array[pos];
}
}
return lowest;
}

Solutions

Expert Solution

Updated C++ Program:

#include <iostream>
#include <fstream>
using namespace std;

typedef int GradeType[100]; // declares a new data type:
float findAverage (const GradeType, int); // finds average of all grades
int findHighest (const GradeType, int); // finds highest of all grades
int findLowest (const GradeType, int); // finds lowest of all grades

int main()
{
GradeType grades; // the array holding the grades.
int numberOfGrades; // the number of grades read.
int pos; // index to the array.
float avgOfGrades; // contains the average of the grades.
int highestGrade; // contains the highest grade.
int lowestGrade; // contains the lowest grade.

//Opening file for reading
fstream fin("gradfile.txt", ios::in);

// Read in the values into the array
pos = 0;

//Reading first grade
fin >> grades[pos];

//Loop till all the grades are read
while(grades[pos] != -99)
{
pos++;

//Reading from file
fin >> grades[pos];
}

//Closing file
fin.close();

numberOfGrades = pos--;

// call to the function to find average
avgOfGrades = findAverage(grades, numberOfGrades);

cout << endl << "The average of all the grades is " << avgOfGrades << endl;

highestGrade = findHighest(grades, numberOfGrades);
cout << endl << "The highest grade is " << highestGrade << endl;

lowestGrade = findLowest(grades, numberOfGrades);
cout << "The lowest grade is " << lowestGrade << endl;

return 0;
}

float findAverage (const GradeType array, int size)
{
float sum = 0; // holds the sum of all the numbers
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];
return (sum / size); //returns the average
}

int findHighest (const GradeType array, int size)
{
int highest = array[0];
for (int pos = 0; pos < size; pos++)
{
if ( highest < array[pos])
{
highest = array[pos];
}
}
return highest;
}

int findLowest (const GradeType array, int size)
{
int lowest = array[0];
for (int pos = 0; pos < size; pos++)
{
if ( lowest > array[pos])
{
lowest = array[pos];
}
}
return lowest;
}

___________________________________________________________________________________________

Sample Run:


Related Solutions

# R Hypothesis Tests install.packages("dplyr") tScore_before <- c(40, 62, 74, 22, 64, 65, 49, 49, 49)...
# R Hypothesis Tests install.packages("dplyr") tScore_before <- c(40, 62, 74, 22, 64, 65, 49, 49, 49) tScore_after <- c(68, 61, 64, 76, 90, 75, 66, 60, 63) # Create a data frame my_data <- data.frame(                 group = rep(c("Score Before", "Score After"), each = 9),                 scores = c(tScore_before, tScore_after)                 ) # Print all data print(my_data) #Compute summary statistics by groups library(dplyr) group_by(my_data, group) %>% summarise(     count = n(),     mean = mean(scores, na.rm = TRUE),     sd...
C++ You are to input the following numbers from a file: 75 80 90 95 100...
C++ You are to input the following numbers from a file: 75 80 90 95 100 100 70 30 20 70 Place them into a linked list (Like the one I uploaded on this homework). The file I uploaded shows how to sort the numbers using bubble sort. You are to sort them based on selection sort! Only sort the values, do not modify the pointers. Use the file I uploaded as a good guide! #include <iostream> #include <fstream> using...
in c++ (Sum, average and product of numbers in a file) Suppose that a text file...
in c++ (Sum, average and product of numbers in a file) Suppose that a text file Exercise13_3.txt contains six integers. Write a program that reads integers from the file and displays their sum, average and product. Integers are separated by blanks. Instead of displaying the results on the screen, send the results to an output named using your last name. Example:       Contents of Exercise13_3.txt: 100 95 88 97 71 67 80 81 82             Contents of YourLastName.txt: Your...
An HTML file is a text file that contains text to be displayed in a browser...
An HTML file is a text file that contains text to be displayed in a browser and __________ to be interpreted (read) by the browser formatting and styling the document Both C++ and javascript are computer programing languages; what are their differences? What HTML tag can let you insert javascript in to document Which attributes of the <script> tag tells the brorwser what kind of scripting language is insterted? (give an example) in the javascript section of the HTML file,...
For the following data 86 84 91 75 78 80 74 75 87 76 82 90...
For the following data 86 84 91 75 78 80 74 75 87 76 82 90 98   68 What is the mean?    ______ What is the median?_________ What is the mode (if any)? _____ What is the standard deviation? ____ What is the variance? ______ What is the five-number summary for this data? Draw a boxplot with reasonable attention to scale. Create a stemplot of this data. Using the same data set and ignoring the fact that the 5-number summary...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
Consider the following list of ages. 72, 64, 82, 81, 84, 51, 81, 64, 75, 53,...
Consider the following list of ages. 72, 64, 82, 81, 84, 51, 81, 64, 75, 53, 98, 78, 71, 35, 99, 88, 82, 52, 74, 86, 88, 74, 94, 80, 52, 76, 70, 74, 64, 83, 95 (a) Create a five-number summary for these ages. Lowest Value Lowest quartile Median Highest quartile Highest value (b) Create a boxplot using the five-number summary from part (a). The box-and-whisker plot has a horizontal axis numbered from 30 to 100. The box-and-whisker is...
Scores are 92, 68, 88, 96, 72, 88, 80, 64, 74, 92, 100, 84   What is...
Scores are 92, 68, 88, 96, 72, 88, 80, 64, 74, 92, 100, 84   What is the IQR? 19 19.5 20.5 20 6
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT