c語言中設置數組元素的個數


c語言中數組元素的個數。

雖然通過對象式宏修改數組元素個數非常的方便,但是每次都需要對程序進行修改,然后重新編譯執行。因此,我們可以定義一個比較大的數組,然后從頭開始僅使用其中需要的部分。

1、

#include <stdio.h>

#define NUMBER 1000

int main(void) { int i, j, num, a[NUMBER], b[11] = {0}; printf("student number: "); do { scanf("%d", &num); if(num < 1 | num > 1000) printf("the range of number is 1-%d.\n", NUMBER); } while(num < 1 | num > 1000); printf("students score: \n"); for(i = 0; i < num; i++) { do { printf("NO.%d = ", i + 1); scanf("%d", &a[i]); if(a[i] < 0 | a[i] > 100) puts("the range of score is 0-100."); } while(a[i] < 0 | a[i] > 100); b[a[i] / 10]++; } printf("---distribution plot---\n"); 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; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM