Write a C++ program that reads data from a file, stores data in a one-dimensional array, and processes data as required.
1) Create a text file named “hw2data.txt” that contains student test scores in the range of 0~100. You can use the following sample data: 76.5 84 75 62 94.5 75.7 93 54 36.5 87.3 81.5 89.5 68 90 79.5 80 92.5 84 96 100 67.7 78 84.5 47 100 87.5 76.25 87 82.5 64
2) Read test scores from the file “hw2data.txt” and store them in a one-dimensional array. Your program should be able to read a flexible number of scores, assuming the total number of scores is no more than 200.
3) Write a function printList that prints the content of an array on the screen. The function has two formal parameters: a double array and an int variable containing the size of the array.
4) Write a function getLargest that finds and returns the largest value (first occurrence if more than one) in an array.
5) Write a function getSmallest that finds and returns the smallest value (first occurrence if more than one) in an array.
6) Write a function getAverage that finds and returns the average value of an array.
7) Write a function getCount that finds and returns the number of times the values of an array fall in a range. This function has four formal parameters: a double array, an int variable containing the size of the array, an int variable for the lower bound of a range, and an int variable for the upper bound of the range.
8) In the function main, call the above functions to output: - all the test scores read from the file - the number of scores read from the file - the highest test score - the lowest test score - the average test score (show 2 decimal places).
9) In the function main, call the getCount function to find the number of scores in each of the following ranges: (1) 0 - 60 (0 <= score < 60, including lower bound but excluding upper bound) (2) 60 - 70 (60<= score < 70) (3) 70 – 80 (70<= score < 80) (4) 80 – 90 (80<= score < 90) (5) 90-100 (90 <= score <=100. Please note that 100 is included in this range) Output the score ranges and the number of scores in each range.
If you run your program with the above sample data, you should have results same as follows:
Range Number of Scores
0 -- 60 3
60 -- 70 4
70 -- 80 6
80 -- 90 10
90 -- 100 7
A different data file will be used to test and grade your program.
C++ Code :
#include<iostream>
#include <fstream>
// Input/output stream class to operate on
files
using namespace std;
void printList(double arr[],int size)
// printList function() , it
will print the content of the array
{
cout<<"\nContent of the array : ";
for(int i=0;i<size;i++)
cout<<arr[i]<<" ";
//
printing the content of the array
}
double getSmallest(double arr[],int size)
// This
function will return the smallest value in the array
{
double min = 99999.0;
// initially, assinging a maximum value for
finding the smallest value in the array
for(int i=0;i<size;i++)
{
if(arr[i]<min)
// finding smallest
value
min = arr[i];
}
return min;
// returning the smallest
value in the array
}
double getLargest(double arr[],int size)
// This
function will return the largest value in the array
{
double max = -999.0;
// initially, assinging a minimum value for
finding the smallest value in the array
for(int i=0;i<size;i++)
{
if(arr[i]>max)
// finding largest
value
max = arr[i];
}
return max;
// returning the largest
value in the array
}
int getCount(double arr[],int size,int lowerbound,int
upperbound) //
this function will return the number of times the values of an
array fall in a range
{
int count = 0;
// for counting the number of
times
for(int i=0;i<size;i++)
{
if(upperbound!=100)
// this is
the case when upperbound is not equals to 100
{
if(lowerbound<=arr[i] &&
arr[i]<upperbound)
// if upperbound is not equal
to 100, we'll exclude the upperbound
count++;
// incrementing the count by 1 if we found a
match
}
else
// this is for special case
when upperbound is equals to 100
{
if(lowerbound<=arr[i] &&
arr[i]<=upperbound) // if
upperbound is equal to 100, we'll include the upperbound
count++;
// incrementing the count by 1 if we found a
match
}
}
return count;
// Returning the number of times the values of
an array fall in a range
}
double getAverage(double arr[],int size)
// this
function will return the average of the array values
{
double sum = 0.0;
// initially the sum is
0.0
for(int i=0;i<size;i++)
sum += arr[i];
// adding all the array
values for finding the average
double average = sum/(size*1.0);
//
calculating average of the array values
return average;
// returning the average of the array
values
}
int main()
{
double n;
// this variable will store
the array values one by one
int size = 0;
// for counting the number of elements in the
hw2data.txt
int index = 0;
// this variable will store
the index value of the array
double ar[200];
// declaring the array of
size 200 , because assuming the total number of scores is no more
than 200
ifstream read("hw2data.txt");
// reading the hw2data.txt
file
while(read>>n)
// till the input ( score ) is present in the
file
{
ar[index] = n;
// storing
the scores in the array
size++;
//
counting scores in the file
index++;
//
incrementing the index of the array after storing the score in the
array
}
printList(ar,size);
// calling printList function for printing the
content of the array
cout<<"\n\n";
double largest_value =
getLargest(ar,size);
// calling getLargest() for
getting the largest value in the array
cout<<"Largest Value =
"<<largest_value<<"\n\n";
//
printing the largest value in the array
double smallest_value =
getSmallest(ar,size);
// calling getSmallest() for
getting the smallest value in the array
cout<<"Smallest Value =
"<<smallest_value<<"\n\n";
//
printing the smallest value in the array
double average = getAverage(ar,size);
// calling
getAverage() for getting the average of the array
cout<<"Average Value =
"<<average<<"\n\n";
// printing the average value
of the array
double count1 = getCount(ar,size,0,60);
// calling getCount for the
range 0-60
cout<<"0 -- 60 :
"<<count1<<"\n\n";
// printing the count for the
range 0-60
double count2 = getCount(ar,size,60,70);
// calling getCount for the
range 60-70
cout<<"60 -- 70 :
"<<count2<<"\n\n";
// printing the count for the
range 60-70
double count3 = getCount(ar,size,70,80);
// calling getCount for the
range 70-80
cout<<"70 -- 80 :
"<<count3<<"\n\n";
// printing the count for the
range 70-80
double count4 = getCount(ar,size,80,90);
// calling getCount for the
range 80-90
cout<<"80 -- 90 :
"<<count4<<"\n\n";
// printing the count for the
range 80-90
double count5 = getCount(ar,size,90,100);
// calling getCount for the
range 90-100
cout<<"90 -- 100 :
"<<count5<<"\n\n";
// printing the count for the
range 90-100
}
Output :
Get Answers For Free
Most questions answered within 1 hours.