I think my code works properly but can I just double check with you guys to see if I've used pointers and functions correctly? And if there's any recommendations on ways to improve my code, that will be very much appreciated. Thank you. PS. I'm new to the forum so I do appologize if for not knowing how to format the code pasted here.
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <math.h>
using namespace std;
//Function to calculate sum
long double SumCalculation(double *SumDataArray, int SumN)
{
double SumVal=0;
for(int i=0;i<SumN;i++)
{
SumVal=SumVal+SumDataArray[i];
//cout<<"sum is here"<<SumDataArray[i];
}
return (SumVal);
}
//Function to calculate mean
double MeanCalculation (double MeanSumVal, int MeanN)
{
double MeanVal;
MeanVal=MeanSumVal/MeanN;
return (MeanVal);
}
//Function to calculate standard deviation
double StandDevCalculation ( double *StandDevDataArray, const double StandDevMeanVal, int StandDevN)
{
double StandDevVal;
double StandDevSumVal=0;
for(int i=0;i<StandDevN;i++)
{
StandDevSumVal=StandDevSumVal+pow((StandDevDataArray[i]-StandDevMeanVal),2);
}
StandDevVal=pow(((1/((double)StandDevN-1))*StandDevSumVal),0.5);
return (StandDevVal);
}
int main ()
{
//Ask user to input number of data points
int N;
cout<<"Enter the number of data points:"<<endl;
cin>>N;
//Ask user to input file name
cout<<"Enter name of file:"<<endl;
char DataFileName[100];
cin>>DataFileName;
//Dynamic memory allocation for data
double *MyData;
MyData=new double[N];
//Open file and check if opened successfully
ifstream InputFile;
InputFile.open(DataFileName);
if(!InputFile.good())
{
cout<<"Error: file could not be opened."<<endl;
return(0);
}
//Read in data from file
int ErrNum=0;
for(int i=0;i<N;i++)
{
InputFile>>MyData[i];
if (MyData[i]==0)
}
cout <<ErrNum<<" bad data found and skipped."<<endl;
InputFile.close();
//Call functions to calculate mean and standard deviation
double ChargeSum;
double ChargeMean;
double ChargeStandDev;
ChargeSum=SumCalculation(MyData,N);
ChargeMean=MeanCalculation(ChargeSum,N);
ChargeStandDev=StandDevCalculation(MyData, ChargeMean, N);
cout<<"The sum is:"<< ChargeSum<<endl;
cout<<"The mean is:"<< ChargeMean<<endl;
cout<<"The standard deviation is:"<< ChargeStandDev<<endl;
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <math.h>
usingnamespace std;
//Function to calculate sum
longdouble SumCalculation(double *SumDataArray, int SumN)
{
double SumVal=0;
for(int i=0;i<SumN;i++)
{
SumVal=SumVal+SumDataArray[i]; // NOTE: Consider using the += operator.
//cout<<"sum is here"<<SumDataArray[i];
}
return (SumVal);
}
//Function to calculate mean
double MeanCalculation (double MeanSumVal, int MeanN)
{
/*
double MeanVal;
MeanVal=MeanSumVal/MeanN;
return (MeanVal);*/
return MeanSumVal / (double) MeanN;
}
//Function to calculate standard deviation
double StandDevCalculation ( double *StandDevDataArray, constdouble StandDevMeanVal, int StandDevN)
{
double StandDevVal;
double StandDevSumVal=0;
for(int i=0;i<StandDevN;i++)
{
StandDevSumVal=StandDevSumVal+pow((StandDevDataArray[i]-StandDevMeanVal),2);
}
StandDevVal=pow(((1/((double)StandDevN-1))*StandDevSumVal),0.5);
return (StandDevVal);
}
int main ()
{
//Ask user to input number of data points
int N;
cout<<"Enter the number of data points:"<<endl;
cin>>N;
//Ask user to input file name
cout<<"Enter name of file:"<<endl;
char DataFileName[100];
cin>>DataFileName;
//Dynamic memory allocation for data
/*double *MyData;
MyData=new double[N];*/
double *MyData = newdouble[N]; // Better way.
//Open file and check if opened successfully
ifstream InputFile;
InputFile.open(DataFileName);
if(!InputFile.good())
{
cout<<"Error: file could not be opened."<<endl;
return(0);
}
//Read in data from file
int ErrNum=0;
for(int i=0;i<N;i++)
{
InputFile>>MyData[i];
if (MyData[i]==0)
{
ErrNum=ErrNum+1;
InputFile.clear();
InputFile.ignore(100, '\n');
i=i-1;
}
}
cout <<ErrNum<<" bad data found and skipped."<<endl;
InputFile.close();
//Call functions to calculate mean and standard deviation
double ChargeSum=SumCalculation(MyData,N);
double ChargeMean=MeanCalculation(ChargeSum,N);
double ChargeStandDev=StandDevCalculation(MyData, ChargeMean, N); // I rearranged this bit.
cout<<"The sum is:"<< ChargeSum<<endl;
cout<<"The mean is:"<< ChargeMean<<endl;
cout<<"The standard deviation is:"<< ChargeStandDev<<endl;
delete[] MyData; // This is entirely appropriate use of a pointer to a 'new' array.
getc(stdin);
return 0;
}