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?

Recommended Answers

All 11 Replies

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.

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

Please help me, anyone...

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.

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

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?

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

void findlowest(int &minScoreP, int numarray[])
{
minScoreP=numarray[0]
for(i=1;i<5;i++)
{
if(minScoreP>numarray[i]
 minScoreP=numarray[i]

}
}

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]

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.