pointers

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)

{
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;
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;


delete[] MyData;



getc(stdin);


return 0;
}
Please put code in format tags and use indentation.

Modified a couple of things I'm picky about, looks like you've used the pointer correctly though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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]; // 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, 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];*/
	double *MyData = new double[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;
} 
Thank you for your quick reply.The corrections were of great help.
Topic archived. No new replies allowed.