954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need to simplify a function...

I have created a function to determine the lowest out of 5 scores,
I'm not too sure as to whether or not it can be simplified...

the function is as follows:

void findLowest (int & minScoreP, int score1P, int score2P, int score3P, 
                                                int score4P, int score5P)
{
    minScoreP = score1P;
    
    if (score2P < minScoreP)
        minScoreP = score2P;
   
    if (score3P < minScoreP)
        minScoreP = score3P;
    
    if (score4P < minScoreP)
        minScoreP = score4P;
        
    if (score5P < minScoreP)
        minScoreP = score5P;
}


can anyone please help me out?

BrandonB
Light Poster
38 posts since Mar 2010
Reputation Points: 11
Solved Threads: 0
 

By Simplify, if you mean, to write function in possibly less number of lines, then you can use array instead of 5 different variables and and use loop to compare it.

Other than that, (Correct me if I am wrong) you havent returned or used that final answer i.e. minimal number.

abhi.navale
Light Poster
38 posts since Apr 2009
Reputation Points: 23
Solved Threads: 6
 

By Simplify, if you mean, to write function in possibly less number of lines, then you can use array instead of 5 different variables and and use loop to compare it.

Other than that, (Correct me if I am wrong) you havent returned are used that final answer i.e. minimal number.

The minScoreP doesn't need to be returned (void functiuons don't need the return; statement, & also minScoreP is a reference parameter, i.e. it modifies the equivalent variable in the main function so it doesn't need to be returned)

I just want to know if I can produce the same result with fewer lines, I havent been exposed to arrays as of yet so I just want to simplify what I have already...

But thank-you for your help...

BrandonB
Light Poster
38 posts since Mar 2010
Reputation Points: 11
Solved Threads: 0
 

Please help me, anyone...

BrandonB
Light Poster
38 posts since Mar 2010
Reputation Points: 11
Solved Threads: 0
 

You could perhaps use the ternary operator?

int foo(int a, int b, int c, int d, int e)
{
  int min = a;
  
  min = min > b ? b : min;
  min = min > c ? c : min;
  min = min > d ? d : min;
  min = min > e ? e : min;
  
  return min;
}


But that probably won't do much (since the approach is basically the same as yours), a better approach would make use of an array, in which all numbers are contained, then you could easily loop through all the elements in the array, checking each time if the current element is lower than the lowest element found so far.

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

you dont know anything about array?
if you want i can give you that function with array. but for explanation you take help of google or come on skype

abhi.navale
Light Poster
38 posts since Apr 2009
Reputation Points: 23
Solved Threads: 6
 

I haven't been exposed to arrays as of yet, this is a function for an assignment that I'm busy with & the program is complete but now it's just an issue of simplifying, so is there any way in which that function can be simplified while using loops or if statements or anything basic?

BrandonB
Light Poster
38 posts since Mar 2010
Reputation Points: 11
Solved Threads: 0
 

It's for an assignment so due to the fact that we haven't yet covered arrays I would assume that we can't make use of them in the answers...

BrandonB
Light Poster
38 posts since Mar 2010
Reputation Points: 11
Solved Threads: 0
 
void findlowest(int &minScoreP, int numarray[])
{
minScoreP=numarray[0]
for(i=1;i<5;i++)
{
if(minScoreP>numarray[i]
 minScoreP=numarray[i]

}
}
abhi.navale
Light Poster
38 posts since Apr 2009
Reputation Points: 23
Solved Threads: 6
 
It's for an assignment so due to the fact that we haven't yet covered arrays I would assume that we can't make use of them in the answers...

That's the most probable, you could however ask your instructor.
Anyways, the code you have so far is probably the simplest you can get, taking your current C++ knowledge into account.

[EDIT]
Since finding the minimum/maximum is a common problem, you might want to do a Google/forum search on this topic.
[/EDIT]

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
It's for an assignment so due to the fact that we haven't yet covered arrays I would assume that we can't make use of them in the answers...

then you can use your current program. its correct.

abhi.navale
Light Poster
38 posts since Apr 2009
Reputation Points: 23
Solved Threads: 6
 

Ok, thank-you very much for your help...

BrandonB
Light Poster
38 posts since Mar 2010
Reputation Points: 11
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You