By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,440 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,440 IT Pros & Developers. It's quick & easy.

Need Help with the functions program

P: n/a
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[i] 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[i];

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

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

Dec 9 '06 #1
Share this Question
Share on Google+
21 Replies


P: n/a
<as*****@hotmail.comwrote in message
news:11*********************@l12g2000cwl.googlegro ups.com
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[i] - average)*(score[i]-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[i] - avg)2
I=0___________________

n-1
where n is the number of values that were averaged (6 in this case),
x[i] 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[i];

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

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

--
John Carson


Dec 9 '06 #2

P: n/a

John Carson wrote:
<as*****@hotmail.comwrote in message
news:11*********************@l12g2000cwl.googlegro ups.com
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[i] - average)*(score[i]-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[i] - avg)2
I=0___________________

n-1
where n is the number of values that were averaged (6 in this case),
x[i] 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[i];

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

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


--
John Carson
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.

Dec 9 '06 #3

P: n/a

John Carson wrote:
<as*****@hotmail.comwrote in message
news:11*********************@l12g2000cwl.googlegro ups.com
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[i] - average)*(score[i]-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[i] - avg)2
I=0___________________

n-1
where n is the number of values that were averaged (6 in this case),
x[i] 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[i];

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

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


--
John Carson
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.

Dec 9 '06 #4

P: n/a

John Carson wrote:
<as*****@hotmail.comwrote in message
news:11*********************@l12g2000cwl.googlegro ups.com
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[i] - average)*(score[i]-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[i] - avg)2
I=0___________________

n-1
where n is the number of values that were averaged (6 in this case),
x[i] 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[i];

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

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


--
John Carson
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.

Dec 9 '06 #5

P: n/a
<as*****@hotmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com
John Carson wrote:
>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.
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=e...G=Search&meta=

You can also find a free downloadable textbook here:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
--
John Carson

Dec 9 '06 #6

P: n/a

as*****@hotmail.com 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().

--
Bob R
POVrookie
--
Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html
Dec 9 '06 #7

P: n/a

John Carson wrote:
<as*****@hotmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com
John Carson wrote:
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.

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=e...G=Search&meta=

You can also find a free downloadable textbook here:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
--
John Carson

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.

Dec 9 '06 #8

P: n/a
"BobR" <Re***********@worldnet.att.netwrote in message
news:QL*********************@bgtnsc04-news.ops.worldnet.att.net
as*****@hotmail.com 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-lit...t.html#faq-5.2
--
John Carson
Dec 9 '06 #9

P: n/a

John Carson wrote:
"BobR" <Re***********@worldnet.att.netwrote in message
news:QL*********************@bgtnsc04-news.ops.worldnet.att.net
as*****@hotmail.com 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-lit...t.html#faq-5.2
--
John Carson
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.

Dec 9 '06 #10

P: n/a

BobR wrote:
as*****@hotmail.com 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().

--
Bob R
POVrookie
--
Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html
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.

Dec 9 '06 #11

P: n/a

as*****@hotmail.com wrote:
BobR wrote:
as*****@hotmail.com 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().

--
Bob R
POVrookie
--
Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

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)

}

Dec 9 '06 #12

P: n/a

John Carson wrote in message ...
>
Doing people's homework for them is a really bad idea.
I agree. That's why I only gave OP a clue.

--
Bob R
POVrookie
Dec 9 '06 #13

P: n/a

as*****@hotmail.com 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.

--
Bob R
POVrookie
Dec 9 '06 #14

P: n/a

BobR wrote:
as*****@hotmail.com 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.

--
Bob R
POVrookie
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)
}

Dec 9 '06 #15

P: n/a
IR
as*****@hotmail.com wrote:
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
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,
--
IR
Dec 9 '06 #16

P: n/a

