Question

Write a program in C++ that reads in integer numbers from a file called scores.txt until...

Write a program in C++ that reads in integer numbers from a file called scores.txt until the sentinel value -999 is read. The program should then output the total and average of the numbers read and output each of the numbers incremented by the overall average to a file called or scoresout.txt along with the sentinel value of -999 at the end

So if 10, 20, 30 and -999 are read in then the program would display a total of 60 and an average of 20 then output to the scoresout.dat file 30, 40, 50 and -999.

Hint: So read input file, compute the total and the average (put that into a variable) then close the input file. After that, re-open the input file, open the output file, take each number from the input file, add the average to it then output it to the output file.

scores.txt content

10
20
30
40
50
60
-999

scoresout.txt

45
55
65
75
85
95
-999

Please for some comments in the code explaining the process and no need for full file directories. The file name will be enough.

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

Here is code:

#include <iostream>

#include <fstream>

using namespace std;

int *expander(int ex_arr[], int counter, int value)

{

int *new_arr = (int *)malloc(sizeof(int) * (counter + 1));

int i;

for (i = 0; i < counter; i++)

{

new_arr[i] = ex_arr[i];

}

new_arr[i] = value;

return new_arr;

}

void printArray(int *array, int size)

{

fstream fout("files\\scoresout.txt", ios::out);

for (int i = 0; i < size; i++)

{

fout << array[i] << endl;

}

fout.close();

}

int main()

{

int *array;

fstream inFile("files\\scores.txt", ios_base::in);

int a;

int count = 0;

// loop the file numbers

while (inFile >> a && a != -999)

{

array = expander(array, count++, a);

}

int sum = 0;

double avg = 0;

for (int i = 0; i < count; i++)

{

sum += array[i];

}

avg = (double)sum / count;

for (int i = 0; i < count; i++)

{

array[i] += avg;

}

printArray(array, count);

inFile.close();

cout << "Done\n";

return 0;

}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a program in C++ that reads in integer numbers from a file called scores.txt until...
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
  • using c++ write a program that reads numbers from the user until the user enters a...

    using c++ write a program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: 1. Output the sum of all even numbers

  • using c++ write a program that reads numbers from the user until the user enters a...

    using c++ write a program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following:   must use loops and numbers to do this 1. Output the sum of all even numbers 2. Output the sum of all odd numbers 3. Output the count of all even numbers 4. Output the count of all odd numbers

  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • Write a program file_max.c that reads a set of integer numbers from a file and prints...

    Write a program file_max.c that reads a set of integer numbers from a file and prints out the maximum number to standard output. The name of the input file should be specified as a command line argument.

  • 1. Write a program called Numbers that a. prompts the user for a file name. b....

    1. Write a program called Numbers that a. prompts the user for a file name. b. reads that file assuming that its contents consist entirely of integers. c. prints the maximum, minimum, sum, count (number of integers in the file), and average of the numbers. For example, if the file numberinput.dat has the following content: 4 -2 18 15 31 27 Your program should produce the following output: csc% java Numbers Enter file name: numberinput.daft Maximum31 Minimum- -2 Sum -...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Write a program that reads the numbers from the file and totals the numbers. The program...

    Write a program that reads the numbers from the file and totals the numbers. The program should print all the numbers and display the total when all the numbers have been added together. (Warning! The input from the file will be considered a string. Be sure to convert the input to int or float – just as you do when numbers are entered from the keyboard.) numbers.txt has this list of numbers 75 78 91 55 99 85 63 81...

  • Write a java program that reads a given data file called tickets that consists of tickets...

    Write a java program that reads a given data file called tickets that consists of tickets and the ticket prices. Your program should read the data and compute the find minimum, maximum and average ticket prices and output the report into a file called output.txt. The report file should look exactly (or better than) the one shown below. Your program should implement the Java ArrayList class, Java File I/O, and Java error handling exception. Below is the content of the...

  • Write a PYTHON program that reads a file (prompt user for the input file name) containing...

    Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file: 1   0.5 2   0.5 3   0.5 4   0.5 The output should be:    The averages are 2.50 and 0.5. a)   Your code with comments b)   A screenshot of the execution Version 3.7.2

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