zoukankan      html  css  js  c++  java
  • C lang:Array_Multidimensional arrays

    #include<stdio.h>
    #include<windows.h>
    #define YEARS 5
    #define MONTHS 12
    void color(short x);
    
    int main(void)
    {
    //definition array
        const float rain[YEARS][MONTHS] =
        {
            {1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 0.1, 0.2, 0.3},
            {1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 0.1, 0.2, 0.3},
            {1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 0.1, 0.2, 0.3},
            {1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 0.1, 0.2, 0.3},
            {1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 0.1, 0.2, 0.3}
        };
        const char mon[12][10] = {"January" , "February"  , "March" ,  "April" ,  "May"  , "June"  , "July" ,  "August"  , "September" , "October" ,  "November" , "December"};
        int year, month;
        int i,w;
        float subtot, total, subtotv;
        color(5);
        printf("    Year    Rainfall(inches)
    ");
        color(4);
        for (year = 0, total = 0; year < YEARS; year++)                     // years cyclic out layer
        {
            for (month = 0, subtot = 0; month < MONTHS; month++)            // month cyclic in layer
            {
                subtot = subtot + rain[year][month];                        // per month count rain
            }
            total = total + subtot;                                         // five years count rain
            printf("    %d%11.2f
    ",2016+year,total);
        }
        color(5);
    
        w =0;
        for(i = 0; i < MONTHS; i++)
            printf("%13s",mon[i]);
        printf("
    ");
        color(4);
        for (month = 0; month < MONTHS; month++)                            // month cyclic out layer
        {
            for (year = 0, subtotv = 0; year < YEARS; year++)               // years cyclic in layer
            {
                subtotv = subtotv + rain[year][month];                      // sth month years count
            }
    //            printf("%.1f
    ",subtotv);
                subtotv = subtotv / YEARS;                                  // sth month years average
                printf("%13.2f",subtotv);
    //            printf("%.1f
    ---------
    ",subtotv);
    
        }
    //    printf("%.1f",rain[1][0]);
    //    printf("%.1f
    %.1f
    %.1f
    ",subtot, total, subtotv);
        color(16);
        printf("
    ");
            printf("
    ");    printf("
    ");    printf("
    ");
        system("pause");
        return 0;
    }
    
    void color(short x)
    {
        if(x >= 0 && x <= 15)
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
        else
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
    }
    
    

  • 相关阅读:
    静态嵌套类(Static Nested Class)和内部类(Inner Class)的不同?
    编写多线程程序有几种实现方式?
    文件拷贝
    如何实现对象克隆?
    c#如何保存richtextbox的rtf格式
    C#实现文件与二进制互转并存入数据库
    c#中绝对路径和相对路径
    C#实现MySQL数据库中的blob数据存储
    CSS控制文字,超出显示省略号
    ES6 语句判断简写
  • 原文地址:https://www.cnblogs.com/enomothem/p/11906235.html
Copyright © 2011-2022 走看看