# Creating a Function That Needs To Determine Perfect Scores

Show 50 post(s) from this thread on one page
Page 1 of 2 12 Last
• June 24th, 2013, 11:37 PM
veryNew
Creating a Function That Needs To Determine Perfect Scores
:confused:This program is incomplete as I am having difficulty creating the function that needs to find the number of perfect scores entered by the user. I have everything but the function complete ,here is my code:

Code:

```// Write a modular program that accepts at least 10 integer // test scores from the user and stores them in an array. // The main should display how many perfect scores were // entered (i.e., scores of 100), using a value-returning countPerfect // function to help it. // Input validation: Do not accept scores less than 0 or greater than 100. #include <iostream> using namespace std; int countPerfect(int intArray[], int);          // Function prototype int main() {         const int TEST_SCORES = 10;         int testScores[TEST_SCORES],            // Holds test scores for 10 tests                 perfect;         cout << "Please enter " << TEST_SCORES << " test scores\n";         cout << endl;         for(int index = 0; index < TEST_SCORES; index ++)         {                 cout << "Test " << (index + 1) << ": ";                 cin >> testScores[index];                 cout << endl;                 while((testScores[index] < 0) || (testScores[index] > 100))                 {                         cout << "Invalid score, please enter a number greater than 0 or less than 100 \n";                         cin >> testScores[index];                 }         }         cout << "Here are the scores you entered\n";         cout << endl;                 for(int index = 0; index < TEST_SCORES; index ++)         {                 cout << "Test " << (index + 1) << " : " << testScores[index] << endl;         }         cout << endl;         cout << "The total number of perfect scores was: \n";         countPerfect(testScores, TEST_SCORES);                         cout << "There were " << perfect << " scores of 100.\n";         cout << endl;         system("pause");         return 0; }```
• June 25th, 2013, 02:37 AM
John E
Re: Creating a Function That Needs To Determine Perfect Scores
You're asking how to write the function countPerfect - is that right? Basically, there are four steps:-

Quote:

1) Initialize a return value to zero
2) Loop around the entries in testScores examining each value, one at a time
3) If the entry value is 100, increment your return value
4) When all the entries have been examined, return the return value
Given that you already pass TEST_SCORES into the countPerfect function, a 'for' loop will probably be what you need. Have a try and let us see what you come up with.

[Edit...]
Oh... and the call to countPerfect will probably need to look like this:-

Code:

`perfect = countPerfect(testScores, TEST_SCORES);`
• June 25th, 2013, 07:03 AM
GCDEF
Re: Creating a Function That Needs To Determine Perfect Scores
You don't need a separate function to count them. Why not just increment a counter when the user inputs the scores, or when you loop through them to output them?
• June 25th, 2013, 11:15 AM
veryNew
Re: Creating a Function That Needs To Determine Perfect Scores
Quote:

Originally Posted by GCDEF
You don't need a separate function to count them. Why not just increment a counter when the user inputs the scores, or when you loop through them to output them?

The assignment instructs me to have a function: "using a value-returning countPerfect function to help it"...
• June 25th, 2013, 12:09 PM
veryNew
Re: Creating a Function That Needs To Determine Perfect Scores
:confused:Let me break this down into what you described above and see where I am making mistakes:

1) Initialize a return value to zero
Code:

`int perfect = 0;`
2) Loop around the entries in testScores examining each value, one at a time
Code:

`for(int index = 0; index < grade; index ++)`
3) If the entry value is 100, increment your return value
Code:

```if(index == 100) perfect ++;```
4) When all the entries have been examined, return the return value
Code:

`return perfect;`
This is my function:

Code:

```int countPerfect(int nums[], int grade) {         int perfect = 0;         for(int index = 0; index < grade; index ++)         {                                         if(index == 100)                                 perfect ++;                         perfect = nums[index];                        }                 return perfect;```
I also want to get an else statement in there in case the user did not enter a perfect score at all, but I am not sure if it will work while in the loop, would it need to go after it?

The display shows this: "There were 100 perfect scores of 100", which obviously is wrong when I input all the scores as perfect scores. So I have a few logical errors that need fixing.
• June 25th, 2013, 12:18 PM
GCDEF
Re: Creating a Function That Needs To Determine Perfect Scores
Quote:

Originally Posted by veryNew
This is my function:

Code:

```int countPerfect(int nums[], int grade) {         int perfect = 0;         for(int index = 0; index < grade; index ++)         {                                         if(index == 100)                                 perfect ++;                         perfect = nums[index];         }                 return perfect;```
I also want to get an else statement in there in case the user did not enter a perfect score at all, but I am not sure if it will work while in the loop, would it need to go after it?

The display shows this: "There were 100 perfect scores of 100", which obviously is wrong when I input all the scores as perfect scores. So I have a few logical errors that need fixing.

You shouldn't be testing index. Index is what it describes, and index into the array. You want to test the array elements, not the index.

What are you trying to accomplish with the line I bolded?

I'd give the variable you named "grade" a more meaningful name.
• June 25th, 2013, 12:19 PM
John E
Re: Creating a Function That Needs To Determine Perfect Scores
I haven't tested it but this is probably what you need:-

Code:

```int countPerfect(int nums[], int grade) {         int perfect = 0;         for(int index = 0; index < grade; index ++)         {                                         if(nums[index] == 100)                                 perfect ++;         }                 return perfect; }```
You don't need this line:-
Code:

