← Back to Index
Research & Engineering Archive

LPC 7. Arrays

By Jingnan Huang · February 22, 2025 · 2196 Words

Last Edit: 2/22/25

很多情况下,需要连续处理多个值,而分别给他们赋值则显得特别麻烦,于是就需要一种更加高效的数据结构

7.1 Why and how to use arrays ?
#

Img

grades[0] = 100;
grades[1] = 95;
grades[2] = 67;
grades[3] = 99;
grades[4] = 72;
grades[5] = 101;
grades[6] = 200;
#define SIZE 7

int main(void) {
  int arr[SIZE];  // 等价于 int arr[7];
  int x = SIZE;   // 等价于 int x = 7;
  return 0;
}
int main(void) {
  const int Size = 7;
  int arr[Size];  // 在某些编译器中可能不合法
  int x = Size;
  return 0;
}

ex. Find Avg of an array
#

#include <stdio.h>
#define SIZE 7 
int main(void){
  int grades[SIZE] = {100, 95, 67, 99, 72, 101, 200};
  int sum = 0;
  double avg = 0;
  for (int index = 0; index < SIZE; index++){
    sum = sum + grades[index];
  }
  avg = (double) sum / SIZE;
  printf("Average is %.2lf", avg);
  return 0;
}

Array Index Range Error
#

7.1.1 ex. Reverse the Elements in an Array
#

Img

#include <stdio.h>
#define SIZE 6
int main(void){
  int arr[SIZE] = {2, 5, 7, 8, 9, 12};
  for (int index = 0; index < SIZE; index++){
      printf("%d, ", arr[index]);
  } 
  printf("\n");
  for(int low = 0, high = SIZE - 1; low < high; low++, high--){
    int temp = arr[low];
    arr[low] = arr[high];
    arr[high] = temp; 
  }
  for (int index = 0; index < SIZE; index++){
    printf("%d, ", arr[index]);
  } 
  printf("\n");
  return 0;
}

7.1.2 Summary of Important Features of Arrays
#

7.2 What are arrays, and how are they stored ?
#

Img

x[i] == *(x + i) == *( &x[i] )

7.2.1 Pointer Arithmetic
#

简单来说,Pointer 和正常的 Variable 不一样,它有着自己的加减乘除方法

#include <stdio.h>
int main(void) {
    const int size = 3;
    int x[size] = {1, 7, 3};  // 定义数组 x
    int *q = &x[2];           // 指针 q 指向 x[2]
    int dist = q - x;         // 计算指针之间的偏移量
    printf("Dist is %d\n", dist); // 输出偏移量
    return 0;
}

7.3 How do we pass an Array to a function ?
#

#include <stdio.h>
double f(int []); // 声明函数,接受一个整数数组

int main(void){
    int x[3] = {1, 7, 3};  // 定义一个数组
    double result = f(x);  // 传递数组 x 给函数 f
    return 0;
}
double f(int list[]){  // 这里 list[] 实际上是指针
    // statements;
}

7.3.1 Size of array in a function is unknown
#

#include <stdio.h>
int sumData(int[], const int); // 函数声明
int main(void){
    int x[3] = {1, 7, 3};  // 定义数组
    int result = sumData(x, 3);  // 传递数组和大小
    printf("Sum of elements in the array: %d.\n", result);
    return 0;
}
int sumData(int list[], const int size) {  
    int sum = 0;
    for (int index = 0; index < size; index++) {  
        sum = sum + list[index];  // 累加数组元素
    }
    return sum;
}

7.3.2 Can I use the pointer syntax too ?
#

#include <stdio.h>
int sumData(int*, int); // 使用指针表示数组参数
int main(void) {
    int x[3] = {1, 7, 3};  
    int result = sumData(x, 3);  // 传递数组 x
    printf("Sum of elements in the array: %d.\n", result);
    return 0;
}
int sumData(int* list, int size) {  
    int sum = 0;
    for (int index = 0; index < size; index++) {  
        sum = sum + *(list + index);  // 使用指针偏移代替数组索引
    }
    return sum;
}

7.3.3 Are we passing the array by value or by pointers ?
#

#include <stdio.h>
void swap(int[], int, int);  // 交换数组中两个元素
void printArray(int[], const int); // 打印数组元素

int main(void) {
    int x[5] = {3, 5, 8, 1, 7};  
    printf("Before swapping: ");
    printArray(x, 5);
    
    swap(x, 0, 4); // 交换 x[0] 和 x[4]

    printf("After swapping: ");
    printArray(x, 5);

    return 0;
}

// 交换 list[i] 和 list[j]
void swap(int list[], int i, int j) {
    int temp = list[i];
    list[i] = list[j];
    list[j] = temp;
}

// 遍历并打印数组
void printArray(int list[], const int size) {
    for (int index = 0; index < size; index++) {
        printf("%d ", list[index]);
    }
    printf("\n");
}