Help please!!! my problem is attaching the txt file Write a program that takes i
ID: 3561831 • Letter: H
Question
Help please!!!
my problem is attaching the txt file
Write a program that takes its input from a file of numbers of type double and outputs the average of the numbers in the file to the screen.The file contains nothing but numbers in the file to the screen.The file contains nothing but numbers of type double separated by blanks and/or line breaks.create your own file name?(kindly show me how save the file and how to run the program on mac's x-code,show your output,thanks). saving it as ".txt" does not work on my system.
my sample data
6 7 8 9 800 7.889 546 8 9.23 7.2392 49.720 677 8 9.3 59 6 2 9 0 2 5.6 76 56 4 8.9 90 1.2 3.4 6.7 8.7 64 56 9.0 7.8 6.7 56 56 78 89 65 78 33 99 77 88.9 8 56 78 98 4.0000 444444.4 6788888 8999999.9 7800000000.0 6.7 1 6 1 2 3 4 5 6 7 8 9
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{
string S;
double next;
fstream input;
fstream input1;
double total;
double totalVar = 0;
int counter = 0;
double avg = 0;
double variance = 0;
input.open("doublenumbers.txt");
if(input.is_open())
{
while(!input.eof())
{
input >>next;
total+= next;
counter++;
}
}
else
{
cout << "Unable to open file";
return 0;
}
input.close();
avg = (double)total/counter;
input1.open("doublenumbers.txt");
while(!input1.eof())
{
input1 >>next;
totalVar = totalVar + pow((avg - next),2);
}
variance = (double)totalVar / counter;
cout<<"Average of the numbers :"<<avg<<endl;
cout<<"Variance of the numbers :"<<variance<<endl;
input1.close();
//system("PAUSE");
return 0;
}
note this is in general form program and u can use any value u want to take as ur input
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.