How do I print ordinal indicators in a C program? Can't print numbers with 'st', 'nd', 'rd'. (Beginner)

959 Views Asked by At
#include <stdio.h>

main()
{
    int i, num, sum=0; //declaration
    printf("How many numbers do you want to calculate average of?\n");
    scanf("%d", &num); //how many numbers are to be calculated
    printf("Enter %d numbers\n", num);

    int a[num]; //array to store data
    for(i=1;i<=num;i++) //loop to take input 
    {
        if(i==1) //for 1st
            printf("1st value : ");
        else if (i<=2) //2nd
            printf("2nd value : ");
        else if (i<=3) //3rd
            printf("3rd value : ");
        else //else print th ordinal
            printf("%dth value : ", i);
        scanf("%d", &a[i]);
    }

    for(i=1;i<=num;i++)
        sum+=a[i];

    float avg;
    avg=sum/num;
    printf("Average : %f", avg);

    return 0;
}

A program to take out the average of n numbers. Now, this code does what it should, but if the size of the array goes beyond 20, it prints 21th, 22th, 23th and so on, which is wrong. I can't think of how to fix this problem. Any help would be great. I am new to programming, so pardon my ignorance. Output depicting what needs to be fixed

4

There are 4 best solutions below

2
On BEST ANSWER

You can mod by 10 to get the last digit. Then based on that you can use "st", "nd", "rd", or "th". You'll also need special cases for 11, 12, and 13.

    if ((i % 10 == 1) && (i % 100 != 11))
        printf("%dst value : ", i);
    else if ((i % 10 == 2) && (i % 100 != 12))
        printf("%dnd value : ", i);
    else if ((i % 10 == 3) && (i % 100 != 13))
        printf("%drd value : ", i);
    else
        printf("%dth value : ", i);
3
On

It is okay to be a beginner, no need to apologize. You can solve your problem using a combination of a SWITCH statement and the modulus operator (%). The modulus operator takes two numbers (n1 % n2) and returns the remainder when n1 is divided by n2.

You will want to construct an array of ordinals, like this:

char *ordinalList[] = { "st", "nd", "rd", "th" };

This will allow you to simply reference this array to append the correct ordinal to a number. The next step is to create an algorithm to determine which array index should be referenced. To do this, you can make a new function and call it in your "main".

char *determineOrdinal (char **ordinalList, int numValue)
{

    if (3 < numValue && numValue < 21)
        return ordinals[3];

    switch (numValue % 10) {
        case 1 :    return ordinalList[0];
                    break;
        case 2 :    return ordinalList[1];
                    break;
        case 3 :    return ordinalList[2];
                    break;
        default:    return ordinalList[3];
                    break;
}

You can pass a number into this function as the numValue argument. Your "main" function might look something like this:

#include <stdio.h>

int main(void)
{
    char *ordinalList[] = { "st", "nd", "rd", "th" };
    char *currentdOrdinal;
    int i, num, sum=0; //declaration
    printf("How many numbers do you want to calculate average of?\n");
    scanf("%d", &num); //how many numbers are to be calculated
    printf("Enter %d numbers\n", num);

    int a[num]; //array to store data
    for(i=1;i<=num;i++) //loop to take input 
    {
        currentdOrdinal = determineOrdinal (ordinalList, i)
        printf("%d%s value : ", i, currentdOrdinal);
        scanf("%d", &a[i]);
    }

    for(i=1;i<=num;i++)
        sum+=a[i];

    float avg;
    avg=sum/num;
    printf("Average : %f", avg);

    return 0;
}

I think that code should work for you. I hope this helps.

0
On

There isn't a standard function that does that. You can write one, or use mine:

ordinal.c

#include "ordinal.h"
#include <stdio.h>

static const char *const suffixes[4] = { "th", "st", "nd", "rd" };
enum { NUM_SUFFIXES = sizeof(suffixes) / sizeof(suffixes[0]) };

static unsigned suffix_index(unsigned n)
{
    unsigned x;

    x = n % 100;
    if (x == 11 || x == 12 || x == 13)
        x = 0;
    else if ((x = x % 10) > 3)
        x = 0;
    return x;
}

char *fmt_ordinal(char *buffer, size_t buflen, unsigned n)
{
    unsigned x = suffix_index(n);
    int len = snprintf(buffer, buflen, "%u%s", n, suffixes[x]);
    if (len <= 0 || (size_t)len >= buflen)
        return 0;
    return(buffer);
}

ordinal.h

/* returns buffer or 0 on failure (implausible unless buffer too small) */
extern char *fmt_ordinal(char *buffer, size_t buflen, unsigned n);

Some of that is overkill on its own, but the source file also contains scn_ordinal() which scans ordinal numbers with greater or lesser strictness, and the header declares it.

int main(void)
{
    char buffer[15];

    /* Test fmt_ordinal() */
    for (unsigned i = 0; i < 35; i++)
        printf("%2u => %4s\n", i, fmt_ordinal(buffer, sizeof(buffer), i));

    return 0;
}
0
On

I played with this a bit and this was my minimal 'lookup' except, sadly, for the expense of the modulo division. I wasn't fussed about values above 99.

if( i > 20 ) i %= 10; // Change 21-99 to 1-10.
if( i >  3 ) i  =  0; // Every other one ends with "th"
//         0   1   2   3 
suffix = &"th\0st\0nd\0rd"[ i * 3 ];  // Acknowledge 3byte regions.

You can use 'suffix' as a pointer to a normal null terminated string.