Need Help with the functions program

A

asif929

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 - 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;
string grade;

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

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

while (score > 4 || score < 0)
{
cout <<" Invalid grade - please re-enter a grade"
<< " between 0 and 4.0 inclusive : ";
cin >> score;
}
sum = sum + score;
}

double average = sum / 6 ;


if (average <= 4.0 && average > 3.2)
{
grade = "A";
}
if (average <= 3.2 && average > 2.4)
{
grade = "B";
}
if (average <= 2.4 && average > 1.6)
{
grade = "C";
}
if (average <= 1.6 && average > 0.8)
{
grade = "D";
}
if (average <= 0.8 && average > 0)
{
grade = "F";
}

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

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

John Carson

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,

Didn't your class cover functions? Doesn't your textbook?

I presume that what you are being asked to do is break up the operations
conducted in main() into several steps, each of which is performed by a
function.

I suggest you post your attempt with functions here and we can give
suggestions as to where you are going wrong.
However, i
simply created this program in arrays and it runs good except i cant
figure out how to compute the standard deviation.

Use a for() loop. Compute (score - average)*(score-average) at each
iteration. You can figure out the rest.
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 - 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;
string grade;

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

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

while (score > 4 || score < 0)
{
cout <<" Invalid grade - please re-enter a grade"
<< " between 0 and 4.0 inclusive : ";
cin >> score;
}
sum = sum + score;
}

double average = sum / 6 ;


if (average <= 4.0 && average > 3.2)
{
grade = "A";
}
if (average <= 3.2 && average > 2.4)
{
grade = "B";
}
if (average <= 2.4 && average > 1.6)
{
grade = "C";
}
if (average <= 1.6 && average > 0.8)
{
grade = "D";
}
if (average <= 0.8 && average > 0)
{
grade = "F";
}

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

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

asif929

John said:
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,

Didn't your class cover functions? Doesn't your textbook?

I presume that what you are being asked to do is break up the operations
conducted in main() into several steps, each of which is performed by a
function.

I suggest you post your attempt with functions here and we can give
suggestions as to where you are going wrong.
However, i
simply created this program in arrays and it runs good except i cant
figure out how to compute the standard deviation.

Use a for() loop. Compute (score - average)*(score-average) at each
iteration. You can figure out the rest.
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 - 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;
string grade;

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

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

while (score > 4 || score < 0)
{
cout <<" Invalid grade - please re-enter a grade"
<< " between 0 and 4.0 inclusive : ";
cin >> score;
}
sum = sum + score;
}

double average = sum / 6 ;


if (average <= 4.0 && average > 3.2)
{
grade = "A";
}
if (average <= 3.2 && average > 2.4)
{
grade = "B";
}
if (average <= 2.4 && average > 1.6)
{
grade = "C";
}
if (average <= 1.6 && average > 0.8)
{
grade = "D";
}
if (average <= 0.8 && average > 0)
{
grade = "F";
}

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

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


Believe me, the way they teach it, i need to be genious to understand.
I dont have any any understanding of functions at all. Belive me
otherewise i wouldve done it by myself. Please help me in some extent i
could do something.
I presume that what you are being asked to do is break up the operations
conducted in main() into several steps, each of which is performed by a
function

Yes you are right. All i know they declare the Global varaibles and do
loop before the main().
Please help me here.
 
A

asif929

John said:
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,

Didn't your class cover functions? Doesn't your textbook?

I presume that what you are being asked to do is break up the operations
conducted in main() into several steps, each of which is performed by a
function.

I suggest you post your attempt with functions here and we can give
suggestions as to where you are going wrong.
However, i
simply created this program in arrays and it runs good except i cant
figure out how to compute the standard deviation.

Use a for() loop. Compute (score - average)*(score-average) at each
iteration. You can figure out the rest.
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 - 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;
string grade;

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

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

while (score > 4 || score < 0)
{
cout <<" Invalid grade - please re-enter a grade"
<< " between 0 and 4.0 inclusive : ";
cin >> score;
}
sum = sum + score;
}

double average = sum / 6 ;


if (average <= 4.0 && average > 3.2)
{
grade = "A";
}
if (average <= 3.2 && average > 2.4)
{
grade = "B";
}
if (average <= 2.4 && average > 1.6)
{
grade = "C";
}
if (average <= 1.6 && average > 0.8)
{
grade = "D";
}
if (average <= 0.8 && average > 0)
{
grade = "F";
}

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

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


