搜索
您的当前位置:首页正文

8. 数组 Arrays

来源:二三娱乐

8.1 一维数组

一维数组:用1个组名,存储相同数据类型的数值列表

数组:
数据类型 数组名称[项目个数]
每个数组,都分配了足够的存储区保存在声明语句中给出的数据项的个数
数组中的每一项叫元素element
名为xxx的数组保留n个什么类型的存储区

#define NUMELS 5

int grades[NUMELS];

element在数组中,被顺序的存储。
通过数组名+元素的位置,能访问任何单独的元素。

grades[3]
//是存储在grades数组中的第三个元素。
//读作grades 3。
//下标不一定非得是整数,还可能是表达式i, i-1, j+1等
#define NUMELS 5

int total = 0;
for (int i=0, i < NUMELS, i++)
    total += grades[i];
#define NUMELS 100

int maximum;
maximum = price[0];
for (int i = 0, i < NUMELS, i++)
    if (price[i] > maximum)
        maximum = price[i]
数组(名称)保留了(项目个数)个(数据类型(字符/双精度数/整数))的存储区

输入输出数组

scant("%d %f", &grades[2], &price[3]);
#define NUMELS 5

for (int i = 0, i < NUMELS, i++)
{
    printf("Enter grade %d: ", i);
    scanf("%d", &grades[i]);
}
#define MAXGRADES 5

#include <stdio.h>

int main()
{
    int grades[MAXGRADES];
    int i,total = 0;

    for (i = 0; i < MAXGRADES; i++) {
        printf("Enter grade %d: ", i);
        scanf("%d", &grades[i]);
    }
    
    for (i = 0; i < MAXGRADES; i++) {
        printf("grades %d is %d\n", i, grades[i]);
    }
    
    for (i = 0; i < MAXGRADES; i++) {
        total += grades[i];
    }
    
    printf("The total is %d\n", total);
    
    return 0;
}

8.2 数组初始化

#define SIZE 7
#define NUMGALS 20
double width[SIZE] = {10.96, 6.43, 2.58, .86, 5.89, 7.56, 8.22 };
int gallons[NUMGALS] = {19, 16,
                        12, 10,
                        5 };
//初始化数值可以越过多行进行扩展

寻找数组中的最大值

#define MAXELS 5

#include <stdio.h>

int main()
{
    int num[MAXELS] = {2, 18, 1, 27, 16};
    int i, a, max;
    
    a = 0;
    
    max = num[0];
    
    for (i = 1; i < MAXELS; i++) {
        if (max < num[i]) {
            max = num[i];
            a = i;
        }
    }
    
    printf("The maximum value is %d %d\n", a, max);
    
    return 0;
}

8.3 数组作为函数参数

找数组中的最大值,使用到了自建函数

#include <stdio.h>
int findMax(int [], int); /* function prototype */

int main()
{
  #define MAXELS 5
  int nums[MAXELS] = {2, 18, 1, 27, 16};

  printf("The maximum value is %d\n", findMax(nums, MAXELS));

  return 0;
}

int findMax(int vals[], int numels)
{
  int i, max = vals[0];

  for (i = 1; i < numels; i++)
    if (max < vals[i])
      max = vals[i];

  return (max);
}

8.4 计算 平均值&标准偏差

#include <stdio.h>
#include <math.h>

double findAvg(int [], int);         /* function prototype */
double stdDev(int [], int, double);  /* function prototype */

int main()
{
  #define NUMELS 10
  int values[NUMELS] = {98, 82, 67, 54, 78, 83, 95, 76, 68, 63};
  double average, stddev;
 
  average = findAvg(values, NUMELS); /* call the function */
  stddev = stdDev(values, NUMELS, average); /* call the function */

  printf("The average of the numbers is %5.2f\n", average);
  printf("The standard deviation of the numbers is %5.2f\n", stddev);

  return 0;
}

double findAvg(int nums[], int numel)
{
  int i;
  double sumnums = 0.0;

  for (i = 0; i < numel; i++)  /* calculate the sum of the grades */
  sumnums = sumnums + nums[i];

  return (sumnums / numel);    /* calculate and return the average */
}

double stdDev(int nums[], int numel, double av)
{
  int i;
  double sumdevs = 0.0;

  for (i = 0; i < numel; i++)
    sumdevs = sumdevs + pow((nums[i] - av),2);

  return(sqrt(sumdevs/numel));
}

8.5 二维、多维数组

二维数组,3行4列

#include <stdio.h>
int main()
{
  #define NUMROWS 3//3行
  #define NUMCOLS 4//4列
  int val[NUMROWS][NUMCOLS] = {8,16,9,52,3,15,27,6,14,25,2,10};
  int i, j;

  printf("\nDisplay of val array by explicit element");
  printf("\n%2d %2d %2d %2d",
         val[0][0],val[0][1],val[0][2],val[0][3]);
  printf("\n%2d %2d %2d %2d",
         val[1][0],val[1][1],val[1][2],val[1][3]);
  printf("\n%2d %2d %2d %2d",
         val[2][0],val[2][1],val[2][2],val[2][3]);

  printf("\n\nDisplay of val array using a nested for loop");
  for (i = 0; i < NUMROWS; i++)
  {
    printf("\n"); /* start a new line for each row */
    for (j = 0; j < NUMCOLS; j++)
      printf("%2d ", val[i][j]);
  }
  printf("\n");

  return 0;
}

二维数组每个元素x10

#include <stdio.h>
int main()
{
  #define NUMROWS 3
  #define NUMCOLS 4
  int val[NUMROWS][NUMCOLS] = {8,16,9,52,3,15,27,6,14,25,2,10};
  int i, j;

  /* multiply each element by 10 and display it */
  printf("\nDisplay of multiplied elements\n");
  for (i = 0; i < NUMROWS; i++)
  {
    printf("\n"); /* start a new line */
    for (j = 0; j < NUMCOLS; ++j)
    {
      val[i][j] = val[i][j] * 10;
      printf("%3d ", val[i][j]);
    } /* end of inner loop */
  } /* end of outer loop */
  printf("\n");

  return 0;
}

利用自建函数

#include <stdio.h>
#define ROWS 3
#define COLS 4

void display(int [ROWS][COLS]); /* function prototype */

int main()
{
  int val[ROWS][COLS] = {8,16,9,52,
                         3,15,27,6,
                         14,25,2,10};

  display(val);

  return 0;
}

void display(int nums[ROWS][COLS])
{
  int rowNum, colNum;

  for (rowNum = 0; rowNum < ROWS; rowNum++)
  {
    for(colNum = 0; colNum < COLS; colNum++)
      printf("%4d",nums[rowNum][colNum]);
    printf("\n");
  }
}

验证元素地址

#include <stdio.h>
#define NUMELS 20
int main()
{
  int numbers[NUMELS];

  printf("The starting address of the numbers array is: %d\n",      

                                                 &(numbers[0]));
  printf("The storage size of each array element is: %d\n", 
                                                   sizeof(int));
  printf("The address of element numbers[5] is : %d\n", &(numbers[5]));

  printf("The starting address of the array,\n");
  printf("  using the notation numbers, is: %d\n", numbers);

  return 0;
}

语法

数组声明:
数据类型 数组名称[项目个数]

#define NUMELS 5//在声明数组之前,定义数组项目个数为一个符号常量
int grades[NUMELS];//一维
#define ROWS 3
#define COLS 4
int val[ROWS][COLS];//二维
Top