Question
Exercise 3: Modify your program from Exercise 1 so that it reads the information from the gradfile.txt file, reading until the end of file is
Exercise 3: 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.
// This program will read in a group of test scores (positive integers from 1 to 100)
// from the keyboard and then calculate and output the average score
// as well as the highest and lowest score. There will be a maximum of 100 scores.
//
#include
#include
#include
using namespace std;
typedef int GradeType[100]; // declares a new data type:
// an integer array of 100 elements
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.
fstream dataFile;
dataFile.open("gradfile.txt", ios::out | ios::app | ios::binary);
if (!dataFile)
{
cout
exit(1);
}
// Read in the values into the array
pos = 0;
dataFile >> grades[pos];
while (grades[pos] != -99)
{
pos++;
dataFile >> grades[pos];
}
numberOfGrades = pos;
// call to the function to find average
avgOfGrades = findAverage(grades, numberOfGrades);
cout
highestGrade = findHighest(grades, numberOfGrades);
cout
lowestGrade = findLowest(grades, numberOfGrades);
cout
system("pause");
return 0;
}
//****************************************************************************
// findAverage
//
// task: This function receives an array of integers and its size.
// It finds and returns the average of the numbers in the array
// data in: array of floating point numbers
// data returned: avarage of the numbers in the array
//
//****************************************************************************
float findAverage(const GradeType array, int size)
{
float sum = 0; // holds the sum of all the numbers
for (int pos = 0; pos
sum = sum + array[pos];
return (sum / size); //returns the average
}
//****************************************************************************
// findHighest
//
// task: This function receives an array of integers and its size.
// It finds and returns the highest value of the numbers in
// the array
// data in: array of floating point numbers
// data returned: highest value of the numbers in the array
//
//****************************************************************************
int findHighest(const GradeType array, int size)
{
int highestgrade = array[0];
for (int pos = 0; pos
if (array[pos] > highestgrade)
{
highestgrade = array[pos];
}
return highestgrade;
}
//****************************************************************************
// findLowest
//
// task: This function receives an array of integers and its size.
// It finds and returns the lowest value of the numbers in
// the array
// data in: array of floating point numbers
// data returned: lowest value of the numbers in the array
//
//****************************************************************************
int findLowest(const GradeType array, int size)
{
int lowestgrade = array[0];
for (int pos = 0; pos > size; pos++)
if (array[pos]
{
lowestgrade = array[pos];
}
return lowestgrade;
}
Please run your code before answer, When I run this on UNIX it showed Segmentation fault (core dumped).
gradfile.txt:
csci2.stcloudstate.edu - PuTTY gradfile.txt testscore.cpp csci2>g++-o testscore testscore.cpp testscore.cpp: In function 'int main)': testscore.cpp:37:9: erroexit' was not declared in this scope testscore.cpp:72:16: error system' was not declared in this scope csci2>g++-o testscore testscore.cpp testscore.cpp: In function 'int main)': testscore.cpp:37:9: erroexit' was not declared in this scope testscore.cpp:72:16: error system' was not declared in this scope csci2>g++-o testscore testscore.cpp testscore.cpp: In function 'int main)': testscore.cpp:37:9: erroexit' was not declared in this scope testscore.cpp:72:16: error system' was not declared in this scope csci2>g++-o testscore testscore.cpp sci2>testscore Segmentation fault (core dumped) csci2 csci2 csci2 sci2>testscore Segmentation fault (core dumped)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