user7468879
user7468879

Reputation:

Function to find the minimum number and print it

A very basic (and embarrassing) question which I have not been able to solve yet.

I created a functionminimum which, when called, should display the smallest integer that the user has entered into a file. I am also using atoi.

The working function is as follows:

int minimum(int number) {

static int minNumber = INT_MAX;

if (number < minNumber) {
    minNumber = number;
}
return minNumber;

}

Next, I am calling the function later on in the program when I am reading from my file that the user has entered these integers.

EDIT I am now using the following correct code as suggested below

for (int i = 1; !finished; i++)
{
    if (fscanf(fp, "%s", word) < 1)
        finished = 1;
    else {
        number = atoi(word);
        minNumber = minimum(number);
        printf("\nstring  is \t %s\n", word);
        printf("integer is \t %d\n", number);
    }
}
minimum(number);

Solved - My problem is that number is always returning my last input as being the smallest.

Let's say I also want to display the index for this integer, thus determining the position of the smallest number. Is creating another function the way to go? Or can this be done just by altering the present function?

Upvotes: 1

Views: 393

Answers (2)

David Ranieri
David Ranieri

Reputation: 41017

When you call minimum() the value of minNumber is always INT_MAX, use a static:

void minimum(int number) {
    static int minNumber = INT_MAX;

    if (number <= minNumber) {
        minNumber = number;
    }
    printf("min number is %d\n", minNumber);
}

or pass minNumber to the function:

void minimum(int number, int *minNumber) {
    if (number <= *minNumber) {
        *minNumber = number;
    }
    printf("min number is %d\n", *minNumber);
}

int minNumber = INT_MAX;
for (int i = 1; !finished; i++)
{
     ...
     // As pointed out by @YuriyIvaskevych, you need to compare
     // the number in each iteration of the for loop
     number = atoi(word);
     minimum(number, &minNumber);
     ...
}

Upvotes: 2

Yuriy Ivaskevych
Yuriy Ivaskevych

Reputation: 966

minimum(number); is located outside of the loop, so basically you check only the last input.

But moving it inside for loop still doesn't solve the problem: every time you call minimum() it just compare it to local variable initialized to INT_MAX and obviusly returns the number it gets (because it is < INT_MAX each time.

So first: as already mentioned in comments by @WhozCraig , create minNumber visible to minimum function but the same for every call to it: static int minNumber = INT_MAX;

minimum function now looks like this:

int minimum(int number) {

   static int minNumber = INT_MAX;

   if (number < minNumber) {
       minNumber = number;
   }

   return minNumber;                            // return a minimum for later print it out
}

Note that I removed call to printf because it would print out minNumber on every call.

Let's store somewhere minimal value we've found so far:

int minNumber = 0 before for loop

And now inside the loop:

...
    else {
        number = atoi(word);
        minNumber = minimum(number);             // now we call it for every input
        printf("\nstring  is \t %s\n", word);
        printf("integer is \t %d\n", number);
    }
...

Now simply print it out:

printf("min number is %d\n", minNumber);

Btw, there is another by way passing a pointer as suggested by @KeineLust

Upvotes: 0

Related Questions