Statistics Program

I have to create a code that uses functions and arrays to find mean and variance of N entered integers. My teacher has been gone for a while, so I'm quite lost with this subject. Help would be greatly appreciated!

[code]
Project.cpp
#include <iostream>
#include <iomanip>
#include "myFuncs.h"

using namespace std;


int main()
{
double arr[10];
int size;
printf_s("Enter the value of N \n");
scanf_s("%d", &size);
printf_s("Enter %d real numbers \n", size);
for (int i = 0; i < size; i++)
{
scanf_s("%f", &arr[i]);

}
double sampleMean;
double variance;



sampleMean = mean(arr, size);
variance = var(arr, size);

cout << "Your sample mean is: " << sampleMean << endl;
cout << "Your variance is: " << variance << endl;

return 0;
}
------------------------------------------------------------------
myFuncs.cpp

#include <iostream>
#include <iomanip>
#include "myFuncs.h"
using namespace std;

double mean(double arr[], double n)
{

double sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];

}

return sum/n;

}

double var(double arr[], double n)
{

double meanValue = mean(arr, n);
int sum1 = 0;
for (int i = 0; i < n; i++) {
sum1 += (arr[i] - meanValue) * (arr[i] - meanValue);

}
return sum1 / (n - 1);
}

-----------------------------------------------------------------------
myFuncs.h

#pragma once
#include <iostream>
#include <iomanip>
using namespace std;

double mean(double[], double);
double var(double [], double);



Can you specify your problem? And maybe put your code between tags?

Why are you including <iostream> and <iomanip> in myFuncs.h? They will be included by including myFuncs.h

As a reminder: #include "file" or #include <lib> just pastes the entire content of the specified file at the place of the include directive.
Hello SoCalAsia,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

You either missed the closing tag or the dashed line is a problem.

You should decide if this is a C program or a C++ program. The C code at the beginning of main does not work well. I had a problem with it entering the proper numbers into the array. When I changed it to C++ code it worked fine.

Not sure yet if it works right yet. Need to find a way to check the variance. For now entering the numbers 1 - 6 produced the mean of 3.5, which I now is correct, and a variance of 3.2, have to check on that number.

Otherwise the program as a whole compiled and ran with no problems or errors.

Hope that helps,

Andy
Hello SoCalAsia,

After my post I looked up and noticed the header file.

The "#pragma once" is OK, but different systems do not use this. A header guard is a better choice in its place:

1
2
3
4
5
6
7
#ifndef _MYFUNCTS_ 
#define _MYFUNCTS_

double mean(double[], double);
double var(double[], double);

#endif // end !_MYFUNCTS_ 


The other two include files are not needed in a header file. They can be removed.

"using namespace std;" should never be in a header file. Please read the following
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Hope that helps,

Andy
Registered users can post here. Sign in or register to post.