C++ program, Microsoft Visual Studio 2010 (codes) The attached text file contain
ID: 663709 • Letter: C
Question
C++ program, Microsoft Visual Studio 2010 (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!!!!!
One code i have it says "BUILD ERROR" code is given below
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
std::ifstream readfile("aa.txt");//reding numbers from text file
std::string eachline;
int count=0;
std::vector<int> dynamicArray;//dynamic vector array
while (std::getline(readfile, eachline))
{
std::istringstream iss(eachline);
int n;
while (iss >> n)//reading each value from file
{
if(n%2==0){
dynamicArray.push_back(n);//storing into array
}
count++;
}
}
int negative=0,positive=0,i;
double standard_deviation=0.0,average=0.0,sum=0.0;
for ( int i = 0; i < dynamicArray.size(); i++) {//iterating values to calcualte positive,negative
if(dynamicArray[i]>=0){//storing positive
positive++;
}
else{
negative++;//storing negative
}
sum=sum+dynamicArray[i]; //storing sum to calculate average
}
int size = dynamicArray.size();
average=sum/size;//calculating average
for ( int i = 0; i < dynamicArray.size(); i++) {//calculating standard deviation
int val = dynamicArray[i]-average;//minus average from each value
int sqrt_val = sqrt(val);//square root taken
standard_deviation=standard_deviation+sqrt_val;//this to calcualte mean
}
standard_deviation = standard_deviation;
cout<<standard_deviation;
standard_deviation=standard_deviation/size;
standard_deviation=sqrt(standard_deviation);
//printing results
cout<<" Total Numbers are:"<<count;
cout<<" Total Positive Numbers are:"<<positive;
cout<<" Total Negative Numbers are:"<<negative;
cout<<" Average of Numbers are:"<<average;
cout<<" Standard deviation of Numbers are:"<<standard_deviation;
//outputting to file
ofstream out_data("output.txt");
out_data << " Total Numbers are:"<<count;
out_data<<" Total Positive Numbers are:"<<positive;
out_data<<" Total Negative Numbers are:"<<negative;
out_data<<" Average of Numbers are:"<<average;
out_data<<" Standard deviation of Numbers are:"<<standard_deviation;
return 0;
}
Explanation / Answer
#include <iostream.h>
#include <fstream.h>
#include <sstream>
#include <string.h>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
std::ifstream readfile("aa.txt");//reding numbers from text file
std::string eachline;
int count=0;
std::vector<int> dynamicArray;//dynamic vector array
while (std::getline(readfile, eachline))
{
std::istringstream iss(eachline);
int n;
while (iss >> n)//reading each value from file
{
if(n%2==0){
dynamicArray.push_back(n);//storing into array
}
count++;
}
}
int negative=0,positive=0,i;
double standard_deviation=0.0,average=0.0,sum=0.0;
for ( int i = 0; i < dynamicArray.size(); i++) {//iterating values to calcualte positive,negative
if(dynamicArray[i]>=0){//storing all positive numbers from 0 to 500
positive++;
}
else{
negative++;//storing negative from -1 to -500
}
sum=sum+dynamicArray[i]; //storing sum to calculate average
}
int size = dynamicArray.size();
average=sum/size;//calculating average
for ( int i = 0; i < dynamicArray.size(); i++) {//calculating standard deviation
int val = dynamicArray[i]-average;//minus average from each value
int sqrt_val = sqrt(val);//square root taken
standard_deviation=standard_deviation+sqrt_val;//this to calcualte mean
}
standard_deviation = standard_deviation;
cout<<standard_deviation;
standard_deviation=standard_deviation/size;
standard_deviation=sqrt(standard_deviation);
//printing results
cout<<" Total Numbers are:"<<count;
cout<<" Total Positive Numbers are:"<<positive;
cout<<" Total Negative Numbers are:"<<negative;
cout<<" Average of Numbers are:"<<average;
cout<<" Standard deviation of Numbers are:"<<standard_deviation;
//outputting to file
ofstream out_data("output.txt");
out_data << " Total Numbers are:"<<count;
out_data<<" Total Positive Numbers are:"<<positive;
out_data<<" Total Negative Numbers are:"<<negative;
out_data<<" Average of Numbers are:"<<average;
out_data<<" Standard deviation of Numbers are:"<<standard_deviation;
return 0;
}
After the above modification the program produces the required output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.