C++ program, Microsoft Visual Studio (codes) The attached text file contains not
ID: 663705 • Letter: C
Question
C++ program, Microsoft Visual Studio (codes)
The attached text file contains nothing but integers with values between -500 and 500. Write a program that counts how many values there are and how many values are negative (0 is counted as positive). Dynamically create an array to store the even values between 0 and 500. Using the statistics library, calculate the average and standard deviation to the array. Output the following to a text file: the total number of values, the number of negative values, the number of even values between 0 and 500, the average of those values, and the standard deviation of those values.
i have a attached file...containg random integer... if you can show me the coded attach the file with name aa which is put in on desktop of my computer named aa.txt
please help!!!!!
i got this code but its doesnt work
#include <iostream>
#include <fstream>
#include<math.h>
using namespace std;
int main(int argc, char * argv[])
{
ifstream myfile("D:\aa.txt", ios_base::in);//inpputting file. Giving file path
int arr[1000];
int a,i=0, sum=0;
int countNeg=0, countEven=0;
printf("The file contains : ");
//Reading from file and putting it in a
while (myfile >> a)
{
arr[i]=a;
if(a<0)
{
countNeg++;
}
else if(a%2==0)
{
countEven++;
}
printf("%d ", a);
i++;
}
printf(" Total numbers : %d",i);
printf(" Total number of negative numbers : %d",countNeg);
printf(" Total number of Even numbers : %d",countEven);
//Calculating Sum
for(int j=0;j<i;j++)
{
sum=sum+arr[j];
}
//Calculating average
float avg=sum/i;
printf(" Average = %f ",avg);
float sum1;
//Calculating Standard Deviation
for (int k = 0; k < i; k++)
{
sum1 = sum1 + pow((arr[k] - avg), 2);
}
float sd=sqrt(sum1/i);
printf("Standard Deviation = %f ",sd);
getchar();
return 0;
}
Explanation / Answer
working code
/* This is C++ sample code for File I/O */
#include <iostream> // library that contain basic input/output functions
#include <fstream> // library that contains file input/output functions
#include <vector>
#include <math.h>
using namespace std;
float standard_deviation(vector<int> data, int n)
{
float mean=0.0, sum_deviation=0.0;
int i;
for(i=0; i<n;++i)
{
mean+=data[i];
}
mean=mean/n;
for(i=0; i<n;++i)
sum_deviation+=(data[i]-mean)*(data[i]-mean);
return sqrt(sum_deviation/n);
}
int main()
{
fstream fp;
vector<int> numbers;
int number;
int count=0;
int neg=0;
int even=0;
int sum=0;
float avg=0;
float sd=0;
fp.open("test.txt", ios::in | ios::binary);
if(fp.is_open()){
while(fp >> number){
numbers.push_back(number);
}
}
fp.close();
cout << "Numbers: ";
for (int i=0; i < numbers.size(); i++) {
sum = sum+ numbers[i];
count++;
if (numbers[i]<0)
neg++;
if (numbers[i]%2 == 0)
even++;
cout << numbers[i] << ' ';
}
avg=sum/count;
sd=standard_deviation(numbers,count);
cout <<"Total numbers"<<count;
cout <<" Total Negative numbers"<<neg;
cout <<" Total even numbers"<<even;
cout<<" Sum of all the numbers"<<sum;
cout<<" Average of all the numbers"<<avg;
cout<<" SD of all the numbers"<<sd;
return 0;
}
output here tested on runnable
http://code.runnable.com/VYjK7KApJ3AnWPpN/output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.