Question
C++
3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning fu
1 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

inFile.txt (Input file):

42
468
335
501
170
725
479
359
963
465
706
146
282
828
962
492
996
943
828
437
392
605
903
154
293
383
422
717
719
896
448
727
772
539
870
913
668
300
36
895
704
812
323
334
674
665
142
712
254
869
548
645
663
758
38
860
724
742
530
779
317
36
191
843
289
107
41
943
265
649
447
806
891
730
371
351
7
102
394
549
630
624
85
955
757
841
967
377
932
309
945
440
627
324
538
539
119
83
930
542
834
116
640
659
705
931
978
307
674
387
22
746
925
73
271
830
778
574
98
513
987
291
162
637
356
768
656
575
32
53
351
151
942
725
967
431
108
192
8
338
458
288
754
384
946
910
210
759
222
589
423
947
507
31
414
169
901
592
763
656
411
360
625
538
549
484
596
42
603
351
292
837
375
21
597
22
349
200
669
485
282
735
54
1000
419
939
901
789
128
468
729
894
649
484
808
422
311
618
814
515

============================

We have to store the input file in bin folder C:\Program Files (x86)\Dev-Cpp MinGW64\bin As I installed Dev C++ in C Drive

===========================

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;

void readFile(int &sum,int &cnt,double &avg,ifstream &DataIn);
bool isFileAvailable(ifstream &DataIn);
int main() {
//Declaring variables
int cnt=0,sum=0;
double avg=0.0;

ifstream DataIn;
   DataIn.open("inFile.txt");
  
bool b=isFileAvailable(DataIn);
if(b)
   {
       readFile(sum,cnt,avg,DataIn);
   cout<<"No of Numbers in the file :"<<cnt<<endl;
cout<<"Sum of Numbers in the file :"<<sum<<endl;
cout<<"Average of Numbers :"<<avg<<endl;
   }
   else
   {
      cout<<"** File Not Found **"<<endl;
   }
  
return 0;
}

bool isFileAvailable(ifstream &DataIn)
{

//checking whether the file name is valid or not
if(DataIn.fail())
{
return 0;
}
else
{
   return 1;
}
}

void readFile(int &sum,int &cnt,double &avg,ifstream &DataIn)
{
   
    int num;
    //Reading the data from the file
while(DataIn>>num)
{
  
cnt++;
sum+=num;
}
DataIn.close();
  
avg=((double)sum)/cnt;
  
}

==========================================

Output:

C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\ReadFileCalculate Sum Average.exe No of Numbers in the file :200 Sum of Numbers in

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
C++ 3. Write a program that reads integers from a file, sums the values and calculates...
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
  • a. Provide me with your code file, output file and the text file. 1. Create a...

    a. Provide me with your code file, output file and the text file. 1. Create a file with a series of integers. Save it as numbers. txt. Write a program that reads all the numbers and calculates their sum . 2. Create a file having different integers than the first one. Save it as numbers1. txt . Write a program that reads all the number and calculates their average. Important: The two files that you are creating will contain different...

  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

  • Write a program that will take input from a file of numbers of type double and...

    Write a program that will take input from a file of numbers of type double and output the average of the numbers in the file to the screen. Output the file name and average. Allow the user to process multiple files in one run. Part A use an array to hold the values read from the file modify your average function so that it receives an array as input parameter, averages values in an array and returns the average Part...

  • Write a program that reads two integer values. It then calculates and displays the sum and...

    Write a program that reads two integer values. It then calculates and displays the sum and average of all values between them, Le. if the first value is vi and the second value is 2, then the program calculates and displays the sum and average of all values in the closed range Iv1,2 Your Program must satisfy the following constraints: A. make sure that v1 is less than 2 B. Use a function named getStats(that takes two integer yalues as...

  • Write in C++ using emacs. Write a program that calculates the ending balance of several savings...

    Write in C++ using emacs. Write a program that calculates the ending balance of several savings accounts. The program reads information about different customers’ accounts from an input file, “accounts.txt”. Each row in the file contains account information for one customer. This includes: the account number, account type (premium, choice, or basic account), starting balance, total amount deposited, and total amount withdrawn. Your program should: - open the file and check for successful open, - then calculate the ending balance...

  • C++ please Question 4: 125 points] Write and test a program that reads two positive integers...

    C++ please Question 4: 125 points] Write and test a program that reads two positive integers nl and n2 with n2 > nl. The program then calculates the sum of the prime numbers, using is prime () function developed above, between nl and n2 (inclusive) A Sample input Enter values for nl and n2 (nl<n2): 3 9 Output: Thé Sum of prime numbers between 3 and 9 (inclusive) is 15

  • 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 reads a list of positive integers from a file named "input.txt."...

    Write a C program that reads a list of positive integers from a file named "input.txt." and count number of odd integers and even integers and prints the results to another file called "results.txt". Please submit both the input and results files along with the c program.

  • Write a small JAVA program that continually reads in user values and calculates the average of all values. The program s...

    Write a small JAVA program that continually reads in user values and calculates the average of all values. The program should first ask the user for the number of values to be entered. Upon storing this value, the program should proceed to read in that amount of values. What will be the best strategy here? a suggestion would be only calculating the average after you know you have read in all values. You can expect to use some type of...

  • with explanation Write a C++ program that calculates and prints the average of several integers. Assume...

    with explanation Write a C++ program that calculates and prints the average of several integers. Assume that the last value read with cin >> is the number 9999. For example, for an input sequence: 10 8 11 7 9 9999 The output should be 9.00

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
ADVERTISEMENT