c语言中数组中元素的个数

1、虽然通过对象式宏来变更元素的数目非常方便,但是每次都需要对程序进行修改,然后重新编译执行。 我们可以定义一个比较大的数组,然后从头开始仅使用其中需要的部分。

#include <stdio.h>
#define NUMBER 80
int main(void)
{
    int i, j;
    int a[NUMBER];
    int b[11] = {0};
    int num;
    puts("please input the num.");
    do
    {
        printf("num = "); scanf("%d", &num);
        if (num < 0 || num > 80)
            puts("the number range is 0--80.");
    }
    while (num < 0 || num > 80);
    puts("please input the elenent.");
    for (i = 0; i < num; i++)
    {
        do
        {
            printf("NO.%d = ", 1 + i); scanf("%d", &a[i]);
            if (a[i] < 0 || a[i] > 100)
                puts("the range is 0--100.");
        }
        while ( a[i] < 0 || a[i] > 100);
        b[a[i]/10]++;
    }
    puts("plot the distribution.");
    printf("------100:");
    for (i = 0; i < b[10]; i++)
        putchar('*');
    putchar('\n');
    for (i = 9; i >= 0; i--)
    {
        printf("%3d--%3d:", i * 10, i * 10 + 9);
        for (j = 0; j < b[i]; j++)
            putchar('*');
        putchar('\n');
    }
    return 0;    
} 
原文地址:https://www.cnblogs.com/liujiaxin2018/p/14565171.html