Question

Write a C++ program that reads data from a file, stores data in a one-dimensional array,...

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.

Homework Answers

Answer #1

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 :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(a) Write a function in C++ called readNumbers() to read data into an array from a...
(a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters: (1) a reference to an ifstream object (2) the number of rows in the file (3) a pointer to an array of doubles The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows is exceeded. (b) Write a program with the...
C++ Write a program which accepts a numeric test score from the user. It will output...
C++ Write a program which accepts a numeric test score from the user. It will output the letter grade to the user corresponding to the score. Assume no “minus grades” (an A ranges from 90-100, B ranges from 80-89, C ranges from 70-79, D ranges from 60-69 and F is everything else.) Ensure that the score remains in the range of 0 to 100. As an example of the output: Enter your numeric grade and I will tell you your...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for...
create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name i.e. jSmith). Next it will need to read three integer values that will represent the 3 e xam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below is an...
Java... Write a class named TestScores. The class constructor should accept an array of test scores...
Java... Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to...
4) Write a C program that reads in a string from the keyboard. Use scanf with...
4) Write a C program that reads in a string from the keyboard. Use scanf with the conversion code %s. Recall that the 2nd arg in scanf should be the address of the location in memory into which the inputted string should be stored. Recall that the name of an array without square brackets is the address of the first slot of the array. 5) Write a program that reads in this file (lab5.txt) and counts and displays the number...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
Movie Structure File Write a program that will read in a CSV file with information regarding...
Movie Structure File Write a program that will read in a CSV file with information regarding movies. The file will contain a movie name, MPAA coding (G, PG, PG-13, R), number of minutes, and ratings (1-10) from the three top critics. Your program will read in the data into an array of structures (No more than 20 movies) and send the array to a function that will produce a professional report file (MovieReport.txt) which includes all of the information, an...
Write a C++ program to read a text file "input.txt" and store the data in form...
Write a C++ program to read a text file "input.txt" and store the data in form of 2d array (or any other data structure you prefer). Input file: a0 400 1 0 20 a1 300 2 0 30 a2 200 3 0 40 a3 100 6 0 40 a4 0 7 0 50
C++ create a program that: in main: -opens the file provided for input (this file is...
C++ create a program that: in main: -opens the file provided for input (this file is titled 'Lab_HW10_Input.txt' and simply has 1-10, with each number on a new line for 10 lines total) -calls a function to determine how many lines are in the file -creates an array of the proper size -calls a function to read the file and populate the array -calls a function to write out the contents of the array in reverse order *output file should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT