I come, I see, I conquer

                    —Gaius Julius Caesar

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1. 使用数组的好处

 要计算某班的平均分数,假设该班只有10个学生。利用前面所学知识,可以有如下两种方法:

方法一:利用一个变量存储分数

#include <stdio.h>

int main(void)
{
    
int score = 0;  //存储分数的变量
    int count = 10//学生人数
    long sum = 0L;  //总分数
    float average = 0.0f//平均分

    
for(int i=0; i<count; i++)
    {
        printf(
"Enter score: ");
        scanf(
"%d"&score); //从键盘读入一个分数
        sum += score//将读入的分数加到总分数里面
    }

    average 
= (float)sum/count; //计算平均分

    printf(
"\nLast score is: %d", score);
    printf(
"\nAverage of the 10 scores entered is: %f\n", average); //输出平均分

    
return 0;
}

 

点评:该方法只保留了最后一个学生的分数,其他学生的分数全部丢失了。因此,如果我们想在此程序的基础上继续求最高分和最低分是行不通的。

下面采用第二种方法。声明10个变量来保存10个学生的分数。

方法二:利用多个变量存储分数

#include <stdio.h>

int main(void)
{
    
//定义10个变量分别存储10个学生的分数
    int score0 = 0, score1 = 0, score2 = 0, score3 = 0, score4 = 0;
    
int score5 = 0, score6 = 0, score7 = 0, score8 = 0, score9 = 0;

    
long sum = 0L;        //总分数
    float average = 0.0f//平均分

    printf(
"Enter the 10 scores:");

    scanf(
"%d%d%d%d%d%d%d%d%d%d"
        
&score0, &score1, &score2, &score3, &score4,
        
&score5, &score6, &score7, &score8, &score9); //输入各个学生的分数

    sum 
= score0 + score1 + score2 + score3 + score4 + 
          score5 
+ score6 + score7 + score8 + score9; //计算总分

    average 
= (float)sum/10.0f//计算平均分

    printf(
"\nAverage of the 10 scores entered is: %f\n", average); //输出平均分

    
return 0;
}

 

点评:该方法克服了方法1中的缺点。但如果班里有50、100或1000个学生,该方法就不切实际了,而应该使用数组。

 

2. 什么是数组

方法2中,声明了10个不同的变量来存放10个分数。共同点是它们都为整型变量。为了解决方法2存在这样的话,我们可以用数组对这10个变量进行统一声明:

int score[10];

以上语句声明了一个数组,数组的名称为score,它包含10个元素,每个元素的类型为整型。

数组是一组数目固定类型相同的元素组成的集合。其中,数目固定指的是元素的个数在程序运行的时候不能更改,类型相同指所有的元素具有相同的数据类型。如:全部为int, long, float, char或其它类型。

怎样访问数组里面的元素呢?我们采用数组名和元素在这个数组中的位置来访问。需要注意的是位置总是从0开始,也称为下标或索引。因此数组中的10个元素分别为:

score[0], score[1], score[2], score[3], score[4], score[5], score[6], score[7], score[8], score[9]

其中,score[0]为数组的第一个元素,score[9]为数组的第十个元素,也即最后一个元素。如果i=2, 那么score[i]与score[2]等价,表示数组的第三个元素。

利用数组计算平均分的程序为:

方法三:利用数组存储分数

#include <stdio.h>

int main(void)
{
    int score[10];  //存储10个整型值的数组
    int count = 10//学生人数
    long sum = 0L;  //总分数
    float average = 0.0f//平均分

    printf(
"Enter the 10 scores: \n");

    
for(int i=0; i<count; i++)
    {
        scanf("%d"&score[i]); //从键盘读入一个分数
          sum += score[i]//将读入的分数加到总分数里面
    }

    average 
= (float)sum/count; //计算平均分
    
    printf(
"The 10 scores are: ");

    
for(i=0; i<count; i++)
        printf(
"%d ", score[i]); //输出各个分数

    printf(
"\nAverage of the 10 scores entered is: %f\n", average); //输出平均分

    
return 0;
}

 

当编译器对以上代码进行编译时,假设计算机指定数组score中的元素从地址为1000的地方开始存放。那么,存放一个元素,计算机需要给该数组多少空间呢?

由于每个元素的数据类型为int,而一般来说,int占用4个字节,所以计算机需要为每一个元素分配4个字节的存储空间。如下图所示:元素score[0]占用了编号为1000, 1001, 1002, 1003共4个字节的内存空间。score[1]占用1004 - 1007号内存空间。其余元素依此类推。

当声明数组 int score[10]; 时,计算机就为数组预留10×4=40字节的存储空间,正式的说法是分配40字节的存储空间,此时空间里面没有存放东西。对于数组中的各元素,它们的存储空间是连续的,没有间断。因此,如果知道了某个元素的存放地址,就能准确获取其它元素的存放地址。

下图是对数组声明的一些解说:

获取元素的地址类似于普通变量,它们的对比如下:

#include <stdio.h>

int main(void)
{
    
int x, y, z;
    
int score[3]; 

    scanf(
"%d %d %d"&x, &y, &z); //输入三个值分别存入变量x, y和z中
    scanf("%d %d %d"&score[0], &score[1], &score[2]); //输入三个值分别存入数组的第1,2,3个元素中

    printf(
"%d %d %d \n", x, y, z); //输出变量的值
    printf("%p %p %p \n"&x, &y, &z); //输出变量的地址
    printf("%d %d %d \n", score[0], score[1], score[2]); //输出数组中元素的值
    printf("%p %p %p \n"&score[0], &score[1], &score[2]); //输出数组中各元素的地址

    
return 0;
}

 

3. 二维、三维数组以及数组元素在内存中的存储方式

以下是一些一维数组的声明:

int        score[10];
char      letter[26];
float      percentage[20];
double   diameter[15];

 

二维数组和三维数组的声明方式如下:

/* 二维数组的声明 */
int         position[10][10];
int         wheretog[10][10];
float      coordina[52][15];
double   floor_go[60][20];

/* 三维数组的声明 */
int        position[10][20][30];
char     position[10][20][30];
double   situaion[10][20][30];

 

我们已经知道一维数组元素在内存中的存放方式,二维,三维在内存中是如何存放的呢?

假设声明如下的二维数组和三维数组:

int     coo[2][3];
char   pos[2][3][4];

 

分析:

当声明二维数组coo时,计算机分配了2×3=6个int大小的内存空间,当声明三维数组pos时,计算机分配了2×3×4=24个char大小的内存空间。以下为数组的内存分配示意图:

 

4. 一维、二维、三维数组的初始化

4.1 已知数组维数的初始化

(1)元素个数与初始值个数恰好相等的情况

例1:

int axis[2= { 12 };
int plane[2][3= { { 123 }, { 456 } };
int cubic[2][3][4= { 
    {
        { 
1,  2,  3,  4 },
        { 
5,  6,  7,  8 },
        { 
9101112 } 
    },

    {
        { 
13141516 },
        { 
17181920 },
        { 
21222324 }
    }
};

 

例2:

int axis[2= { 12 };
int plane[2][3= { 123456 };
int cubic[2][3][4= { 1,  2,  3,  4,  5,  6,  7,  8,  
                       
910111213141516
                      
1718192021222324 };

 

(2)元素个数与初始值个数不相等的情况

例1:

/* 数组的其余位置自动设为零 */
bool axis[4= { truefalse1 };
char plane[2][3= { '0''1''2',  3,  4 };
int  cubic[2][3][4= { 1,  2,  3,  4,  5,  6,  7,  8,  910111213 };

 

调试查看数组在内存中的内容如下(红色的代表数组获得的指定初值,紫色的为系统自动赋的初值):

Name        Value

+ &axis     0x0012ff4c   ""
+ &plane   0x0012ff44   "012"
+ &cubic   0x0012fee4   ""


0012FEDA  CC CC CC CC CC CC CC CC CC CC 01  烫烫烫烫烫.
0012FEE5  00 00 00 02 00 00 00 03 00 00 00  ...........
0012FEF0  04 00 00 00 05 00 00 00 06 00 00  ...........
0012FEFB  00 07 00 00 00 08 00 00 00 09 00  ......... .
0012FF06  00 00 0A 00 00 00 0B 00 00 00 0C  ...........
0012FF11  00 00 00 0D 00 00 00 00 00 00 00  ...........
0012FF1C  00 00 00 00 00 00 00 00 00 00 00  ...........
0012FF27  00 00 00 00 00 00 00 00 00 00 00  ...........
0012FF32  00 00 00 00 00 00 00 00 00 00 00  ...........
0012FF3D  00 00 00 00 00 00 00 30 31 32 03  .......012.
0012FF48  04 00 CC CC 01 00 01 00 00 00 00  ..烫.......

 

4.2 未知数组维数的初始化

根据数组元素在内存中的分配规则可知,数组的某个维的大小可以省略,且这个维只能是数组最左边的一个维,数组在声明的同时还必须初始化。见下例:

//正确的维大小缺省
bool condition[ ] = { truefalse1 }; //相当于condition[3]
char plane_a[ ][3= { '0''1' }; //相当于plane_a[1][3],数组第一个元素为字符'0',其它为0
char plane_b[ ][3= { '0''1''2''3' }; //相当于plane_b[2][3]
int   cubic_a[ ][3][4= { 1,  2,  3,  4,  5,  6,  7,  8,  9101112 }; //相当于cubic_a[1][3][4]
int   cubic_b[ ][3][4= { 1,  2,  3,  4,  5,  6,  7,  8,  910111213 }; //相当于cubic_b[2][3][4]

//错误的维大小缺省
//char plane_a[ ][ ] = { 1, 2, 3 };
//char plane_a[1][ ] = { 1, 2, 3 };
//int  cubic_a[ ][ ][ ] = { 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12 };
//int  cubic_a[ ][ ][4] = { 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12 };
//int  cubic_a[ ][3][ ] = { 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12 };
//int  cubic_a[2][3][ ] = { 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12 };
//int  cubic_a[2][ ][ ] = { 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12 };
//int  cubic_a[2][ ][4] = { 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12 };

 

posted on 2008-09-27 11:57  jcsu  阅读(8850)  评论(0编辑  收藏  举报