Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Finding the lowest number in C (http://www.go4expert.com/forums/finding-lowest-c-t1522/)

 The Foolish One 3Oct2006 12:35

Finding the lowest number in C

Sorry if I do/say something stupid this is my first post here.

I have a function that is passed 6 variables and I want to write some code to figure out which one has the lowest value. This is what I have so far:
Code:

```int LeastInStore(int pup, int kitn, int bird, int snk, int fish, int bun) {         int lowest = 0;         int i;         int quantityOfPets[7];         char petType[20];         int lowestpet =0;         for(i=0; i<7; i++) quantityOfPets[i]=NULL; //initializing the array to NULL                 quantityOfPets[1] = pup;                 quantityOfPets[2] = kitn;                 quantityOfPets[3] = bird;                 quantityOfPets[4] = snk;                 quantityOfPets[5] = fish;                 quantityOfPets[6] = bun;                 for(i = 1; i<6; i++){                         if(quantityOfPets[i] < quantityOfPets[i+1])                                 if(lowest > quantityOfPets[i]){                                   lowest = quantityOfPets[i];                                 }                 }                         return lowest; }```
I know there is something fundamentally wrong with this (or I missed something that is throwing everything off) because I get really odd returns from trying to run this. Any ideas on what I am doing wrong? Or better yet how I can do it right?

 shabbir 3Oct2006 13:24

Re: Finding the lowest number in C

You have posted the query as an article in the Article / Source code section. I have moved it to the Queries and discussion forum.

No you have not messed the stuff its just one logical error.

lowest should be initialized to one of the element rather than 0
Code:

`lowest = quantityOfPets[1];`
Also loop till all the elements and not till <6
Code:

`for(i = 1; i<7; i++)`

 The Foolish One 3Oct2006 22:12

Re: Finding the lowest number in C

shabbir: Thanks for moving it, I don't know why/how I posted it there :)

And thanks for the help, looping through 6 of the 7 items was a holdover from before I initialized the Array, I wanted to "make sure" that I didn't get some wacked out value in there. It's overall effect was completely unnoticed by me because of the second thing you corrected.

Changing that out of place 0 to one of the values that I am actually testing for worked like a charm. Not quite sure why I put 0 in there in the first place. Anyways thanks for the help, everything (well at least this function) is working now.

 shabbir 4Oct2006 09:58

Re: Finding the lowest number in C

My pleasure.

 All times are GMT +5.5. The time now is 10:43.