`                        perfect = nums[index];`
• June 25th, 2013, 01:23 PM
veryNew
Re: Creating a Function That Needs To Determine Perfect Scores
Yeah that code worked, thank you. Here is my complete code:
:)
Code:

```#include <iostream> using namespace std; int countPerfect(int intArray[], int);          // Function prototype int main() {         const int TEST_SCORES = 10;         int testScores[TEST_SCORES],            // Holds test scores for 10 tests                 perfect;         cout << "Please enter " << TEST_SCORES << " test scores\n";         cout << endl;         for(int index = 0; index < TEST_SCORES; index ++)         {                 cout << "Test " << (index + 1) << ": ";                 cin >> testScores[index];                 cout << endl;                 while((testScores[index] < 0) || (testScores[index] > 100))                 {                         cout << "Invalid score, please enter a number greater than 0 or less than 100 \n";                         cin >> testScores[index];                 }         }         cout << "Here are the scores you entered\n";         cout << endl;                 for(int index = 0; index < TEST_SCORES; index ++)         {                 cout << "Test " << (index + 1) << ": " << testScores[index] << endl;         }         cout << endl;                 perfect = countPerfect(testScores, TEST_SCORES);         cout << "The total number of perfect scores was: " << perfect << endl;         cout << endl;         system("pause");         return 0; } int countPerfect(int nums[], int numScore) {         int perfect = 0;         for(int index = 0; index < numScore; index ++)         {                                         if(nums[index] == 100)                                 perfect ++;         }                 return perfect; }```
• June 25th, 2013, 01:26 PM
GCDEF
Re: Creating a Function That Needs To Determine Perfect Scores
Looks good, and kudos for using code tags, indentation and posting readable code in your first post here.
• June 25th, 2013, 01:35 PM
veryNew
Re: Creating a Function That Needs To Determine Perfect Scores
Quote:

Originally Posted by GCDEF
What are you trying to accomplish with the line I bolded?.

Code:

`perfect = nums[index];`
Not really sure, I am new so I was just playing around and seeing what I could come up with

Quote:

Originally Posted by GCDEF
I'd give the variable you named "grade" a more meaningful name.

I tried this one:

Code:

`int countPerfect(int nums[], int numScore)`
So it represents the number of scores or elements that were entered into the array.

The program works now, but is there any way I can add an "else" statement to it in the case the user enters not one single perfect score? I am not sure where to put it, as it wont' be able to go into the loop itself. Otherwise it still counts the perfect scores and will read 0 if it doesn't find any. I just want a statement to display: "There were no perfect scores entered".
• June 25th, 2013, 01:40 PM
veryNew
Re: Creating a Function That Needs To Determine Perfect Scores
Quote:

Originally Posted by GCDEF
Looks good, and kudos for using code tags, indentation and posting readable code in your first post here.

Thank you, very much appreciated. I am learning from my past mistakes from other forums that I am registered with where I have been scolded in the past:)
• June 25th, 2013, 01:41 PM
2kaud
Re: Creating a Function That Needs To Determine Perfect Scores
try this modification

Code:

```        perfect = countPerfect(testScores, TEST_SCORES);         if (perfect > 0) {                 cout << "The total number of perfect scores was: " << perfect << endl;         } else {                 cout << "There were no perfect scores entered" << endl;         }         cout << endl;```
• June 25th, 2013, 01:45 PM
2kaud
Re: Creating a Function That Needs To Determine Perfect Scores
As you are learning c++, you might like to look at these sites

http://www.learncpp.com/
http://www.cplusplus.com/doc/tutorial/

Happy programming!:wave:
• June 25th, 2013, 02:01 PM
veryNew
Re: Creating a Function That Needs To Determine Perfect Scores
Quote:

Originally Posted by 2kaud
try this modification

Code:

```        perfect = countPerfect(testScores, TEST_SCORES);         if (perfect > 0) {                 cout << "The total number of perfect scores was: " << perfect << endl;         } else {                 cout << "There were no perfect scores entered" << endl;         }         cout << endl;```

That worked to perfection, thank you very much. I just wasn't sure where to put the else statement, as I knew you wouldn't want to put it in the loop, it had to go outside somewhere. I just never thought of putting it in main with the conditional statement after the call.
• June 25th, 2013, 05:06 PM
Eri523
Re: Creating a Function That Needs To Determine Perfect Scores
Quote:

Originally Posted by veryNew
I just wasn't sure where to put the else statement, as I knew you wouldn't want to put it in the loop, it had to go outside somewhere. I just never thought of putting it in main with the conditional statement after the call.

This is a perfectly simple, in a textbook way, example of why it's reasonable and important to separate business logic (counting the perfect scores achieved) from presentation (simply the screen output here). The business logic here is the countPerfect() function, and, by returning numeric (integer) zero in case not a single perfect score was entered, it does exactly what it's supposed to. In contrast, the presentation logic resides in main() here, and this is what is responsible for taking care of dispaying a special text message to the user in case not a single perfect score was entered.

Actually, kudos to your teacher in case that this learning experience was just intended that way! Practice usually shows, unfortunately, that most teachers aren't so good. Of course that doesn't diminish in any way the kudos to you for solving the assignment the way you did! :thumb:
Show 50 post(s) from this thread on one page
Page 1 of 2 12 Last