Use Array to track Counters - C Array

C examples for Array:Array Element

Description

Use Array to track Counters

Demo Code

#include <stdio.h>

int main (void)
{
    int ratingCounters[11], response;

    for (int i = 1; i <= 10; ++i)
        ratingCounters[i] = 0;//from   w  ww  . j a  v a  2s.co m

    printf ("Enter number:");

    for (int i = 1; i <= 20; ++i)
    {
        scanf ("%i", &response);

        if (response < 1 || response > 10)
            printf ("Bad response: %i\n", response);
        else
            ++ratingCounters[response];
    }
    for (int i = 1; i <= 10; ++i)
        printf ("%4i%14i\n", i, ratingCounters[i]);

    return 0;
}

Result


Related Tutorials