-2

The trouble here is I'd like the code to say the number correctly (1st, 2nd, 3rd, 21st, 22nd, 23rd etc), leave alone the problem with 11,12,13 (it can be easily fixed), but why does this simple modulo [ (i+1 % 10) ==1/2/3 ] does work only with 1, 2, and 3, and never after, so it produces "th" from else{} ? It should be straight forward, but if you take any number, for instance location 22 of the array (22+1 % 10) is clearly 3! so it should meet the condition (please note the +1 is due to 0 indexing)

for (int i = 0; i < arrLenght; i++)
{
    if (array[i] == key)
    {
        if ((i+1 % 10) == 1)
        {
            printf("bravo! %i is the %ist number of the array! it's address is %p\n", key, i+1, &array[i]);
        }
        else if ((i+1 % 10) == 2)
        {
            printf("bravo! %i is the %ind number of the array! it's address is %p\n", key, i+1, &array[i]);
        }
        else if ((i+1 % 10) == 3)
        {
            printf("bravo! %i is the %ird number of the array! it's address is %p\n", key, i+1, &array[i]);
        }
        else
        {
            printf("bravo! %i is the %ith number of the array! it's address is %p\n", key, i+1, &array[i]);
        }
        return 1;
    }
}
Duck Ling
  • 1,577
  • 13
  • 20
  • 3
    Operator precedence. Change `if ((i+1 % 10) == 1)` to `if ((i+1) % 10 == 1)`, and so on... – Paul R Jan 12 '19 at 12:33
  • Define "work". Don't *ever* mix modulo and additive arithmetic operations without establishing expressions solidly behind parenthesis. Fyi, [`%` has a higher precedence than `+`](https://en.cppreference.com/w/cpp/language/operator_precedence). So, think about what `(i + 1 % 10)` means vs. `((i + 1) % 10)` – WhozCraig Jan 12 '19 at 12:34
  • That big difference between "what you want" and "what it does"... It requires you to read documentation, or to be safe, use brackets when in doubt. If after that you still have a problem, then come here, but don't dump it here without your own thorough investigation. – Paul Ogilvie Jan 12 '19 at 12:38
  • Note: `a%b` is not the same as "modulo" when `a<0`. [ref](https://stackoverflow.com/q/13683563/2410359) (In OP's code, `a>=0`, so not a great concern.) – chux - Reinstate Monica Jan 12 '19 at 13:25
  • I do agree, that's really bad of me. thanks for your help. i really need to step up in maths! – Duck Ling Jan 12 '19 at 14:44

2 Answers2

1

It's completely related to operator precedence. To recognize it simply, try the following,

printf("%d", 20+1 % 10);   // 21
printf("%d", (20+1) % 10); // 1
1

Besides the error, due to the fact that operator % has a higher precedence (the same as * or /) than + , there's some code duplication that can be avoided:

// Use an array to store the superscripts
const char *sup[] = {
 "th", "st", "nd", "rd"
};

for (int i = 0; i < arrLenght; i++)
{
    if (array[i] == key)
    {
        // Evaluate the index, remembering operator precedence
        int idx = (i + 1) % 10;
        if (idx > 3)
        {
            idx = 0;   // Default to 'th'
        }

        printf("bravo! %i is the %i%s number of the array! it's address is %p\n"
              , key, i + 1
              , sup[idx]        // ^^ print the superscript       
              , (void *)&array[i]);  // the format specifier %p requires a (void *) pointer

        return 1;
    }
}
Bob__
  • 12,361
  • 3
  • 28
  • 42