Tips in making my code efficient

Mar 24, 2010 at 8:15pm
My C++ professor assigned a class project and I was put in the group to do a getlowest function of five scores.

This is what I have so far as a code.

int findLowest(int score1 ,int score2 ,int score3 ,int score4 ,int score5)
{
int lowest = 0;
if (score1 < score2 && score1 < score3 && score1 < score4 &&
score1 < score5)
{
lowest = score1;
}
else if (score2 < score3 && score2 < score4 && score2 < score5)
{
lowest = score2;
}
else if (score3 < score4 && score3 < score5)
{
lowest = score3;
}
else if (score4 < score5)
{
lowest = score4;
}
else
{
lowest = score5;
}
return lowest;
}

It's long I know and it works but I was wondering if there was a better way of making it efficient.
Mar 24, 2010 at 8:51pm
You could use sort()

http://www.cplusplus.com/reference/algorithm/sort/

That sure would make it more efficient ;-)
Mar 24, 2010 at 8:51pm
Does the function have to have that prototype? That is, can you pass an array of integers instead?

If so, think about how some pseudocode might go if you have a collection of numbers over which you can iterate:
1. Declare a variable 'lowest' and set it to the first value in the collection
2. Iterate over the rest (i.e. starting at the second value) of the elements in the collection
- for each, check if it's value is less than 'lowest'
- if it is... <you fill this bit in> :)
3. ...

Even if you can't pass an array of integers to the function, it would probably be cleaner if you stored them locally in an array (i.e. inside the function) and followed the logic given above...

Hope that helps...

P.S. The other way that you could do it is simply to SORT the collection, then return the first element (assuming the collection is sorted in descending order). There are lots of efficient sorting algorithms described all over the web. That would be a good exercise for you I think...
Mar 24, 2010 at 8:57pm
This seems kind of nuts but how about using the STL algorithm min:
 
return min( min( min( score1, score2 ), min( score3, score4 ) ), score5 );
Mar 24, 2010 at 9:04pm
Another way is:

1
2
int array[] = { score1, score2, score3, score4, score5 };
return *std::min_element( array, array + 5 );


Yet another way is to use std::partial_sort, but for obtaining only the lowest element,
std::min_element() is more efficient.
Mar 24, 2010 at 9:35pm
It would probably be easier to determine the lowest as input was gathered, IMO.
Mar 24, 2010 at 11:24pm
looping over the array can be fine, same with sorting it. If you are allowed to use STL you may want to use it, as the algorithms are pretty fast, but I am not sure your professor/teacher will allow it. If you want to do something crazy, you could use a set. Though that may be a bit advanced. (point being elements added to a set are sorted without you calling a sort function, just need a comparison function)
Last edited on Mar 24, 2010 at 11:26pm
Mar 25, 2010 at 11:03pm
1
2
3
4
5
if (b < a)  { a = b; }
if (c < a)  { a = c; }
if (d < a)  { a = d; }
if (e < a)  { a = e; }
return a;


i think this is very fast, (n-1) comparisons and no needless work.
in the end its almost like the solution of moorecm, but since you dont need to call functions this should be faster.
Mar 26, 2010 at 12:00am
I think that the best solution depends on how the input is gathered.
Mar 26, 2010 at 3:56am
i assume u need to gather the input to..
so the function and main would be like this..

int lowest(int score[], int size)
{
int lowest=100;
for (int i=0; i<size; i++)
{
if(score[i]<lowest)
lowest = score[i];
}
return lowest;
}


int main()
{
int score[5];

cout << "enter 5 value : ";
for (int i=0; i<5; i++)
cin >> score[i];

cout << "The Lowest is : " << lowest(score,5);
}
Mar 26, 2010 at 12:30pm
As if the last three solutions posted weren't "simple" enough...
Mar 29, 2010 at 12:17pm
Thanks for the help people. I actually went with the array that jsmith posted since that helped me understand a hint that my professor told me when he checked out my long code.
Mar 29, 2010 at 12:59pm
i suggest to read the scores into an array then find the lowest of them by putting them into an loop
as,
min=a[0];
for(i=0;i<5;i++)
if(a[i]<min)
min=a[i];
you got the answer
Mar 29, 2010 at 1:47pm
Wooot! I wanna participate too! Here's my submission:

1
2
int m(int a, int b, int c, int d, int e)
{ return b<a?m(b,c,d,e,a):c<a?m(c,b,d,e,a):a<d?a<e?a:e:m(d,b,c,e,a); }



Oh.. wait. This isn't an obfuscated C contest? Then use std::min_element. ;-)

Ciao, Imi.
PS: Kidding. No offense please.
Topic archived. No new replies allowed.