IR wrote:
as*****@hotmail.com wrote:
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

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,
--
IR
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:
Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>
  2. #include <iomanip>
  3. #include <math.h>
  4. using namespace std;
  5. void enterScores(int *, const int);
  6. void calculateAverage(int *, const int, double &);
  7. void calculateStandardDeviation(int *, const int, double &, double &);
  8. int main()
  9. {
  10. const int SIZE = 6;
  11. double score[SIZE];
  12. double averageScore = 0.0;
  13. double stdDev = 0.0;
  14. cout << fixed << setprecision(2);
  15. enterScores(score, SIZE);
  16. calculateAverage(score, SIZE, averageScore);
  17. calculateStandardDeviation(score, SIZE, averageScore, stdDev);
  18. cout << "average score is : " << averageScore << endl;
  19. cout << "standard deviation is : " << stdDev << endl;
  20. }
  21.  
  22. void enterScores(int * score, const int SIZE)
  23. {
  24. for (int grade = 0; grade < SIZE; ++grade)
  25. {
  26. cout <<" Please enter grade #"<< grade + 1 << ": ";
  27. cin >score[grade];
  28. while( score[ grade ] 4 || score[ grade ] < 0 )
  29. {
  30. cout <<" Invalid grade - please Re-enter a grade"
  31. << " between 0 and 4 inclusive : ";
  32. cin >score[ grade ];
  33. }
  34. }
  35. }
  36.  
  37. void calculateAverage(int * score, const int SIZE, double &
  38. averageScore)
  39. {
  40. int sum = 0;
  41. for(int i = 0; i < SIZE; ++i)
  42. sum += score[i];
  43.  
  44. averageScore = sum/SIZE;
  45. }
  46.  
  47. void calculateStandardDeviation(int * score, const int SIZE, double &
  48. averageScore, double & stdDev)
  49.  
  50. {
  51. int sum_of_squares = 0;
  52. for (int grade = 0; grade < SIZE; ++grade)
  53. {   double diff = (score[grade] - averageScore);
  54. sum_of_squares +=(diff * diff);
  55. }
  56. stdDev = sqrt (averageScore / SIZE);
  57. }
  58.  
Dec 9 '06 #17

P: n/a

as*****@hotmail.com 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.

--
Bob R
POVrookie
Dec 9 '06 #18

P: n/a

BobR wrote:
as*****@hotmail.com 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.

--
Bob R
POVrookie

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[i];

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';
}
}

Dec 10 '06 #19

P: n/a

as*****@hotmail.com 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 <cmath>
>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.

--
Bob R
POVrookie
Dec 10 '06 #20

P: n/a

BobR wrote:
as*****@hotmail.com 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 <cmath>
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.

--
Bob R
POVrookie

Thanks everybody for the help especially Bob R. 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 tires and
satisfied with the work i done:)

Below are the codes i am pasting 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
&);
char printLetterGrade (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);
printLetterGrade(averageScore);
char letter = printLetterGrade(averageScore);

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)
{
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(double * score, const int SIZE, double &
averageScore)
{
double sum = 0;
for(int i = 0; i < SIZE; ++i)
sum += score[i];

averageScore = sum/SIZE;
}

char printLetterGrade(double averageScore)
{
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;

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

Dec 10 '06 #21

P: n/a
IR
OK... a few tips now that you got it working... (just to make the
code cleaner, it doesn't change anything to the functionality)

as*****@hotmail.com wrote:
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;
}
Why not return the average score from the function, like you did in
printLetterGrade?

char printLetterGrade(double averageScore)
{
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;
}
If averageScore is between 4.0 (included) and 3.2 (excluded), then
it CAN'T be in one of the other intervals. If the final grade is A,
you don't need to test for B, C, ...
So instead of a series of unconnected "if", you should really have
if / else if / ...

In your case, performance doesn't really matter, but keep in mind
that whenever a project gets big enough (eg. multithreaded server
with hundreds or thousands of clients...), every single clock cycle
does matter.

Although "premature optimization is the root of all evil", this kind
of construct is very easy to spot, it should become a "second
nature"...

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 - 1));
}
Same comment as for calculateAverage: why not just make stdDev the
return value? And you don't need to pass averageScore as a reference
(just looking at the function declaration would make me think that
averageScore is modified by the function, which obviously isn't the
case).
Although your program is not yet homogeneous, I guess you did a
pretty god job until now. :)

However, keep in mind... those tips could make a difference between
a C and a A. And in real world programming, they really make a
difference between an easily maintainable program, and a hard-to-
understand code (remember, you're not just writing code right now,
someone will have to mantain it in 1 or 10 years...).

Anyway, keep up the good work :)
Cheers,
--
IR
Dec 10 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.