Question 1 has already been posted to Chegg and I am waiting

Question 1 has already been posted to Chegg and I am waiting for the answer on this issue - (When the below program is added to a Dev C++ complier, there are no errors shown; however, when the program is run, the answer appears as zero instead of the correct value. What am I doing wrong?)

Question 2 is another using the same program. Please answer this question for this posting. My teach wants us to write a probram that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function. My problem is that it seems that the mean, standard deviation and the number of data are already global and there seems to not be any other program functions to make local. What am I not doing correctly or understanding.

include<iostream>//Basically a code file where all input (keyboard) and output (monitor) functions are defined.
#include<fstream>//Basically a code whereas information is able to be read from a file
#include<vector>//Arrays are a programming tool that provide a mechanism to store a group of values under a single name. The values can be any available data type (e.g., int, double, string, etc.). In C++, the term vectors are used, rather than arrays.
#include<cmath>//declares a set of functions to compute common mathematical operations and transformations
using namespace std;//By using namespaces, you can create your own classes and functions with the same names that the standard library provides and place them in differently named namespaces.

int main()//The main function is a driver function because it tells the other functions in the program the sequence of actions that need to be executed.
{//states that the main program begins here
vector<int> v;//declares a vector of integers when the number of vectors is unknown (v). Permits the program to run without knowing the exact number of integers.
ifstream inputFile; // Ifstream handles file input (reading from files)
ofstream outputFile;//ofstream handles file output (writing to files)
outputFile.open (\"output.txt\"); //opens the output file
int n;//A generic substitute identification for the integers since the integers listed in the array file are unknown until the integers are read. All the numbers in the array will be read in sequence
int total = 0;//Within the array file, the program to calculate the total is to retrieve the integer from the primary position or the zero position
int standardDeviation = 0;// within the array file, the program to calculate standard deviation is to retrieve the integer from the primary position or the zero position
if (inputFile.is_open()) //checking whether the input file can be opened or not

       { //states the function to get the data from the array file and be used in the calculations begins here
           while (!inputFile.eof()) //loop to get each value from the input file until the End of File.
           { //states the function to retrieve each integers begins here
           inputFile >> n; // reading each value into variable n
           total = total + n; //caalculating the sum of all the numbers in the file  
           v.push_back(n); //adding each value from file to vector.
           }//states the function to retrieve each integer ends here
          
           double mean = total/ (double)v.size(); //provides the formula to calculate the mean which is the total of all the array numbers added together divided by (the variance times itself)
           for(int i=0; i<v.size(); i++)//states the first integer is retrieved from the zero spot in the array,the number of times the search for the integer is to take place, determines that the array value is increased by one every time the array is accessed.

           {//states the function to calculate the variance from the mean using the integers with the array data begins here
           standardDeviation += pow(v[i] - mean, 2); //produces the vector value which is (array number for all the loops - the mean) ( times itself)]
           }//states the function to calculate the variance from the mean using the integers within the array data ends here
      
           standardDeviation = sqrt(standardDeviation / (double)v.size());// tells the function the std dev is the [(vector value * itself) / (the number of integers in the array)], afterward the square root of the total result is calculated
           cout<<\"Mean: \"<<mean<<endl;//states to the computer to print out the word \"mean\", the actual calculation for the mean right afterward, then the indication that the end of the line has been reached
           outputFile << \"Mean: \"<<mean<<endl; //states to the computer to put in the output file the word \"mean\", the actual calculation for the mean right afterward, then the indication that the end of the line has been reached
           cout<<\"Standard Deviation: \"<<standardDeviation<<endl;//states to the computer to print out the word \"standarddeviation\", the actual calculation for the standard deviation right afterward, then the indication that the end of the line has been reached
           outputFile << \"Standard Deviation: \"<<standardDeviation<<endl;//states to the computer to put in the output file the word \"standard deviation\", the actual calculation for the standard deviation right afterward, then the indication that the end of the line has been reached
           cout<<\"File has been generated.\"<<endl;//states to the computer to print out \"the file has been generated,\" then the indication that the end of the line has been reached

       }//states the function to get the data from the array file and be used in the calculations ends here
      
       outputFile.close();//closing output file
       inputFile.close();//closing input file
       return 0;// indicates the end of all programming
      
      
}//states that the main program ends here

Solution

readvalues.txt (save this file under D Drive.Then the path of the file pointing to it is D:\\\ eadvalues.txt)

98
93
72
86
99
61
89
77
91
73
79
71
86
74
19

______________________

#include <iostream>
#include <fstream>
#include <iomanip> // std::setprecision
#include <vector>
#include <cmath>
using namespace std;
//Declaring global variables
double mean,standard_deviation;
//Function declarations
void calAvg(vector<int> vec);
void standardDeviation(vector<int> vec);
int main()
{
//defines an input stream for the data file
ifstream dataIn;
  
//Defines an output stream for the data file
ofstream dataOut;
  
//Declaring variables
int nos;
  
//Declaring vector
vector<int> vec;

  
//Opening the input file
dataIn.open(\"D:\\\ eadvalues.txt\");
  
while(dataIn>>nos)
{
vec.push_back(nos);
}
  
//Closing the data input stream
dataIn.close();
  
  
//Calling the function by passing the grades[] array as argument
calAvg(vec);
standardDeviation(vec);
  
  


//creating and Opening the output file
dataOut.open(\"D:\\\ esults.txt\");
  
//Setting the precision to Two decimal places
dataOut<< std::setprecision(2) << std::fixed;

//displaying the mean and standard deviation
dataOut<<\"Mean is \"<<mean<<endl;
dataOut<<\"Standard Deviation is \"<<standard_deviation<<endl;

dataOut.close();

return 0;
}
//Function implementation which calculates the average value of grades[] array
void calAvg(vector<int> vec)
{
//Declaring local variables
double sum=0.0;
  
//calculating the sum of grades[] array elements
for(int i=0;i<vec.size();i++)
{
//calculating the sum
sum+=vec[i];
}
  
//calculating the average
mean=sum/vec.size();
  
}
//This function which claculates the standard deviation
void standardDeviation(vector<int> vec)
{
int sum=0;
double variance;
//This loop Calculating the sum of square of eeach element in the deviation[] array
for(int i=0;i<vec.size();i++)
{
//Calculating the sum of square of eeach element in the deviation[] array
sum+=pow(vec[i]-mean,2);
}
//calculating the standard deviation of grades[] array
variance=(double)sum/vec.size();
standard_deviation=sqrt(variance);
}
  

_________________________

results.txt (We can see the file under D Drive .As we specified path of the output file as D:\\\ esults.txt)

Mean is 77.87
Standard Deviation is 18.93

___________________Thank You

Question 1 has already been posted to Chegg and I am waiting for the answer on this issue - (When the below program is added to a Dev C++ complier, there are no
Question 1 has already been posted to Chegg and I am waiting for the answer on this issue - (When the below program is added to a Dev C++ complier, there are no
Question 1 has already been posted to Chegg and I am waiting for the answer on this issue - (When the below program is added to a Dev C++ complier, there are no
Question 1 has already been posted to Chegg and I am waiting for the answer on this issue - (When the below program is added to a Dev C++ complier, there are no

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site