Question

In: Mechanical Engineering

in c++ (Sum, average and product of numbers in a file) Suppose that a text file...

in c++ (Sum, average and product of numbers in a file) Suppose that a text file Exercise13_3.txt contains six integers. Write a program that reads integers from the file and displays their sum, average and product. Integers are separated by blanks.

Instead of displaying the results on the screen, send the results to an output named using your last name.

Example:

      Contents of Exercise13_3.txt:

100 95 88 97 71 67 80 81 82

     

      Contents of YourLastName.txt:

Your first and last name

Total = 761

Average = 84.6

     

Run the program for the example above and for another example with 15 scores.

Solutions

Expert Solution

#include <iostream>

#include <fstream>

#include <sstream>

#include<string>

using namespace std;

int main()

{

ifstream in("Exercise13_3.txt");

if(!in)

{

cout << "Cannot open input file.\n";

}

else

{

ofstream out;

out.open("danger.txt");

string str;

size_t pos = 0;

string sub;

float sum;

float product;

float average;

float val;

string delimiter = " ";

int count = 0;

while(getline(in,str))

{

cout << str << endl;

sum = 0.0;

product = 1.0;

average = 0.0;

count = 0;

while ((pos = str.find(delimiter)) != std::string::npos)

{

sub = str.substr(0, pos);

stringstream sub_value(sub);

sub_value >> val;

str.erase(0, pos + delimiter.length());

sum+=val;

product*=val;

count+=1;

}

stringstream last_value(str);

last_value >> val;

count++;

sum+=val;

product*=val;

average = sum/count;

cout<<"sum = "<<sum<<endl;

cout<<"product = "<<product<<endl;

cout<<"average = "<<average<<endl;

ostringstream str1;

str1<<sum;

string sum1 = str1.str();

out << "Total = " << sum1 <<"\n";

str1<<product;

string product1 = str1.str();

out << "Product = " << product1 <<"\n";

str1<<average;

string average1 = str1.str();

out << "Average = " << average1 <<"\n";

}

out.close();

}

in.close();

return 0;

}


Related Solutions

Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
The sum of two numbers is 34. a)Find the largest possible product of these numbers.
  1-The sum of two numbers is 34.    a)Find the largest possible product of these numbers.    b)What would be the largest possible product if the sum if the two numbers were "k"? 2-Sixty meters of fencing are used to fence a rectangular garden.    a)Find the dimensions that will give that maximum area.    b)What would be the maximum area if "k" feet of fencing were used in terms of "k"? THANK YOU
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Language C: Suppose you are given a file containing a list of names and phone numbers...
Language C: Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
how to creat Create a text file that contains a list of integer numbers separated by...
how to creat Create a text file that contains a list of integer numbers separated by comma (numbers.txt). E.g. 3, 7, 5, 1, 11, 8,2, 6 2- how to Write a python program (minMax.py) to read the file numbers.txt and find the largest and smallest numbers in the file. (without using use min(), max()). calculate the average and output the results. 3-Use python file i/o, write a python program (fileCopy.py) that makes a copy of minMax.py (Read contents of minMax.cp...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT