09.C language array


A first look at the demand, Super Girl draft a total of five participants, each super-woman entered and stored in a variable weight, the average weight of the final calculation of the surplus woman.

Example (book40.c)

/*
 *  程序名:book40.c,此程序用于演示没有数组的情况下的多个变量求和
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  double aa,bb,cc,dd,ee;  // 定义5个变量存放5名超女的体重
  aa=bb=cc=dd=ee=0;

  printf("请输入第1名超女的体重(公斤):");
  scanf("%lf",&aa);
  printf("请输入第2名超女的体重(公斤):");
  scanf("%lf",&bb);
  printf("请输入第3名超女的体重(公斤):");
  scanf("%lf",&cc);
  printf("请输入第4名超女的体重(公斤):");
  scanf("%lf",&dd);
  printf("请输入第5名超女的体重(公斤):");
  scanf("%lf",&ee);

  printf("超女的平均体重是:%lf\n",(aa+bb+cc+dd+ee)/5);
}

If there are 100 Super Girl, this program will be very long annoying lock.

First, the array

An array (Array) is a group of the same data type variable can store a set of data that defines the syntax is:

数据类型 数组名[数组长度];

E.g:

double money[20];

money is an array that contains 20 elements each have a double variable, by array subscripting elements (Element), the subject of the array numbering starts at 0, the first element of the array is money [ 0], the second element is money [1], and so on until the first 20 elements money [19].

Value assigned to each element of type double, you can write:

money[0] = 10.55;
money[1] = 32.54;
money[2] = 2055.65;
……
money[19] = 2055.65;

Define the array when the array length must be an integer, can be constant, or may be variable.

Index data must also be an integer, can be constant, or may be variable.

Use the same type of array elements and the use of the same variable.

scanf("%lf", &money[4]);   // 把一个值读入数组的第5个元素

Type of the array can be any type of data.

int      no[22];    // 可储存22个int类型整数的数组
char    actors[26]; // 可储存26个字符的数组
double big[500];  // 可储存500个double类型整数的数组

Second, the memory size

We already know how to get a single variable methods take up memory size.

int ii;       // 定义一个整型变量
printf("sizeof(ii)=%d\n",sizeof(ii));     // 输出结果:sizeof(ii)=4
printf("sizeof(ii)=%d\n",sizeof(int));    // 输出结果:sizeof(ii)=4

An array is composed of a plurality of variables, memory size of the total space of a plurality of variables and memory space occupied by sizeof (array name) can be obtained size of the entire array of memory occupied, as follows:

int ii[10];    // 定义一个整型数组变量
printf("sizeof(ii)=%d\n",sizeof(ii));     // 输出结果:sizeof(ii)=40

Third, the array initialization

Using memset function to initialize the array, as follows:

int no[10];
memset(no,0,sizeof(no));

The first parameter is the name of the array, the second parameter to fill 0, the third parameter is an array of the total amount of memory space, get with sizeof (variable names).

IV Notes

1) define the array when the array length must be an integer, constants and variables can be used.

int size=100;
int numbers[size];

2) When using an array, the array index must also be an integer, constants and variables can be used.

3) When using an array, the compiler does not check the array index is correct, but in the program must not be subscript out of bounds array subscript out of bounds if the program is running, the equivalent of a memory access other programs, you may causes the program to abort (Core
dump), the consequences are very serious.

Example (book41.c)

/*
 *  程序名:book41.c,此程序演示数组下标越界的后果
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>
#include <string.h>

int main()
{
  int ii=0;
  int sum[5];    // 定义一维数组

  for (ii=0;ii<100;ii++)
  {
    printf("ii=%d\n",ii);
    sum[ii]=ii;
  }

  return 0;
}

running result

Here Insert Picture Description

Segment error is an illegal operation program memory, causing the program to crash.

If the array index out of bounds, is not necessarily cause the program to crash it? Not necessarily, if the book41.c cycle changed to 6, may not segment error.

Fifth, the array in a for loop

In the array after learning the knowledge, the demand at the beginning of this chapter have a better solution, book40.c program must be modified.

Example (book42.c)

/*
 *  程序名:book42.c,此程序演示采用for循环语句和数组结合使用
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>
#include <string.h>

int main()
{
  int    ii=0;           // 用于循环的计数器
  int    total=5;        // 超女的总人数,初始化为5
  double weight[total];  // 定义数组,用于存放超女的体重
  double sum=0;          // 存放超女体重的和

  memset(weight,0,sizeof(weight));    // 初始化数组为0

  // 循环输入超女的体重,并计算体重的总和
  for (ii=0;ii<total;ii++)
  {
    printf("请输入第%d名超女的体重:",ii+1);   // 输入提示信息
    scanf("%lf",&weight[ii]);    // 接受从键盘输入的体重
    sum=sum+weight[ii];          // 计算超女体重的和
  }

  printf("超女的平均体重是:%lf\n",sum/total);  // 输出平均体重
}

running result

Here Insert Picture Description

for loop provides a straightforward way to use the array subscript, for loop processing using an array of data, which is much more convenient than using five separate scanf statement. Whether 5 or 100 Super Girl, the program will not increase again code.

Six, two-dimensional array

Two-dimensional array definition syntax is:

数据类型 数组名[第一维的长度][第二维的长度];

In our real life, two-dimensional array are everywhere, such as Super Girl talent, a total of five groups, each group of 4 people, if you want to specify by Super Girl, the group must also specify the number of the super-woman and her number in the group.

int girl[5][4];
girl[0][0]    girl[0][1]    girl[0][2]    girl[0][3] 
girl[1][0]    girl[1][1]    girl[1][2]    girl[1][3]
girl[2][0]    girl[2][1]    girl[2][2]    girl[2][3]
girl[3][0]    girl[3][1]    girl[3][2]    girl[3][3]
girl[4][0]    girl[4][1]    girl[4][2]    girl[4][3]

May also be viewed as a two-dimensional array coordinate system with the x-axis and y
-axis, in order to determine a point, x and y values must be specified simultaneously in one plane.

Two-dimensional array is initialized with memset, such as:

memset(girl,0,sizeof(girl));

Put a little more difficult to demand that the beginning of this section: Super Girl draft has three groups, each with five players, provide an interface to input weight each super woman, super-woman final calculation of the average weight of each group.

Example (book43.c)

/*
 *  程序名:book43.c,此程序演示采用for循环语句和二维数组结合使用
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>
#include <string.h>

int main()
{
  int    ii=0;          // 用于组别循环的计数器
  int    jj=0;          // 用于超女人数循环的计数器
  int    class=3;       // 小组总数,初始化为3
  int    total=5;       // 每个组超女的总人数,初始化为5
  double weight[class][total];  // 定义二维数组,存放超女的体重
  double sum[class];    // 定义一维数组存放超女体重的和

  memset(weight,0,sizeof(weight));   // 初始化数组为0
  memset(sum,0,sizeof(sum));          // 初始化数组为0

  // 采用两个循环,第一级循环为小组数,第二级循环为超女人数
  for (ii=0;ii<class;ii++)
  {
    for (jj=0;jj<total;jj++)
    {
      printf("请输入第%d组第%d名超女的体重:",ii+1,jj+1);
      scanf("%lf",&weight[ii][jj]);    // 接受从键盘输入的体重
      sum[ii]=sum[ii]+weight[ii][jj];  // 计算小组超女体重的和
    }
  }
}

running result

Here Insert Picture Description

Seven, multidimensional arrays

From the class of two-dimensional array can use to launch a multi-dimensional array, in the actual development, more than a two-dimensional array of scenarios rarely.

For beginners, it is recommended not to complicate the issue, can master one and two dimensional arrays on it, and so, after skill upgrading, you will find multi-dimensional array is actually very easy.

Eight, string

1, the concept of string

String is a null character '\ 0' end of an array of characters, is a special array of characters, it is agreed, is the rule.

Null character '\ 0' can also be written directly to zero.

Here Insert Picture Description

Because the need to end the string with a 0, so the character string is defined in the time, to a multi-byte set aside to store 0.

 char name[21];  // 定义一个最多存放20个字符或10个汉字的字符串

2, the initialization string

Is an array of strings, of course, be a method to initialize an array initialization string.

char strname[21];
memset(strname,0,sizeof(strname));

3, the assignment of a string

If you want to deposit in strword string "hello", strcpy function can be used, the following code may be used:

  strcpy(strword,"hello");
  // 或者用以下代码
  char strword[21];
  memset(strword,0,sizeof(strword));
  strword[0]='h';
  strword[1]='e';
  strword[2]='l';
  strword[3]='l';
  strword[4]='o';
  strword[5]='\0';      // 或者 name[5]=0;

4, the discussion on the end of the string 0

您可能会问,如果字符串不用0结束,会有什么样的结果,我们用代码来演示一下。

示例(book44.c)

/*
 *  程序名:book44.c,此程序用于演示字符串没有用0结束的后果
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>
#include <string.h>

int main()
{
  char name[3];
  memset(name,0,sizeof(name));

  name[0]='a';
  name[1]='b';
  name[2]='c';

  printf("name=%s=\n",name);
}

运行效果

Here Insert Picture Description

可以看到,输出abc之后,有乱码,并且每次执行程序输出的结果不可预知。

您可能还会问,如果字符串以0结束了,但是后面的内容并不是0,怎么办?后面的内容将被丢弃。

5、字符串数组

以0结束的一维字符数组是一个字符串。

char strname[21];       // 可以存放20个字符的字符串
memset(strname,0,sizeof(strname));
strcpy(strname,"我真的可以存十个汉字");

给一维字符数组加上一维,就是字符串数组了。

char strname[10][21];   // 10个字符串,每个字符串可以存放20个字符
memset(strname,0,sizeof(strname));
strcpy(strname[0],"坦已");
strcpy(strname[1],"褒似");
strcpy(strname[2],"西施");
strcpy(strname[3],"王昭君");
strcpy(strname[4],"貂婵");
……
strcpy(strname[9],"陈圆圆");

十、课后作业

1)编写示例程序,从界面上输入一个字符串,计算字符串的长度。如果输入的是"abcdaaf",显示的结果是7。

2)编写示例程序,从界面上输入一个字符串,把字符串的每个字符从左到右一个一个的显示出来,如果输入的是"abcdaaf",显示的结果是:a
b c d a a f。

3)编写示例程序,从界面上输入一个字符串,把字符串的每个字符从右到左一个一个的显示出来,如果输入的是"abcdaaf",显示的结果是:f
a a d c b a。

4)编写示例程序,某班有10个学生,定义一个一维数组,从界面上输入每个学生的成绩,最后显示这个班的平均成绩。

5)编写示例程序,某年级有两个班,每班有8名学生,定义一个二维数组,从界面上输入每个学生的成绩,最后显示每个班的平均成绩和年级的平均成绩。

6)编写示例程序,定义一个可以存放10个字符串的数组,字符串的有效长度是30个字符,从界面上输入10个字符串并存放在数组中,然后把这10个字符串显示出来。

以下题难难度较大,可以等到以后功力提升的时候再做。

7)如果还觉得不过隐,可以挑战一下三维数组,某学校有两个年级,每个年级有三个班,每个班有四名学生,从界面上输入每个学生的成绩,最后显示每个班的平均成绩、年级的平均成绩和全校的平均成绩。

别玩三维以上的数组,没什么意义。

十一、版权声明

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

Published 29 original articles · won praise 2 · Views 687

Guess you like

Origin blog.csdn.net/m0_45133894/article/details/104633213