Question

C++ programming language Write a program that asks the user for a file name. The file...

C++ programming language

Write a program that asks the user for a file name.
The file contains a series of scores(integers), each written on a separate line.
The program should read the contents of the file into an array and then display the following content:

1) The scores in rows of 10 scores and in sorted in descending order.
2) The lowest score in the array
3) The highest score in the array
4) The total number of scores in the array
5) The average score in the array.
6) The median score in the array.

Program structure should be as follows:

A top level function named TestScoreAnalysis() which accepts a
file of scores to be analyzed and displayed to the console
screen describing the six content items specified above.

-- A function to read data from file into an Array
-- A function to sort an array in descending order

A separate function for each analysis task:
-- The scores in rows of 10 scores and in sorted in descending order.
-- The lowest score in the array
-- The highest score in the array
-- The total number of scores in the array
-- The average score in the array.
-- The median score in the array.

Allocate an array to hold at least 100 scores.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ code

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

//function prototype
void TestScoreAnalysis(ifstream &infile);
// sorted in descending order.
void sort(int arr[], int size);
// The lowest score in the array
int lowestScore(int arr[], int size);
//The highest score in the array
int highestScore(int arr[], int size);
//The total number of scores in the array
int totalScore(int arr[], int size);
//The average score in the array.
double averageScore(int arr[], int size);
// The median score in the array.
double medianScore(int arr[], int size);
void print(int arr[], int size);
//main function
int main()
{
   // asks the user for a file name.
   cout << "Enter file name: ";
   string name;
   cin >> name;
   ifstream infile(name);
   if (!infile)
   {
       cout << "FILE NOT FOUND!" << endl;
       return -1;
   }
   TestScoreAnalysis(infile);
   //pause
   system("pause");
   return 0;
}

//function definition

void TestScoreAnalysis(ifstream &infile)
{
   //Allocate an array to hold at least 100 scores.
   int arr[100];
   //counter
   int c = 0;
   //read score form file
   while (infile>>arr[c])
   {
       //update counter
       c++;
   }
   cout << "Scores:\n";
   print(arr, c);
   cout << "Sorted Array: \n";
   sort(arr, c);
   print(arr, c);
   cout << "Lowest score: " << lowestScore(arr, c) << endl;
   cout << "Highest Score: " << highestScore(arr, c) << endl;
   cout << "Total Score: " << totalScore(arr, c) << endl;
   cout << "Average score: " << averageScore(arr, c) << endl;
   cout << "Median score: " << medianScore(arr, c) << endl;
}
void print(int arr[], int size)
{
   //print 5 numbers in a row
   for (int i = 0; i < size; i++)
   {
       cout << arr[i] << " ";
       if ((i + 1) % 5 == 0)
           cout << endl;
   }
   cout<<endl;
}
void sort(int arr[], int size)
{
   for (int i = 0; i < size; i++)
   {
       for (int j = i + 1; j < size; j++)
       {
           if (arr[i] < arr[j])
           {
               int temp = arr[i];
               arr[i] = arr[j];
               arr[j] = temp;
           }
       }
   }
}
int lowestScore(int arr[], int size)
{
   int min = arr[0];
   for (int i = 0; i < size; i++)
   {
       if (min > arr[i])
           min = arr[i];
   }
   return min;
}
int highestScore(int arr[], int size)
{
   int max = arr[0];
   for (int i = 0; i < size; i++)
   {
       if (max < arr[i])
           max = arr[i];
   }
   return max;
}
int totalScore(int arr[], int size)
{
   int sum = 0;
   for (int i = 0; i < size; i++)
   {
       sum += arr[i];
   }
   return sum;
}
double averageScore(int arr[], int size)
{
   return (double)totalScore(arr, size) / size;
}
double medianScore(int arr[], int size)
{
   double median;
   if (size % 2 == 0)
       median = ((double)arr[size / 2] + (double)arr[size / 2 - 1]) / 2;
   else
       median = (double)arr[size / 2];
   return median;
}

//file

//Output

//If you need any help regarding this solution........ please leave a comment...... thanks

Add a comment
Know the answer?
Add Answer to:
C++ programming language Write a program that asks the user for a file name. The file...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • (C++ programming) Need help with homework. Write a program that can be used to gather statistical...

    (C++ programming) Need help with homework. Write a program that can be used to gather statistical data about the number of hours per week college students play video games. The program should perform the following steps: 1). Read data from the input file into a dynamically allocated array. The first number in the input file, n, represents the number of students that were surveyed. First read this number then use it to dynamically allocate an array of n integers. On...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • In C++ This program will read a group of positive numbers from three files ( not...

    In C++ This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the scores in the order that they were input, showing each number and what percentage it is above or below the average for each file. Note: Finding the average doesn’t require an array. Finding...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • C++ programming language POINTERS Write a program that dynamically allocates an array, on the Heap, large...

    C++ programming language POINTERS Write a program that dynamically allocates an array, on the Heap, large enough to hold 200 test scores between 55 and 99 -- using a Random Number generator to populate the array. Then do the following: 1) Sort scores in ascending order. 2) List your scores in rows of ten(10) values. 3) Calculate the Mean for the distribution. 4) Calculate the Variance for the distribution. 5) Calculate the Median for the distribution. 6) Calculate the Mode...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT