Thread: C++ Simple Basic Function Program - Please help

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    7

    Question C++ Simple Basic Function Program - Please help

    I am trying to figure out how to create a very basic C++ program based on functions that calculates the average of a series of 5 tests scores, where the lowest score in the series is dropped. I am using MS Visual C++ 6.0 This is what I'd like to do:
    1) I want to create a function "getValues" that asks user for 5 tests scores and store them in variables.
    2) then use a function "findLowest" to determine which of the 5 test scores is the lowest and return that value.
    3) Finally, use a function called "calcAverage" to calculate and display the average for the 4 highest scores.
    Also I should not accept test scores higher then 100 or lower than 0.
    Please any comments, help, suggestions, etc will be greatly appreciate it.
    Thanks in advance,
    Jenny
    P.S.: This is what I got so far, but I know it is all wrong:

    #include <iostream>
    using namespace std;

    void getValues(int, int, int, int, int);
    void findLowest(int, int, int, int, int);

    int main()
    {

    getValues(int, int, int, int, int);


    }

    void getValues (int test1, int test2, int test3, int test4, int test5)
    {

    cout << "Please enter 5 test scores: ";
    cin >> test1 >> test2 >> test3 >> test4 >> test5;
    findlowest(lowest);
    }


    void findLowest (int test1, int test2, int test3, int test4, int test5)
    {

    }

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    write a function called min

    like this:

    int min(int value1, int value2) {
    return minimumofbothvalues;
    };

    but that code is incomplete, beware!!! you must fix it to work!!!

    then, use this 'min' function in findlowest in order to succeed in your code

    + your invocation of 'getvalues' in main is broken

    edit: changed mind
    .sect signature

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    In order for main() to pass 5 ints to getvalues you need to delcare them first. Then put their names in the parenthese when you call the function, not their type.


    findlowest() is expecting five values, but you are passing only one. findvalue is actually expecting 5 ints but you are actually passing a variable that hasn't been declared yet so it doesn't even have a type.

    In order to figure out what code to put in the body of findlowest() you should write out how you would do this with pen and paper first (psuedocode). Then change that into working code. If you want to post your pseudocode for review, feel free. Otherwise, post your attempts otherwise.

    When you are writing a function you need to know what information you need to do the task I want to do. Where am I going to get that information--gobal, parameters, local variables. How am I going to manipulate that information--code in the body of the function. What am I going to do with the information once I've manipulated it--return value, pass parameters by reference rather than by value, or just do something internal to function only like write to file or write to screen.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    Thanks guys for your replies. Im still kinda lost here. OK, first of all I should ask the user to input 5 test scores by doing this?:

    Code:
    #include <iostream>
    using namespace std;
    
    void getValues(int, int, int, int, int);
    void findLowest(int, int, int, int, int);
    
    int main()
    {
    	int test1, int test2, int test3, int test4, int test5;
    	cout << "Please enter 5 test scores: ";
    	cin >> test1 >> test2 >> test3 >> test4 >> test5;
    	getValues(int test1, int test2, int test3, int test4, int test5);
    Is this correct? or do I take the "int's" out of the parenthesis in "get Values"? I am reading a book called "Starting out with C++" on "Local and Global varibles" as well as the "passing info to parameters by value" and the "return statement" but i'm in great confusion at this moment and can't put the peaces all together. I apologize for being such a C++ noob.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    cin >> test1 >> test2 >> test3 >> test4 >> test5;
    getValues(test1, test2,test3, test4, test5);


    this is called calling getValues. You only pass the name of the variables, not the type of the variables when doing this.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    OK after reading some more, I believe this is the first step:
    Code:
    #include <iostream>
    using namespace std;
    
    void getValues(int, int, int, int, int);
    
    int main()
    {
         int test1, int test2, int test3, int test4, int test5;
         cout << "Please enter 5 test scores: ";
         cin >> test1 >> test2 >> test3 >> test4 >> test5;
         getValues(test1, test2, test3, test4, test5);
         lowestTest = findLowest()
    }	
    
    void getValues(int test1, int test2, int test3, int test4, int test5)
    {
    Now I am stuck on what to do next. What do I do inside function "getValues"? Do I use "if" statements for input validation such as:
    Code:
    void getValues(int test1, int test2, int test3, int test4, int test5)
    {
         if (test1 < 0 || test1 > 100)
         {
              cout << "Test Scores must be between 0 and 100"\n";
              return;
         }
         else if (test2 < 0 || test2 > 100)
        {
              cout << "Test Scores must be between 0 and 100"\n";
              return;
         }
         //and so on until test5
         //then...???
    Any comments???

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    change this:
    int test1, int test2, int test3, int test4, int test5;

    to this:

    int test1, test2, test3, test4, test5;

    or this:

    int test1;
    int test2;
    int test3;
    int test4;
    int test5;

    put this:

    cout << "Please enter 5 test scores: ";
    cin >> test1 >> test2 >> test3 >> test4 >> test5;

    before the if statement in getValue();


    change this:
    lowestTest = findLowest();

    to this:

    int lowestTest = findLowest(test1, test2, test3, test4, test5);

    and place it after you've tested all 5 ints for validity in getValues();

    drop the else in this line:

    else if (test2 < 0 || test2 > 100)
    Last edited by elad; 04-12-2004 at 11:39 AM.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>lowestTest = findLowest()
    3 problems with this line.
    1) You haven't declared lowestTest
    2) You don't have findLowest() declared at the top of your file
    3) You don't have a semicolon at the end of your line Never forget those.

    Ok, the name of getValues suggests that that's where you're getting user input. So you might want to put the cout and cin lines in the function getValues() instead of just in main(). The checking for valid input is good. But instead of returning, you need to loop when you get bad input (which you couldn't do since you did the input in main(). Here's some revised code:

    Code:
    #include <iostream>
    using namespace std;
     
    void getValues(int, int, int, int, int);
     
    int main()
    {
    	 int test1, test2, test3, test4, test5;
    	 getValues(test1, test2, test3, test4, test5);
    	 // find lowest value...
    }
     
    void getValues(int& test1, int& test2, int& test3, int& test4, int& test5)
    {
     bool loop = true;
     do
     {
      cout << "Please enter 5 test scores: "; cin >> test1 >> test2 >> test3 >> test4 >> test5; if (test1 < 0 || test1 > 100 || test2 < 0 || test2 > 100 || test3 < 0 || test3 > 100 || test4 < 0 || test4 > 100 || test5 < 0 || test5 > 100) { cout << "Test Scores must be between 0 and 100"\n"; } else loop = false; }while(loop);}
    I changed the parameters for getValues to references, so that changing them in the function would actually change them in main too. Also, I put it in a loop so that only valid input would be accepted, and it would keep asking for input until the input was ok. Then for findLowest, I would suggest you make the min() function like someone suggested, and return min(test1, min(test2, min(test3, min(test4, test5)))). Hope this helps!

    **EDIT** AHHHH WTF major bug, that was all properly formatted when I typed it in!!!
    Ok i'll stick that on separate lines AGAIN...

    **EDIT2** NVM, it's gonna take a year to re-indent it too. Jeez, can someone else indent that for me? lol...
    Last edited by Hunter2; 04-12-2004 at 11:51 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    In C you need to declare all variables at the start of a function. In C++ you only need to declare the variables before you use them. Traditionally, this is done at the start of the function, but it need not be, and at times, it is better for clarity to declare them just in the nick of time.

    Don't try to use passing by refernce if the text you are reading from is having you pass by value only at this point. While passing by reference would make things a little more sophisticated, it isn't apparently available to you yet. You can write the program without passing by reference if you pay attention to what's being passed to where, when.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    7

    Question

    Thanks so much for your help Elad and Hunter2. I have my Yahoo messenger ON that alerts me when I get a message. OK, let's see if I understood:
    Code:
    #include <iostream>
    using namespace std;
    
    void getValues(int, int, int, int, int);
    void findLowest(int);
    void calcAverage(float);
    
    int main()
    {
         int test1, test2, test3, test4, test5;
         getValues(test1, test2, test3, test4, test5);
    	 int lowestTest = findLowest(test1, test2, test3, test4, test5);
    	 float average = calcAverage(?)//will figure this out later on
    }	
    
    void getValues(int& test1, int& test2, int& test3, int& test4, int& test5);
    {
    	bool loop = true;
    	do
        {
    	cout << "Please enter 5 test scores: ";
    	cin >> test1 >> test2 >> test3 >> test4 >> test5;
    	if (test1 < 0 || test1 > 100 || test2 < 0 || test2 > 100 
    	|| test3 < 0 || test3 > 100 || test4 < 0 || test4 > 100 || test5 < 0 || test5 > 100)
    	{
    		cout << "Test Scores must be between 0 and 100"\n";
        }
    	else              
    	{
    	loop = false;
    	}
    	while(loop);
    	}
    }
    
    void findLowest(int test1, int test2, int test3, int test4, int test5)
    {
    return findLowest(test1, findLowest(test2, findLowest(test3, findLowest(test4, test5))));
    }
    Is this correct so far? I know am still having a hard time figuring out how to find the lowest score out of the 5. Please need help.
    Last edited by eclaixp; 04-12-2004 at 12:34 PM.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Sorry if I didn't make myself clear about findLowest. For simplicity's sake, you'll need 2 functions to do it, like this:
    Code:
    int min(int a, int b)
    {
    if(a < b)
    return a;
    else
    return b;
    }
     
    int findLowest(int test1, int test2, int test3, (...))
    {
    return min(test1, min(test2, min(test3, min(test4, test5))));
    }
    That finds which is lower, test4 or test5, and compares that to test3, takes the lower one and compares it to test2, etc. until you get to test1, and after it compares against test1, you'll have the lowest number out of all 5.

    >>void getValues(int, int, int, int, int);
    Ooops, forgot to change that in my example. If you're going to use references for parameters, you'll need to update this line too (int& instead of just int).
    void getValues(int&, int&, int&, int&, int&);

    Code:
    	else 
    	{
    		loop = false;
    	}
    	while(loop); //--\
    }	  // <-----------/
    The while(loop); is part of the do-while loop. Because of this, you'll need to put it after the closing curly brace of the do.
    Code:
    do
    {
    	(...)
    	else 
    	{
    		loop = false;
    	}
    }while(loop);
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <iostream>using namespace std;
    void getValues(int, int, int, int, int); 
    //note change in next line
    int findLowest(int, int, int, int, int);
    void calcAverage(float);
    int main()
    { 
    int test1, test2, test3, test4, test5; 
    getValues(test1, test2, test3, test4, test5); 
    //int lowestTest = findLowest(test1, test2, test3, test4, test5); 
    //float average = calcAverage(?)//will figure this out later on
    } 
     
    //drop the semicolon at the end of the next line
    void getValues(int& test1, int& test2, int& test3, int& test4, int& test5)
    { 
    bool loop = true; 
    do 
    { 
    cout << "Please enter 5 test scores: "; 
    cin >> test1 >> test2 >> test3 >> test4 >> test5; 
    if (test1 < 0 || test1 > 100 || test2 < 0 || test2 > 100 || test3 < 0 || test3 > 100 || test4 < 0 || test4 > 100 || test5 < 0 || test5 > 100) 
    { 
    cout << "Test Scores must be between 0 and 100"\n"; 
    } 
    else 
    { 
    loop = false; 
    }while(loop); 
    } 
    //put stuff here since passing by value only 
    int lowest = FindLowest(test1, test2, test3, test4, test5); 
    cout << "lowest = " << lowest << endl; 
     
    calcAvg(test1, test2, test3, test4, test5);
    }
     
    //note change here
    int findLowest(int test1, int test2, int test3, int test4, int test5)
    {
    //forget the following line, try again.
    return findLowest(test1, findLowest(test2, findLowest(test3, findLowest(test4, test5))));
     
    instead declare a local variable of type int to return to getValue
    assign test1 to variable
    if test1 less than variable, 
    then assign test to variable
    continue through test five
    return local variable
    }
    calcAvg will need all five ints to determine the average of the five. If you make it void return type then you should display the result in calcAvg. If you want to returna result to getValue()from which it is called then you need to declare a local variable of type float, calculate the avg of the 5 ints, assign that value to the float and return the float. If you return the float, then you need to change the declaration signature. Make sure the declaration signature and the definition signature agree, except the terminating semicolon in the declaration isn't in the definitionsignature.
    Last edited by elad; 04-12-2004 at 04:10 PM.

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    I am confused by your last reply Elad This is what I have so far, please guys read my comments in the code, I will need help there:

    Code:
    #include <iostream>
    using namespace std;
    
    
    //Are the function prototypes fine???
    void getValues(int&, int&, int&, int&, int&);
    int findLowest(int, int, int, int, int);
    int min(int, int);
    int calcAverage(int, int, int, int);
    
    int main()
    {
    	int test1, test2, test3, test4, test5;
    	getValues(test1, test2, test3, test4, test5);
    	int lowestTest = findLowest(test1, test2, test3, test4, test5);
    	float average = calcAverage(?, ?, ?, ?);//how do I make the 4 highest scores be inside parenthesis???
    	return 0;
    }	
    
    void getValues(int& test1, int& test2, int& test3, int& test4, int& test5);
    {
    	bool loop = true;
    	do
        {
    	cout << "Please enter 5 test scores: ";
    	cin >> test1 >> test2 >> test3 >> test4 >> test5;
    	if (test1 < 0 || test1 > 100 || test2 < 0 || test2 > 100 
    	|| test3 < 0 || test3 > 100 || test4 < 0 || test4 > 100 || test5 < 0 || test5 > 100)
    	{
    		cout << "Test Scores must be between 0 and 100"\n";
        }
    	else              
    	{
    	loop = false;
    	}
    	}while(loop);
    }
    //Is this correct?
    int min(int a, int b)
    {
    if(a < b)
    return a;
    else
    return b;
    }
     
    int findLowest(int test1, int test2, int test3, int test4, int test5)
    {
    return min(test1, min(test2, min(test3, min(test4, test5))));
    }
    
    int calcAverage (int ?, int ?, int ?, int ?)//need help here as well
    {
    return (? + ? + ? + ?) / 4;
    }
    
    }
    Someone please guide me thru this!!!!

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ah, well everything looks fine up till the calcAverage part. To be honest, I can't think of any very efficient/neat way of doing it so that you calculate the average of the highest marks. Actually, in this case I think it would be easier to solve your problem using arrays or vectors instead of 5 different variables. *Sigh* lot more complicated than I thought originally. Well, here's some code:
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
     
    //Find the index of the lowest number in 'nums'.
    int findLowest(vector<int> nums)
    {
    	 int lowestIndex = 0;	 //Assume nums[0] is lowest to begin with
    	 for(int i = 1; i < nums.size(); ++i)	 //Check for lower numbers
    	 {
    		 if(nums[i] < nums[lowestIndex])
    			 lowestIndex = i;
    	 }
     
    	 return lowestIndex;
    }
     
    //Input 5 values into 'nums'
    void getValues(vector<int>& nums)
    {
    	 for(int i = 0; i < 5; ++i)
    	 {
    		 int input;
    		 cout << "Enter a test score: ";
    		 cin >> input;
    		 if(input < 0 || input > 100)
    		 {
    			 cout << "Must input test score between 0 and 100.\n";
    			 --i;	 //so that this value doesn't count as one of the 5
    		 }
    		 else
    			 nums.push_back(input);	 //Good input, so add it to nums
    	 }
    }
     
    //Calculate the average of the first 4 numbers in 'nums'
    float calcAverage(vector<int> nums)
    {
    	 return ((float)nums[0] + (float)nums[1] + (float)nums[2] + (float)nums[3]) / 4.f;
    }
     
    int main()
    {
    	 vector<int> nums;
     
    	 getValues(nums);
    	 int lowest = findLowest(nums);
    	 nums.erase(nums.begin() + lowest);	 //Erase the lowest score from 'nums'
     
    	 float average = calcAverage(nums);
     
    	 return 0;
    }
    I haven't tested it, but it looks to me like it should calculate your average ok. I hope you understand vectors, if not, then I'll see if I can modify it to use arrays.. and if not arrays, then you'll have to find some way to figure out which of test1/2/3/4/5 is the lowest and then not include that one in the call to calcAverages()... seems like a pain in the butt to me

    **EDIT**
    Actually come to think of it, it doesn't seem like a bad problem to solve using a binary search tree... but it would take a while to write all the insertion/removal/lookup/iterator code for it.
    Last edited by Hunter2; 04-12-2004 at 06:42 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    Hunter2,

    I thank you again for your reply. Someone else suggested that I use "arrays", but unfortunately, I must use "functions" to do this program. I hope someone can help me figure out how to calculate and display the average of the 4 highest test scores and leave the lowest out. I really appreciate your efforts.

    Thanks a bunch,

    Jenny

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Basic program design, passing pointer to a function
    By heras in forum C Programming
    Replies: 14
    Last Post: 04-02-2008, 03:21 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. function
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-02-2002, 07:38 PM