Believe me, the way they teach it, i need to be genious to understand.
I dont have any any understanding of functions at all. Belive me
otherewise i wouldve done it by myself. Please help me in some extent i
could do something.
I presume that what you are being asked to do is break up the operations
conducted in main() into several steps, each of which is performed by a
function

Yes you are right. All i know they declare the Global varaibles and do
loop before the main().
Please help me here.
 
A

asif929

John said:
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,

Didn't your class cover functions? Doesn't your textbook?

I presume that what you are being asked to do is break up the operations
conducted in main() into several steps, each of which is performed by a
function.

I suggest you post your attempt with functions here and we can give
suggestions as to where you are going wrong.
However, i
simply created this program in arrays and it runs good except i cant
figure out how to compute the standard deviation.

Use a for() loop. Compute (score - average)*(score-average) at each
iteration. You can figure out the rest.
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 - 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;
string grade;

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

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

while (score > 4 || score < 0)
{
cout <<" Invalid grade - please re-enter a grade"
<< " between 0 and 4.0 inclusive : ";
cin >> score;
}
sum = sum + score;
}

double average = sum / 6 ;


if (average <= 4.0 && average > 3.2)
{
grade = "A";
}
if (average <= 3.2 && average > 2.4)
{
grade = "B";
}
if (average <= 2.4 && average > 1.6)
{
grade = "C";
}
if (average <= 1.6 && average > 0.8)
{
grade = "D";
}
if (average <= 0.8 && average > 0)
{
grade = "F";
}

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

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


Believe me, the way they teach it, i need to be genious to understand.
I dont have any any understanding of functions at all. Belive me
otherewise i wouldve done it by myself. Please help me in some extent i
could do something.
I presume that what you are being asked to do is break up the operations
conducted in main() into several steps, each of which is performed by a
function

Yes you are right. All i know they declare the Global varaibles and do
loop before the main().
Please help me here.
 
J

John Carson

Believe me, the way they teach it, i need to be genious to understand.
I dont have any any understanding of functions at all. Belive me
otherewise i wouldve done it by myself. Please help me in some extent
i could do something.

Functions are covered in every C++ textbook. Answering your question would
mean either:

1. Giving an exceedingly long answer that explains functions.
2. Giving you code to answer your question, without you needing to
understand functions.

Neither seems appropriate.

You can find numerous tutorials on functions here:

http://www.google.com.au/search?hl=en&q=C+++function+tutorial&btnG=Search&meta=

You can also find a free downloadable textbook here:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
 
B

BobR

(e-mail address removed) wrote in message ...
Believe me, the way they teach it, i need to be genious to understand.
I dont have any any understanding of functions at all. Belive me
otherewise i wouldve done it by myself. Please help me in some extent i
could do something.


Yes you are right. All i know they declare the Global varaibles and do
loop before the main().
Please help me here.


#include <iostream>
#include <ostream>

int ToBook(){
const int Size = 6;
double score[ Size ];
double sum( 0);
for( int i( 0 ); i < Size; ++i ){
std::cout << "Input a Score " << i+1 << ":" ;
std::cin >> score[ i ];
while( score[ i ] > 4 || score[ i ] < 0 ){
std::cout <<" Invalid grade - please re-enter a grade"
<< " between 0 and 4.0 inclusive : ";
std::cin >> score[ i ];
}
sum = sum + score[ i ];
}
double average = sum / 6 ;
std::string grade;
if( average <= 4.0 && average > 3.2 ){
grade = "A";
}
if( average <= 3.2 && average > 2.4 ){
grade = "B";
}
if( average <= 2.4 && average > 1.6 ){
grade = "C";
}
if( average <= 1.6 && average > 0.8 ){
grade = "D";
}
if( average <= 0.8 && average > 0 ){
grade = "F";
}
// - Output the result: -
std::cout << "The average is " << average << "." <<std::endl;
std::cout << "The final letter grade is " << grade <<std::endl;
return 0;
} // function

int main(){
return ToBook();
} // main()


There is an secret message inside main().
 
A

asif929

John said:
Functions are covered in every C++ textbook. Answering your question would
mean either:

1. Giving an exceedingly long answer that explains functions.
2. Giving you code to answer your question, without you needing to
understand functions.

Neither seems appropriate.

You can find numerous tutorials on functions here:

http://www.google.com.au/search?hl=en&q=C+++function+tutorial&btnG=Search&meta=

You can also find a free downloadable textbook here:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html


Honestly! The project is due tommorow and i have tried my best to
understand the functions. It would take a while to go through the
everylink above. If you could atleast show me one code or give me some
idea of how to intailze the function, i will try to do it myself. I may
sound like a pushy, but believe me i dont know what to do here and its
due tommorow.
Thanks again.
 
J

John Carson

BobR said:
(e-mail address removed) wrote in message ...
Believe me, the way they teach it, i need to be genious to
understand. I dont have any any understanding of functions at all.
Belive me otherewise i wouldve done it by myself. Please help me in
some extent i could do something.


Yes you are right. All i know they declare the Global varaibles and
do loop before the main().
Please help me here.


#include <iostream>
#include <ostream>

int ToBook(){
const int Size = 6;
double score[ Size ];
double sum( 0);
for( int i( 0 ); i < Size; ++i ){
std::cout << "Input a Score " << i+1 << ":" ;
std::cin >> score[ i ];
while( score[ i ] > 4 || score[ i ] < 0 ){
std::cout <<" Invalid grade - please re-enter a grade"
<< " between 0 and 4.0 inclusive : ";
std::cin >> score[ i ];
}
sum = sum + score[ i ];
}
double average = sum / 6 ;
std::string grade;
if( average <= 4.0 && average > 3.2 ){
grade = "A";
}
if( average <= 3.2 && average > 2.4 ){
grade = "B";
}
if( average <= 2.4 && average > 1.6 ){
grade = "C";
}
if( average <= 1.6 && average > 0.8 ){
grade = "D";
}
if( average <= 0.8 && average > 0 ){
grade = "F";
}
// - Output the result: -
std::cout << "The average is " << average << "." <<std::endl;
std::cout << "The final letter grade is " << grade <<std::endl;
return 0;
} // function

int main(){
return ToBook();
} // main()


Doing people's homework for them is a really bad idea.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
 
A

asif929

John said:
BobR said:
(e-mail address removed) wrote in message ...
Didn't your class cover functions? Doesn't your textbook?
Believe me, the way they teach it, i need to be genious to
understand. I dont have any any understanding of functions at all.
Belive me otherewise i wouldve done it by myself. Please help me in
some extent i could do something.

I presume that what you are being asked to do is break up the
operations conducted in main() into several steps, each of which is
performed by a function

Yes you are right. All i know they declare the Global varaibles and
do loop before the main().
Please help me here.


#include <iostream>
#include <ostream>

int ToBook(){
const int Size = 6;
double score[ Size ];
double sum( 0);
for( int i( 0 ); i < Size; ++i ){
std::cout << "Input a Score " << i+1 << ":" ;
std::cin >> score[ i ];
while( score[ i ] > 4 || score[ i ] < 0 ){
std::cout <<" Invalid grade - please re-enter a grade"
<< " between 0 and 4.0 inclusive : ";
std::cin >> score[ i ];
}
sum = sum + score[ i ];
}
double average = sum / 6 ;
std::string grade;
if( average <= 4.0 && average > 3.2 ){
grade = "A";
}
if( average <= 3.2 && average > 2.4 ){
grade = "B";
}
if( average <= 2.4 && average > 1.6 ){
grade = "C";
}
if( average <= 1.6 && average > 0.8 ){
grade = "D";
}
if( average <= 0.8 && average > 0 ){
grade = "F";
}
// - Output the result: -
std::cout << "The average is " << average << "." <<std::endl;
std::cout << "The final letter grade is " << grade <<std::endl;
return 0;
} // function

int main(){
return ToBook();
} // main()


Doing people's homework for them is a really bad idea.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
Doing people's homework for them is a really bad idea.
Thank you anyways. Sorry for disturbing you. I am just not born to be a
programmer. I did best in my extent.
 
A

asif929

BobR said:
(e-mail address removed) wrote in message ...
Believe me, the way they teach it, i need to be genious to understand.
I dont have any any understanding of functions at all. Belive me
otherewise i wouldve done it by myself. Please help me in some extent i
could do something.


Yes you are right. All i know they declare the Global varaibles and do
loop before the main().
Please help me here.


#include <iostream>
#include <ostream>

int ToBook(){
const int Size = 6;
double score[ Size ];
double sum( 0);
for( int i( 0 ); i < Size; ++i ){
std::cout << "Input a Score " << i+1 << ":" ;
std::cin >> score[ i ];
while( score[ i ] > 4 || score[ i ] < 0 ){
std::cout <<" Invalid grade - please re-enter a grade"
<< " between 0 and 4.0 inclusive : ";
std::cin >> score[ i ];
}
sum = sum + score[ i ];
}
double average = sum / 6 ;
std::string grade;
if( average <= 4.0 && average > 3.2 ){
grade = "A";
}
if( average <= 3.2 && average > 2.4 ){
grade = "B";
}
if( average <= 2.4 && average > 1.6 ){
grade = "C";
}
if( average <= 1.6 && average > 0.8 ){
grade = "D";
}
if( average <= 0.8 && average > 0 ){
grade = "F";
}
// - Output the result: -
std::cout << "The average is " << average << "." <<std::endl;
std::cout << "The final letter grade is " << grade <<std::endl;
return 0;
} // function

int main(){
return ToBook();
} // main()


There is an secret message inside main().

Thanks for your response and help. The program above just work fine.
But, there is requirement that the program should be composed of
atleast three seperate functions in addition to main().

And the average grade suppose to get by loops.

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
and so on upto F.
 
A

asif929

BobR said:
(e-mail address removed) wrote in message ...
Didn't your class cover functions? Doesn't your textbook?
Believe me, the way they teach it, i need to be genious to understand.
I dont have any any understanding of functions at all. Belive me
otherewise i wouldve done it by myself. Please help me in some extent i
could do something.

I presume that what you are being asked to do is break up the operations
conducted in main() into several steps, each of which is performed by a
function

Yes you are right. All i know they declare the Global varaibles and do
loop before the main().
Please help me here.


#include <iostream>
#include <ostream>

int ToBook(){
const int Size = 6;
double score[ Size ];
double sum( 0);
for( int i( 0 ); i < Size; ++i ){
std::cout << "Input a Score " << i+1 << ":" ;
std::cin >> score[ i ];
while( score[ i ] > 4 || score[ i ] < 0 ){
std::cout <<" Invalid grade - please re-enter a grade"
<< " between 0 and 4.0 inclusive : ";
std::cin >> score[ i ];
}
sum = sum + score[ i ];
}
double average = sum / 6 ;
std::string grade;
if( average <= 4.0 && average > 3.2 ){
grade = "A";
}
if( average <= 3.2 && average > 2.4 ){
grade = "B";
}
if( average <= 2.4 && average > 1.6 ){
grade = "C";
}
if( average <= 1.6 && average > 0.8 ){
grade = "D";
}
if( average <= 0.8 && average > 0 ){
grade = "F";
}
// - Output the result: -
std::cout << "The average is " << average << "." <<std::endl;
std::cout << "The final letter grade is " << grade <<std::endl;
return 0;
} // function

int main(){
return ToBook();
} // main()


There is an secret message inside main().

Thanks for your response and help. The program above just work fine.
But, there is requirement that the program should be composed of
atleast three seperate functions in addition to main().

And the average grade suppose to get by loops.

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

After working for several hours, finally i would be able to declare the
first function. I pasted the code below.

Can somebody help me to figure out how to compute the average grade by
LOOPING THROUGH THE ARRAY.


#include <iostream>
#include <iomanip>

using namespace std;

double average(double SUM, double size)
{
double answer = SUM/size;
return answer;
}
int main()
{
const int SIZE = 6;
double score[SIZE];
double sum = 0;
string gletter;

cout << fixed << setprecision(2);
for (int grade = 0; grade < SIZE; ++grade)
{
cout <<" Please enter grade #"<< grade + 1 << ": ";
cin >> score[grade];
while( score[ grade ] > 4 || score[ grade ] < 0 )
{
cout <<" Invalid grade - please Re-enter a grade"
<< " between 0 and 4.0 inclusive : ";
cin >> score[ grade ];
}
sum = sum + score[grade];
}

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

// for (int grade = 0; grade < SIZE; ++grade)

}
 
B

BobR

(e-mail address removed) wrote in message ...
Thanks for your response and help. The program above just work fine.
But, there is requirement that the program should be composed of
atleast three seperate functions in addition to main().

I did one, now it's your turn.

Show us the revised code. Ask specific questions.
 
A

asif929

BobR said:
(e-mail address removed) wrote in message ...

I did one, now it's your turn.

Show us the revised code. Ask specific questions.

Thanks for your clue. Your clue was the reason i would be able to get
the average of the GPA or grades average in functions. The new codes
are below. But 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)
{
double answer = SUM/size;
return answer;


}


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

cout << fixed << setprecision(2);
for (int grade = 0; grade < SIZE; ++grade)
{
cout <<" Please enter grade #"<< grade + 1 << ": ";
cin >> score[grade];
while( score[ grade ] > 4 || score[ grade ] < 0 )
{
cout <<" Invalid grade - please Re-enter a grade"
<< " between 0 and 4.0 inclusive : ";
cin >> score[ grade ];
}
sum = sum + score[grade];
}


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


