I need immediate help in writing a function program. I have to write a
program in functions and use array to store them. I am not familiar
with functions and i tried to create it but i fails to run, However, i
simply created this program in arrays and it runs good except i cant
figure out how to compute the standard deviation. The coding is below.
Any help will be appreciated.

1) The Program will prompt the user for six grades to be entered (one
at a time, read in each grade by the user , and them in an array of six
elements.

2) The average grade (again, on a 0.0 - 4.0 scale), by looping through
the array again;

3) The program will compute the standard deviation of the six
individual grades from the average according to following formula:

``````n-1
E       (x[i] - avg)2
I=0___________________

n-1``````

where n is the number of values that were averaged (6 in this case),
x is a particular value, and avd of all n values. I.e for each of
the n values, you take the difference between that value and the
average, square that difference, and sum all n squares. Then divid that
sum by n-1 and take the square root of that quotient. This gives you
the standard deviation.

``````#include <iostream>

using namespace std;

int main()
{
const int SIZE = 6;
double score[SIZE];
int i =0;
double sum = 0;

for ( i = 0; i < SIZE; i++)

{
cout << "Input a Score " << i+1 << ":" ;
cin >> score[i];

while (score[i] > 4 || score[i] < 0)
{
<< " between 0 and 4.0 inclusive : ";
cin >> score[i];
}
sum = sum + score[i];
}

double average = sum / 6 ;

if (average <= 4.0 && average > 3.2)
{
}
if (average <= 3.2 && average > 2.4)
{
}
if (average <= 2.4 && average > 1.6)
{
}
if (average <= 1.6 && average > 0.8)
{
}
if (average <= 0.8 && average > 0)
{
}

// Output the result:
cout << "The average is " << average << "." << endl;

cout << "The final letter grade is " << grade << endl;
return 0;
} //  function main``````

>>I am not familiar with functions and i tried to create it but i fails to run
please post the code so that we can help you. What compiler and operating system are you using?

I am using PICO compiler. Its a secure shell client(ssh) or putty can also be used. Windows XP. This is C++ program.

I.e for each of
the n values, you take the difference between that value and the
average, square that difference, and sum all n squares. Then divid that
sum by n-1 and take the square root of that quotient. This gives you
the standard deviation.

That is how the sd is calculated. It will be a lot easier for you if you just take the requirements one sentence at a time. Below are code snippets of what I think will work -- I didn't compile or test them. But hopefuly it will give you an idea of how to do this.

``````// get simple average of all scores
average = sum / SIZE;
...
...
// [b]inside a loop[/b], do first sentence.
double diff = (scopre[i] - average) ;
sum_of_squares += (diff * diff);
///
// after the above loop finishes, do second sentence

n = sum_of_squares / SIZE;
standard_deviation = sqrt(n); // sqrt() is in math.h``````

Thanks for your clue. I am working on the standard deviation. One QUESTION here for computing the standard deviation. Do you want me to run the loop within a loop or it is suppose to be a sepertate loop. The new codes are below. MOST IMPORTANTLY, i can't figure out that how can i get the average grades in letter by LOOPING through the array. Please point me in some direction that i can figure this out.

2) a) The average grade (again, on a 0.0 - 4.0 scale), by looping
through the array again;
b) the final letter grade according to the following equivalences:

> 3.2 < grade <=4.0 : A
> 2.4 < grade <=3.2 : B
> 1.6 < grade <=2.4 : C
> 0.8 < grade <=3.2 : D
> 0 <= grade <= 0.8 : F

``````#include <iostream>
#include <iomanip>
using namespace std;
double average(double SUM, double size)
{
}
int main()
{
const int SIZE = 6;
double score[SIZE];
double sum = 0;
cout << fixed << setprecision(2);
{
while( score[ grade ] > 4 || score[ grade ] < 0 )
{
<< " between 0 and 4.0 inclusive : ";
}
}
double gaverage = average(sum, SIZE);
cout << "The average grade is :" << gaverage << endl;
}``````
``````void enterScores(int *, const int);
void calculateAverage(int *, const int, double &);
void calculateStandardDeviation(int *, const int, double &, double &);

int main()
{
const int SIZE = 6;
int score[SIZE];
double averageScore = 0.0;
double stdDev = 0.0
cout << fixed << setprecision(2);

calculateAverage(score, SIZE, averageScore);
calculateStandardDeviation(score, SIZE, averageScore, stdDev);

cout << "average score is " << averageScore << " with standard deviation of " << stdDev << endl;
}

void enterScores(int * score, const int SIZE)
{
{
while( score[ grade ] > 4 || score[ grade ] < 0 )
{
<< " between 0 and 4 inclusive : ";
}
}
}

void calculateAverage(int * score, const int SIZE, double & averageScore)
{
int sum = 0;
for(int i = 0; i < SIZE; ++i)
sum += score[i];

averageScore = sum/SIZE;
}``````

The above program is incomplete. It declares three functions, each corresponding to the steps described in the instructions. It utilizes passing by reference to pass information back and forth between functions. If you aren't familiar with pass by reference, then continue to use a return value as you did in your code to return the average and the standard deviation. It utilizes the interchangeability of pointers and arrays. It doesn't use global variables, and keeps variables constant when they should be constant. It uses ints when appropriate and doubles when appropriate. It uses your code as much as possible and adapts your code when necessary. You can ue it as a template to finish your program by writing out the remaining function definitions.

In general, each major action in a program can be written into a funciton. If the action is taken only once in a program, it doesn't save any time or efffort and is actually wasteful of time and energy to do this. However, if the action is taken more than once during a program, or if you want to reuse the function in another program, it is very helpful to encapsulate each major action into functions for ease of reuse and to minimize typing, etc. Since it's easier to learn how to do this when it isn't immediately helpful, you end up with teaching exercises such as this, where each function is only used once in the program.

To convert the average score, which is of type double, to a letter grade, which is of type char, you can use a series of if statements like you did with in the first post. However, since each grade level is self exclusionary, once you find the appropriate grade you can skip the rest by using a series of exclusionary if, else if, else statements rather than independently standing if statements.

Yes! You are absolutely right that i am not familiar with pointers. I hardly know about functions. Well! i guess practice makes you best;) . I guess atleaset i start grasping on some functions. I would appreciate if you shed some light on pass by pointers and reference in functions and how can i utilize pointers in my program.

Below is the code including the computations of standard deviation codes.

``````#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

double average(double SUM, double size)
{
}

double standard_deviation(double SUM_OF_SQUARES, double size)
{
double answer1 = sqrt (SUM_OF_SQUARES / size);
}

int main()
{
const int SIZE = 6;
double score[SIZE];
double sum = 0;
double sum_of_squares = 0;

cout << fixed << setprecision(2);
{
while( score[ grade ] > 4 || score[ grade ] < 0 )
{
<< " between 0 and 4.0 inclusive : ";
}
}

double gaverage = average(sum, SIZE);
cout << "The average grade is :" << gaverage << endl;

{
double diff = (score[grade] - gaverage);
sum_of_squares +=(diff * diff);
}

double n = standard_deviation (sum_of_squares, SIZE);
cout << "The standard deviation is : " << n << endl;

}``````

. Do you want me to run the loop within a loop or it is suppose to be a sepertate loop.

get the sum as you are doing now, but after the loop finishes, the average is just the sum divided by the number of items. No point in calling a function to get that simple calculation.

>> MOST IMPORTANTLY, i can't figure out that how can i get the average grades in letter

The average used in calculating the standard deviation is NOT a letter grade such as 'A', or 'B', but the numerical average, such as 3.5. The letter grade is not even mentioned in the requirements you posted.

(sorry-- I hit the wrong button -- WaltP)

gradedani.cpp:19: error: cannot convert `double*' to `int*' for argument `1' to `void enterScores(int*, int)'
gradedani.cpp:20: error: cannot convert `double*' to `int*' for argument `1' to `void calculateAverage(int*, int, double&)'
gradedani.cpp:21: error: cannot convert `double*' to `int*' for argument `1' to `void calculateStandardDeviation(int*, int, double&, double&)'
gradedani.cpp: In function `void calculateStandardDeviation(int*, int, double&, double&)':
gradedani.cpp:58: warning: converting to `int' from `double'

``````#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
void enterScores(int *, const int);
void calculateAverage(int *, const int, double &);
void calculateStandardDeviation(int *, const int, double &, double &);
int main()
{
const int SIZE = 6;
double score[SIZE];
double averageScore = 0.0;
double stdDev = 0.0;
cout << fixed << setprecision(2);
enterScores(score, SIZE);
calculateAverage(score, SIZE, averageScore);
calculateStandardDeviation(score, SIZE, averageScore, stdDev);
cout << "average score is : " << averageScore << endl;
cout << "standard deviation is : " << stdDev << endl;
}

void enterScores(int * score, const int SIZE)
{
{
while( score[ grade ] > 4 || score[ grade ] < 0 )
{
<< " between 0 and 4 inclusive : ";
}
}
}

void calculateAverage(int * score, const int SIZE, double & averageScore)
{
int sum = 0;
for(int i = 0; i < SIZE; ++i)
sum += score[i];

averageScore = sum/SIZE;
}

void calculateStandardDeviation(int * score, const int SIZE, double & averageScore, double & stdDev)

{
int sum_of_squares = 0;
{   double diff = (score[grade] - averageScore);
sum_of_squares +=(diff * diff);
}
stdDev = sqrt (averageScore / SIZE);
}``````

Anatomy of an error message:

OK, check your code. Definition for enterScores is: `void calculateAverage(int *, const int, double &);` You call the function with `calculateAverage(score, SIZE, averageScore);` The error states

gradedani.cpp:19: error: cannot convert `double*' to `int*' for argument `1' to `void enterScores(int*, int)'

Argument 1 -- might mean score. What type of variable is score? What type of variable is the function expecting for parameter 1 (see definition)? Is there a conflict?

Thanks for your help. I fixed that problem earlier. It's been straignt nine hours working on this project. It's almost completed. The problem is that i wouldn,t be able to output the grade 'letter'. The program below compiles and compute the 6 grades averages and its standard deviation, but nothing shows in letter output.

I guess there is something wrong the way i declare the function. please somebody look at the program and help me out here. I am exausted plz. i just wanna get over of this.

Thanks

``````#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

void enterScores(double *, const int);
void calculateAverage(double *, const int, double &);
void calculateStandardDeviation(double *, const int, double &, double
&);

int main()
{
const int SIZE = 6;
double score[SIZE];
double averageScore = 0.0;
double stdDev = 0.0;
char letter;
cout << fixed << setprecision(2);

enterScores(score, SIZE);
calculateAverage(score, SIZE, averageScore);
calculateStandardDeviation(score, SIZE, averageScore, stdDev);

cout << "average score is : " << averageScore <<"."<< endl;
cout << "standard deviation is : " << stdDev <<"."<< endl;
cout << "The final grade is: " << letter << endl;

}

void enterScores(double * score, const int SIZE)
{
{

}
}

}

void calculateAverage(double * score, const int SIZE, double &
averageScore)
{
double sum = 0;
for(int i = 0; i < SIZE; ++i)
sum += score[i];

averageScore = sum/SIZE;

}

void calculateStandardDeviation(double * score, const int SIZE, double
& averageScore, double & stdDev)

{
double sum_of_squares = 0;
{   double diff = (score[grade] - averageScore);
sum_of_squares +=(diff * diff);
}
stdDev = sqrt (sum_of_squares / SIZE);

}

void printLetterGrade(char letter, double & averageScore)

{
if (averageScore <= 4.0 && averageScore > 3.2)
{
letter = 'A';
}
if (averageScore <= 3.2 && averageScore > 2.4)
{
letter = 'B';
}
if (averageScore <= 2.4 && averageScore > 1.6)
{
letter = 'C';
}
if (averageScore <= 1.6 && averageScore > 0.8)
{
letter = 'D';
}
if (averageScore <= 0.8 && averageScore >= 0)
{
letter = 'F';
}``````

Thanks everybody for the help. Your tips really walk me through the program and help me debug it. Well its been good straight 10 hours doing this program. i am exausted and tired but satisfied with the work i done:)

Below are the codes i pasted for everybody.

``````#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
void enterScores(double *, const int);
void calculateAverage(double *, const int, double &);
void calculateStandardDeviation(double *, const int, double &, double &);
int main()
{
const int SIZE = 6;
double score[SIZE];
double averageScore = 0.0;
double stdDev = 0.0;

cout << fixed << setprecision(2);

enterScores(score, SIZE);
calculateAverage(score, SIZE, averageScore);
calculateStandardDeviation(score, SIZE, averageScore, stdDev);
cout << "The average score is : " << averageScore <<"."<< endl;
cout << "The final grade is: " << letter << "." << endl;
cout << "The standard deviation is : " << stdDev <<"."<< endl;
}

void enterScores(double * score, const int SIZE)
{
{
while( score[ grade ] > 4 || score[ grade ] < 0 )
{
<< " between 0 and 4 inclusive : ";
}
}
}
void calculateAverage(double * score, const int SIZE, double & averageScore)
{
double sum = 0;
for(int i = 0; i < SIZE; ++i)
sum += score[i];

averageScore = sum/SIZE;
}
{
char letter = 'O';
if (averageScore <= 4.0 && averageScore > 3.2)
{
letter = 'A';
}
if (averageScore <= 3.2 && averageScore > 2.4)
{
letter = 'B';
}
if (averageScore <= 2.4 && averageScore > 1.6)
{
letter = 'C';
}
if (averageScore <= 1.6 && averageScore > 0.8)
{
letter = 'D';
}
if (averageScore <= 0.8 && averageScore >= 0)
{
letter = 'F';
}
return letter;
}
void calculateStandardDeviation(double * score, const int SIZE, double & averageScore, double & stdDev)
{
double sum_of_squares = 0;