fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdio>
  4.  
  5. int main(){
  6. std::ifstream inputFile("Random.txt");
  7. if (!inputFile){
  8. printf(There was an error opening the file.\n"); // Displays a message that there has been an error opening the file
  9. return 1;
  10. }
  11. int number;
  12. int count = 0;
  13. double sum = 0;
  14.  
  15. while (inputFile >> number){
  16. count++;
  17. sum += number; // Reads all the numbers from the file and calculates the number of numbers in the file
  18. }
  19.  
  20. inputFile.close();
  21.  
  22. double average = (count != 0) ? (sum / count): 0; // Calculates the sum of all numbers
  23.  
  24. printf("Number of numbers: %d\n", count);
  25. printf("Sum of numbers: %.2f\n", sum);
  26. printf("Average of all numbers: %.2\n", average);
  27.  
  28. return 0
  29. }
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. }
  37.  
  38.  
  39.  
Success #stdin #stdout 0.03s 25308KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <fstream>
#include <cstdio>

int main(){
    std::ifstream inputFile("Random.txt");
    if (!inputFile){
        printf(There was an error opening the file.\n"); // Displays a message that there has been an error opening the file
        return 1;
    }
    int number;
    int count = 0;
    double sum = 0; 
    
    while (inputFile >> number){
        count++;
        sum += number; // Reads all the numbers from the file and calculates the number of numbers in the file
    }
    
    inputFile.close();
    
    double average = (count != 0) ? (sum / count): 0; // Calculates the sum of all numbers
    
    printf("Number of numbers: %d\n", count);
    printf("Sum of numbers: %.2f\n", sum); 
    printf("Average of all numbers: %.2\n", average);
    
    return 0
}
    
    
    
    
    
    
    }