Question

Please write a C++ program to read a list of numbers from a datafile, and calculate...

Please write a C++ program to read a list of numbers from a datafile, and calculate the average, lowest and largest number.


Please use the following command to copy the datafile scores.txt to your home directory:

Please use end-of-file loop to read all the floating-point numbers from the datafile scores.txt.
Please display the average, lowest and largest number of all the numbers in the datafile.
Please create a function to process the datafile, and call the dataProcessing function in the main function.

Homework Answers

Answer #1

/**********************************************/

Where we have to save the input file ..???

/**********************************************/

// scores.txt (Input file)

78
89
98
87
76
65
55
99
87
54

/**********************************************/

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
// function declarations
void dataProcessing(double nos[], int total_num, double& min, double& max, double& avg);
int main()
{
// Declaring variables
int total_num=0;
double min, max;
double avg;
string filename="scores.txt";
// defines an input stream for the data file
ifstream dataIn;
  
double num;
dataIn.open(filename.c_str());
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
//Reading the data from the file
while(dataIn>>num)
{
total_num++;
}
dataIn.close();
  
// Creating array dynamically
double* nos = new double[total_num];
  
dataIn.open(filename.c_str());
for(int i=0;i<total_num;i++)
{
dataIn>>num;
nos[i]=num;
}
dataIn.close();
  
dataProcessing(nos, total_num, min, max, avg);
  

// Displaying the output
cout << "Minimum :" << min << endl;
cout << "Maximum :" << max << endl;
cout << "Average :" << avg << endl;
}
  
  

return 0;
}
// This function will find out the minimum , maximum and average score in array
void dataProcessing(double nos[], int total_num, double& min, double& max, double& avg)
{
min=nos[0];
max=nos[0];
double sum=0;
for(int i=0;i<total_num;i++)
{
if(min>nos[i])
{
min=nos[i];
}
if(max<nos[i])
{
max=nos[i];
}
sum+=nos[i];
}
avg=sum/total_num;
}

/***********************************************/

/***********************************************/

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
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should...
PLease code a C++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the 10 numbers and the average of the 10 numbers please use file i/o and input measures for Handling Errors in C++ When Opening a File
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
PLEASE DO QUICK LINUX ASSIGNMENT PLEASE ILL THUMBS UP You need to paste the command and...
PLEASE DO QUICK LINUX ASSIGNMENT PLEASE ILL THUMBS UP You need to paste the command and the output in a word document and submit it. Part1: 1. Log in to Linux using your user account name and password. 2. If you logged in using a graphical login screen, open a terminal window by clicking on the icon in the lower left corner of the desktop to open the main menu, then selecting System Tools, then Terminal. A terminal window opens....
JAVA ANSWER IN JAVA PLEASE.. ReadFile Create a linked list from an input file (input.txt) that...
JAVA ANSWER IN JAVA PLEASE.. ReadFile Create a linked list from an input file (input.txt) that contains an even number of first names. The number of items in the file is unknown. SplitMerge Create a split function that divides the newly created linked list into two equal sublists: myList1 and myList2. For example, originally you would point to (John, Jack, Jill, Jim). After the split, myList1 would point to john and jack and myList2 would point to jill and Jim....
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 55000 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
Please write C++ program. Fibonacci numbers have the property that each consecutive number is a sum...
Please write C++ program. Fibonacci numbers have the property that each consecutive number is a sum of two preceding: 0, 1, 1, 2, 3, 5, 8, 13, 21 ..... Your program should ask user for the integer, and output the result, which shows Fibonacci number at that position. For example if user enters '5', program should output '3' (fifth position in the series). Your program should use recursive function to compute and return back to the main the Fibonacci number....
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
Using PUTTY We will create functions.h and functions.cpp to separate the read and write function from...
Using PUTTY We will create functions.h and functions.cpp to separate the read and write function from main function. Use the following steps:o Create a directory called lab07o Upload the yourlastnameFirstnameLab06a.cpp from your computer to the csegrid. Create a functions.h file with the following: #ifndef _READWRITE #define _READWRITE void readWrite( ); #endif Create a functions.cpp#include functions.hImplement readWrite() with the code in case 1 of the main.cpp. Modify yourlastnameFirstnameLab06a.cpp #include functions.h Call readWrite for case 1 (instead of the case1 code) Compile...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that...
Machine Problem 3 - Linked List C++ For this assignment you will write a program that inserts 20 random integers from 0 to 100 in order in a linked list object. The program will create another linked list, but with 15 random integers from 0 – 100 in order. The program then will merge those two ordered linked list into a single ordered list. The function merge should receive references to each of the list objects to be merged and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT