【C语言】学习笔记4——数组

我直接把控制语句和循环跳过了。大致看了一下,讲得太繁琐了。这部分在后面用C写数据结构就可以练得很熟了。

1. 数组: 由数据类型相同得一系列元素组成。内存上是一片连续得存储单元。

2. 声明

int nums[5]           // 内含5个int类型元素的数组

float candy[365]     // 内含365个float类型元素的数组

char chars[9]         // 内含9个ichar类型元素的数组

3. 初始化

  这个小程序用了sizeof方法

  sizeof days2 得到的是数组的大小

  sizeof days2[0] 得到的是一个元素的大小

#include <stdio.h>

#define MONTHS 12

int main()
{
    
    int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31};
    
    //声明一个只读数组, 一旦初始化,不能对数组里的值进行更改. 如果省略放括号中的数字,编译器会根据初始化列表中的数量来确定数组的大小 
    const int days2[] = {31,28,31,30,31,30,31,31,30,31,30,31};
    
    int index;
    
    printf("遍历 days:\n");
    for(index = 0; index < MONTHS; index++)
    {
        printf("Month %2d has %2d days.\n", index + 1, days[index]);
    }
    
    printf("\n\n遍历 days2:\n");
    for(index = 0; index < sizeof days2 / sizeof days2[0]; index++)
    {
        printf("Month %2d has %2d days.\n", index + 1, days2[index]);
     } 
    return 0; 
}

/*
Output:
遍历 days:
Month  1 has 31 days.
Month  2 has 28 days.
Month  3 has 31 days.
Month  4 has 30 days.
Month  5 has 31 days.
Month  6 has 30 days.
Month  7 has 31 days.
Month  8 has 31 days.
Month  9 has 30 days.
Month 10 has 31 days.
Month 11 has 30 days.
Month 12 has 31 days.


遍历 days2:
Month  1 has 31 days.
Month  2 has 28 days.
Month  3 has 31 days.
Month  4 has 30 days.
Month  5 has 31 days.
Month  6 has 30 days.
Month  7 has 31 days.
Month  8 has 31 days.
Month  9 has 30 days.
Month 10 has 31 days.
Month 11 has 30 days.
Month 12 has 31 days.

*/

 

//一般初始化

int arr[6] = {0, 0, 0, 0, 0, 212};

//C99, 把 arr[5]初始化为212, 把其他初始化为0
int arr[6] = {[5] = 212};

多维数组

#include <stdio.h>
#define MONTHS 12
#define YEARS 5

int main()
{
    const float rain[YEARS][MONTHS] =
    {
        {4.3, 4.3, 4.3, 3.0, 3.0, 2.0, 1.2, 0.2, 0.2, 0.4, 2.4, 3.5, 6.6},
        {8.5, 2.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.3, 2.1},
        {9.1, 8.8, 5.5, 2.6, 4.4, 3.2, 4.3, 5.6, 7.9, 5.4, 6.2, 2.5, 5.6},
        {7.2, 5.3, 6.5, 3.0, 3.0, 2.0, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 1.4},
        {3.0, 3.0, 2.0, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 9.1, 8.8, 5.5},
        {1.2, 1.6, 2.4, 0.0, 5.2, 1.2, 1.6, 2.4, 0.0, 5.2, 6.5, 3.0, 3.0}
    };
    
    int year, month;
    float subtot, total;
    
    printf(" YEAR  RAINFALL (inches)\n");
    for(year = 0, total = 0; year < YEARS; year++)
    {
        for(month = 0, subtot = 0; month < MONTHS; month++)
            subtot += rain[year][month];
        printf("%5d %15.1f\n", 2013 + year, subtot);
        total += subtot;
    }
    printf("\n %d年到%d年%d年的总降水量为%.1f,平均每年降水量为%.1f.\n\n", 2013, 2013 + YEARS - 1, YEARS, total, total / YEARS );
    
    printf("每月平均降水量为\n\n"); 
    printf(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec\n");
    
    for(month = 0; month < MONTHS; month++)
    {
        for(year = 0, subtot = 0; year < YEARS; year++)
            subtot += rain[year][month];
        printf("%4.1f ", subtot / YEARS);
        
    }
    printf("\n");
     
    return 0;
}

/*
Output:
 YEAR  RAINFALL (inches)
 2013            28.8
 2014            31.9
 2015            65.5
 2016            38.3
 2017            37.5

 2013年到2017年5年的总降水量为202.0,平均每年降水量为40.4.

每月平均降水量为

 Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
 6.4  4.7  3.9  2.3  2.9  1.9  2.4  2.7  2.3  1.4  4.9  4.6

*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值