Question

these numbers are from Random.txt file since i couldn't upload the file 42 468 335 501...

these numbers are from Random.txt file since i couldn't upload the 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

In C++

Prototype your functions above "main" and define them below "main"

The file named Random.txtPreview the document contains a long list of random numbers. Download the file to your system, then write a program that opens the file, reads all the numbers from the file, and calculates the following:

The number of numbers in the file
The sum of all the numbers in the file (a running total)
The average of all the numbers in the file
The program should display the number of numbers in the file, the sum of the numbers, and the average of the numbers.

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

//C++ program

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

int main(){
   ifstream in1;
   ifstream in2;
   ofstream out;
   in1.open("Random.txt");
   out.open("output.txt");
  
   if(!in1){
       cout<<"file not open\n";
       return 0 ;
   }
  
   int max = INT_MIN;
   int num;
  
   while(!in1.eof()){
       in1>>num;
       if(num>max)max=num;
   }
  
   in1.close();
   int count[max+1];
   for(int i=0;i<=max;i++)count[i] = 0;
   in2.open("Random.txt");
  
   while(!in2.eof()){
       in2>>num;
       count[num]++;
   }
  
   long sum=0;
   long totalCount=0;
  
   for(int i=0;i<=max;i++){
       if(count[i]!=0){
           out<<i<<"\t"<<count[i]<<"\n";
           cout<<i<<"\t"<<count[i]<<"\n";
           sum+=i*count[i];
           totalCount+=count[i];
       }
   }
  
   out<<"\nSum : "<<sum<<"\n";
   out<<"\nAverage : "<<(double)sum/totalCount<<"\n";
  
   in2.close();
   out.close();
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
these numbers are from Random.txt file since i couldn't upload the file 42 468 335 501...
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
  • An easier way to do the Binary Search part or a simplistic way for beginners. Not...

    An easier way to do the Binary Search part or a simplistic way for beginners. Not using such high code for the assignment in general also commenting on what each line does. COMMENT ON the code below .what does each part do for the program? and a better and more simple and basic way to do binary search and get same results. #include <iostream> #include <fstream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp...

  • In C++ Programming: Using a single for loop, output the even numbers between 2 and 1004...

    In C++ Programming: Using a single for loop, output the even numbers between 2 and 1004 (inclusive) that iterates (loops) exactly 502 times. The outputted numbers be aligned in a table with 10 numbers per row. Each column in the table should be 5 characters wide. Do not nest a loop inside of another loop. Hint: First create and test the code that output the numbers all on one line (the command line will automatically wrap the output to new...

  • Create a program that will use the attached input file and perform the following operations. Read...

    Create a program that will use the attached input file and perform the following operations. Read the file into an appropriate JCF data structure. Look up a (list of) names and numbers matching a last name or the first letters of a last name, ignoring case. Look up a (list of) names and numbers matching a number or the first digits of a number. Add a name and number to the list. Sort the list by first name, last name...

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