[I have to create a code to calculate the mean and the standard deviation with array. This is my very first time to use array, so I keep having problems but don't know where to or how to fix them. Here's the code I have come up with so far.
// Includes
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
//Function Prototype
double mean(double[] , int size);
double standardDev(double[] , int size, double mean);
// Start of Main
int main () {
//Variables
int arraySize = 50;
double number = [50];
// Prompt the user about the program
cout << "This program will help the user to get the value of mean and standard deviation from 50 different numbers. " << endl;
cout << "Please follow the instruction, and enther the correct value." << endl;
cout << endl;
cout << endl;
// Input the numbers
for (int i=0; i<50; i++) {
cout << "Enter 50 different numbers : ";
cin>>n[i];
}
// Call the function
mean = (number, size);
standardDev = (number, size, mean);
// Output
cout << "The value of the mean: " << mean << endl;
cout << "The value of the standard deviation: " << standardDev << endl;
return 0;
}
// Function Definition
double mean (double number[], int size) {
double s1 = -999.99;
for ( int i = 0; i < arraySize; i++ ) {
s1 += n[i];
}
m=s1/50;
return m;
}
double standardDev (double number[], int size, double m) {
double standardDev;
double s2 = -999.99;
for ( int i = 0; i < size; i++ ) {
s2 += pow((n[i]-m),2);
}
standardDev= sqrt(s2/(size-1));
return standardDev;
}
MAIN(){
-Prompt & explain
-Input 50 values
-Call the 2 functions
-Output the ans
}
I would suggest that rather than asking the user to inter 50 values, which is quite a lot, especially if they are in a lab setting of neurological mice brain membrane calculations and the 42nd is missed or entered wrong.
Perhaps start the program with either One(1) number or ask the user how many numbers to be entered.
You 'can' vary the size of an array with vectors, but to start, leave your array size set to 50 as this really won't hurt much.
At this point - stop.
get some paper.
get a sdt dev formula
- d^2 Y
------------- F(x)
- D x^2
(btw , mine is negated for a special reason)
AND THEN step through the problem, step by step,
on paper.
so that you can make notes, scribbles, and
so you can make intermediate calculations
which will be added to your code
as benchmarks to verify that your code matches
your expectations at various spots.
Since your program (now) only asks for a single value as input,
then you only need to write-down the steps once.
Once you have those steps fully and clearly defined,
then you can group those steps into small functions
Too, I suggest declaring ALL of your vars at the beginning,
even if you need to comment-out some of them.
(because some will be local vars - only visible within certain func's)
By loading them all up-front, then it'll be easy to see what you have avail to work with, and what their TYPE is, and their init value, if any.
Lastly, try to follow some naming convention such as naming
all arrays with a_, such a_Array[].
or a_n[]
otherwise, something like an array named n
can easily get confused with the variable n.
I went over the source code and fixed some mistakes but I keep having an error saying: (38): error C2065: 'i' : undeclared identifier
How should I "identify" the letter i ?
=============================================
// Includes
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
//Function Prototype
double sD(double[] , int); // Function for standard deviation
double m1(double[] , int); // Function for mean
double m2;
// Start of Main
int main () {
// Variables
int arraySize=50; // Array size
double n[50]; // Data can hold 50 numbers
m2; // Second mean value
// Prompt the user about the program
cout << endl;
cout << "This program will help user to get the value of standard deviation and that of mean. " << endl;
cout << "Please follow the instruction, and enter the value correctly. " << endl;
cout << endl;
cout << endl;
cout << endl;
// Input the values
cout << "Enter the values to calculate standard deviation : " << endl;
for (int i=0; i<50; i++); {
cin>>n[i];
}
// Call the Function and Print the Output
cout << "The value of mean is: " << m1 (n, 50) << endl;
cout << "The value of standard deviation : " << sD (n, 50) << endl;
return 0;
} // End of main
// Function Definition
double m1 (double n[], int arraySize) {
double sum=0.0;
for (int i = 0; i <= arraySize; i++) {
sum += n[i];
}
m2=sum/50;
return m2;
} // Function for mean
for ( int i = 0; i <=arraySize; i++ ) {
sum2 += pow((n[i]-m2),2);
}
sD=sqrt(sum2/(arraySize-1));
return sD;
} // Function for standard deviation
// End of Program