// for (int grade = 0; grade < SIZE; ++grade)


}
 
I

IR

b) the final letter grade according to the following
equivalences:

When you have a problem like that, try to rephrase it in plain
english.
In such a simple case, explaining that problem in english (not
mathematical notations) will even give you the C++ keywords you need
to use.


Cheers,
 
A

asif929

IR said:
When you have a problem like that, try to rephrase it in plain
english.
In such a simple case, explaining that problem in english (not
mathematical notations) will even give you the C++ keywords you need
to use.


Cheers,

I am getting the following compile error. can somebody help me with
passing the reference and pointers, from integers to double. Or any
other fix. Thanks

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'


The Code begins here:
Code:
 #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)
{
  for (int grade = 0; grade < SIZE; ++grade)
  {
     cout <<" Please enter grade #"<< grade + 1 << ": ";
     cin >> score[grade];
     while( score[ grade ] > 4 || score[ grade ] < 0 )
     {
               cout <<" Invalid grade - please Re-enter a grade"
                    << " between 0 and 4 inclusive : ";
   cin >> score[ grade ];
     }
  }
}

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;
    for (int grade = 0; grade < SIZE; ++grade)
 {   double diff = (score[grade] - averageScore);
      sum_of_squares +=(diff * diff);
    }
    stdDev = sqrt (averageScore / SIZE);
}
 
B

BobR

(e-mail address removed) wrote in message ...
I am getting the following compile error. can somebody help me with
passing the reference and pointers, from integers to double. Or any
other fix. Thanks

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

So, you take a look at 'enterScores' and how you called it:
void enterScores( int *score, const int SIZE);

It expects an pointer to type int, and an int.
const int SIZE = 6;
double score[SIZE];
enterScores( score, SIZE);

So, you just thought it would be cool if you tried to sneak in an
double('score')?!?
Go through your code, top to bottom, and look for those mismatches. Fix 'em.
 
A

asif929

BobR said:
(e-mail address removed) wrote in message ...
I am getting the following compile error. can somebody help me with
passing the reference and pointers, from integers to double. Or any
other fix. Thanks

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

So, you take a look at 'enterScores' and how you called it:
void enterScores( int *score, const int SIZE);

It expects an pointer to type int, and an int.
const int SIZE = 6;
double score[SIZE];
enterScores( score, SIZE);

So, you just thought it would be cool if you tried to sneak in an
double('score')?!?
Go through your code, top to bottom, and look for those mismatches. Fix 'em.


Thanks Bob for you help. But, i figured it out by myself earlier. Well,
it's been good straight 7 hours struggling to create a program. As you
can see majority of the part has been completed. The only problem i am
getting that grade 'letter' is not showing in the output. Even though,
the program is compiling and managing to output the average and
standard deviation.

Can you look at the coding below and point out the error(s).

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
&);
void printLetterGrade(char, 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);
printLetterGrade(letter, averageScore);

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)
{
for (int grade = 0; grade < SIZE; ++grade)
{
cout <<" Please enter grade #"<< grade + 1 << ": ";
cin >> score[grade];

}
}
}

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

averageScore = sum/SIZE;


}

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

{
double sum_of_squares = 0;
for (int grade = 0; grade < SIZE; ++grade)
{ 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';
}
}
 
B

BobR

(e-mail address removed) wrote in message ...
Thanks Bob for you help. But, i figured it out by myself earlier. Well,
it's been good straight 7 hours struggling to create a program. As you
can see majority of the part has been completed. The only problem i am
getting that grade 'letter' is not showing in the output. Even though,
the program is compiling and managing to output the average and
standard deviation.
Can you look at the coding below and point out the error(s).
Thanks.
// >#include <math.h>
#include said:
void printLetterGrade(char, 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);
char letter;
printLetterGrade(letter, averageScore);
cout << "average score is : " << averageScore <<"."<< endl;
cout << "standard deviation is : " << stdDev <<"."<< endl;
cout << "The final grade is: " << letter << endl;
}

void printLetterGrade(char letter, double & averageScore){

Here, a copy of 'letter' has been made.
if (averageScore <= 4.0 && averageScore > 3.2){
letter = 'A';

You store a character in 'letter'.

and as soon as you hit the next line, 'letter' is destroyed.

You passed the char by value, when it seems you want to pass by non-const
reference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top