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

  1. #16
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, that IS using functions. Arrays and functions don't contradict each other you know Arrays are a *datatype* (well, several of a datatype is more accurate, but whatever...), while functions are just a completely separate idea. Using functions just means using "void getValues(*something*) { *something* }". Well, if you don't want to use arrays then you could exclude the variable you don't want, by using a whole bunch of if/else:
    Code:
    if(test1 == lowest)
         calcAverage(test5, test2, test3, test4);
    else if(test2 == lowest)
         calcAverage(test1, test5, test3, test4);
    else if(test3 == lowest)
         calcAverage(test1, test2, test5, test4);
    else if(test4 == lowest)
         calcAverage(test1, test2, test3, test5);
    else
         calcAverage(test1, test2, test3, test4);
    That should work, though it's not as neat as other solutions could be.
    Just Google It. √

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

  2. #17
    Registered User
    Join Date
    Apr 2004
    Posts
    7

    Question

    Quote Originally Posted by Hunter2
    Well, that IS using functions. Arrays and functions don't contradict each other you know Arrays are a *datatype* (well, several of a datatype is more accurate, but whatever...), while functions are just a completely separate idea. Using functions just means using "void getValues(*something*) { *something* }". Well, if you don't want to use arrays then you could exclude the variable you don't want, by using a whole bunch of if/else:
    Code:
    if(test1 == lowest)
         calcAverage(test5, test2, test3, test4);
    else if(test2 == lowest)
         calcAverage(test1, test5, test3, test4);
    else if(test3 == lowest)
         calcAverage(test1, test2, test5, test4);
    else if(test4 == lowest)
         calcAverage(test1, test2, test3, test5);
    else
         calcAverage(test1, test2, test3, test4);
    That should work, though it's not as neat as other solutions could be.
    Thanks Hunter2. However, where do I place this code? Here???:
    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);
    	if(test1 == lowestTest)
    		calcAverage(test5, test2, test3, test4);
    	else if(test2 == lowest)
    		calcAverage(test1, test5, test3, test4);
    	else if(test3 == lowest)
    		calcAverage(test1, test2, test5, test4);
    	else if(test4 == lowest)
    		calcAverage(test1, test2, test3, test5);
    	else
    		calcAverage(test1, test2, test3, test4);
    	
    	float average = calcAverage();
    	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;
    }
    
    }

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, first of all, in calcAverage(), look at the parameters. You can't leave them all as "?" forever Just stick in names for the parameters - call them a, b, c, d or something. The names of the parameters have absolutely no relation to the names of the variables that you're passing to the functions.
    i.e.
    Code:
    int a = 0, b = 1, c = 2, d = 3;
     
    void doSomething(int a, int fdsa, int asdf, int bbbb)
    {
    //do something
    }
     
    int main()
    {
    doSomething(a, b, c, d); //Perfectly fine
    return 0;
    }
    Next, look at this:
    Code:
    if(test1 == lowestTest)
    	calcAverage(test5, test2, test3, test4);
    else if(test2 == lowest)
    	calcAverage(test1, test5, test3, test4);
    else if(test3 == lowest)
    	calcAverage(test1, test2, test5, test4);
    else if(test4 == lowest)
    	calcAverage(test1, test2, test3, test5);
    else
    	calcAverage(test1, test2, test3, test4);
     
    float average = calcAverage();	 //calcAverage() takes 4 parameters...
    The point of all the if's and else's here, is that it will call calcAverage() with the proper parameters, based on which variable matches the 'lowest score' returned by findLowest(). So that line at the bottom, 'float average = calcAverage();' isn't needed. But what I forgot in my example, is that you're not storing what calcAverage() is returning So declare 'float average;' before the whole if/else if block, and stick (whatever calcAverage() returns) into average wherever you call calcAverage().

    I hope you understand all that, and the reason behind it. My explaining skills are sort of going down the drain lately Good luck!

    P.S.
    Do you not have a compiler or something? Because a lot of these 'is this right' questions could be answered if you compile the code yourself, and see if any errors pop up. It's a good idea to get the hang of finding out what errors mean; it really helps when you're trying to debug a program on your own.
    Just Google It. √

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

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