0

Basically I'm writing a code that allows me to find the minimum number of coffee that a minor drinks. Clearly, in this case, it's "1 coffee", by the 14 year-old. But the output is giving me 0, even though he's 25 (above 18)

#include <stdio.h>
int main()
{
    int tabAges[] = {25, 22, 14, 19, 36, 17};
    int tabCoffee[] = {0, 4, 1, 3, 5, 2}; 
    int nbElements = sizeof(tabAges) / sizeof(int);
    int i, j, minCoffee;

    for(i=0; i<nbElements; i++){
        if (tabAges[i]<18)
            minCoffee = tabCoffee[i];
        for(j=0; j<nbElements; j++)
            if (tabCoffee[j]<minCoffee)
                minCoffee = tabCoffee[j];
    }

    printf("the minimum of coffee drunk by minor is %d", minCoffee);
    return 0;
}
Matt
  • 968
  • 2
  • 13
  • 22

3 Answers3

1

First of all, you must initialize all your variables before using them. That is, minCoffee not only should be an unsigned int it should be then initialized to UINT_MAX. Second, you should only check if the current checked coffee amount is lowered than the minimum. There's no use reiterating the array again. Just check:

unsigned int minCoffee = UINT_MAX;
...
if ((tabAges[i] < 18) && (minCoffee < tabCoffee[i]))
    minCoffee = tabCoffee[i];
Creator
  • 401
  • 2
  • 9
0

Try

#include <stdio.h>
int main()
{
  int tabAges[] = {25, 22, 14, 19, 36, 17};
  int tabCoffee[] = {0, 4, 1, 3, 5, 2}; 
  int nbElements = sizeof(tabAges) / sizeof(int);
  int i, j, minCoffee = INT_MAX;

  for(i = 0; i < nbElements; i += 1) {
    if(tabAges[i] < 18)
      if(tabCoffee[i] < minCoffee)
         minCoffee = tabCoffee[i];
  }
  if(minCoffee == INT_MAX) {
    minCoffee = 0;
  }

  printf("the minimum of coffee drunk by minor is %d", minCoffee);
  return 0;
}

Edited per Matt suggestion.

Dominique Fortin
  • 2,212
  • 15
  • 20
  • I wouldn't recommend using an arbitrary large constant like this. Although it shouldn't make a difference in this scenario, it is still possible for there to be values greater than `999999999`. At least on my system, this is less than half of `INT_MAX`. Additionally, if you were to make the value even higher, you might overflow the variable, potentially leading to unintended behaviour. – Matt Nov 05 '18 at 00:09
  • @Matt God idea. – Dominique Fortin Nov 05 '18 at 00:27
0

As others have said in the comments, there is no need for the second loop. Since you only care about the number of coffees drunk by minors, you only need to compare the number of coffees at these indexes to your minCoffee variable. Doing this, your loop might look something like this:

for(i=0; i<nbElements; i++) {
    if (tabAges[i]<18) {
        if (tabCoffee[i]<minCoffee) {
            minCoffee = tabCoffee[i];
        }
    }
}

(As an aside, while single line control statements do work without curly braces, they can sometimes cause unintended behaviour if you later come back and add a line. It's safer (and arguably more readable) to always include them.

If you prefer, the above if statements can be condensed into a single one: if (tabAges[i] < 18 && tabCoffee[i] < minCoffee.

Now we still need to figure out how to initialise the minCoffee variable, since we have no idea what could be in there.

One option is to import limits.h, and set it to INT_MAX (or UINT_MAX if you decide to use an unsigned integer).

#include <limits.h>
// declare variables etc...
minCoffee = INT_MAX;

If for some reason you can't/don't want to do this, an alternative is to initialise minCoffee to something nonsensical, then have a special case in your loop to set it to the first value for a minor it detects.

minCoffee = -1 //This doesn't make sense, you can't drink less than 0 coffees!
for(i=0; i<nbElements; i++) {
    if (tabAges[i]<18) {
        if (minCoffee == -1) {
            minCoffee = tabCoffee[i]
        } else if (tabCoffee[i]<minCoffee) {
            minCoffee = tabCoffee[i];
        }
    }
}

In practice, this achieves exactly the same result as the previous solution, however it requires an additional test and is not as intuitive to read over, so I would not recommend this method.

Now if you were to put all of this together, you might end up with a solution that looks something like this:

#include <stdio.h>
#include <limits.h>
int main() {
    int tabAges[] = {25, 22, 14, 19, 36, 17};
    int tabCoffee[] = {0, 4, 1, 3, 5, 2}; 
    int nbElements = sizeof(tabAges) / sizeof(int);
    int i, j, minCoffee;

    minCoffee = INT_MAX; 

    for(i=0; i<nbElements; i++){
        if (tabAges[i]<18)
            if (tabCoffee[i]<minCoffee)
                minCoffee = tabCoffee[i];
    }

    printf("the minimum of coffee drunk by minor is %d", minCoffee);
    return 0;
}
Matt
  • 968
  • 2
  • 13
  • 22