CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Jun 2013
    Posts
    22

    Question Creating a Function That Needs To Determine Perfect Scores

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

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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:-

    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);
    Last edited by John E; June 25th, 2013 at 02:41 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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?

  4. #4
    Join Date
    Jun 2013
    Posts
    22

    Re: Creating a Function That Needs To Determine Perfect Scores

    Quote Originally Posted by GCDEF View Post
    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"...

  5. #5
    Join Date
    Jun 2013
    Posts
    22

    Question Re: Creating a Function That Needs To Determine Perfect Scores

    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.
    Last edited by veryNew; June 25th, 2013 at 12:12 PM.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Creating a Function That Needs To Determine Perfect Scores

    Quote Originally Posted by veryNew View Post
    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.

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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];
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  8. #8
    Join Date
    Jun 2013
    Posts
    22

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

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  10. #10
    Join Date
    Jun 2013
    Posts
    22

    Re: Creating a Function That Needs To Determine Perfect Scores

    Quote Originally Posted by GCDEF View Post
    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 View Post
    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".

  11. #11
    Join Date
    Jun 2013
    Posts
    22

    Re: Creating a Function That Needs To Determine Perfect Scores

    Quote Originally Posted by GCDEF View Post
    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

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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;
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Jun 2013
    Posts
    22

    Re: Creating a Function That Needs To Determine Perfect Scores

    Quote Originally Posted by 2kaud View Post
    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.

  15. #15
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Thumbs up Re: Creating a Function That Needs To Determine Perfect Scores

    Quote Originally Posted by veryNew View Post
    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!
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured