C语言高级教程之变长数组详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C语言高级教程之变长数组详解

编程爱好者-阿新   2023-03-20 我要评论

一、本文的编译环境

本文的编译环境使用的是集成开发环境:Visual Studio 2019

Visual Studio 2019官网链接如下

Visual Studio 2019官网链接

Visual Studio 2019集成的开发环境的特点有

  • Visual Studio 2019默认安装Live Share代码协作服务。
  • 帮助用户快速编写代码的新欢迎窗口、改进搜索功能、总体性能改进。
  • Visual Studio IntelliCode AI帮助。
  • 更好的Python虚拟和Conda支持。
  • 以及对包括WinForms和WPF在内的.NET Core 3.0项目支持等 。

前面文章的所有数组都在代码中指定了固定的长度。也可以定义其长度在程序运行期间确定的数组。下面是一个示例:

二、一维数组在执行期间确定长度

size_t size = 0;
printf ("Enter the number of elements you want to store: ") ;
scanf ("%zd", &size) ;
float values[size] ;

在这段代码中,把从键盘上读取的一一个值放在size中。接着使用size的值指定数组array的长度。

因为size_t是用实现代码定义的整数类型,所以如果尝试使用%d读取这个值,就会得到一个编译错误。

%zd中的z告诉编译器,它应用于size_t, 所以无论整数类型size_t是什么,编译器都会使说明符适用于读取操作。

三、二维数组在执行期间确定长度

还可以在执行期间确定二维或多维数组中的任意维或所有维。

例如:

size_ t rows = 0;
size t columns = 0;
printf ("Enter the number of rows you want to store: ") ;
scanf ("%zd",&rows) ;
printf ("Enter the number of columns in a row: ") ;
scanf ("%zd",&columns) ;
float beans [ rows] [columns] ;

这里从键盘读取二维数组中的两个维。

这两个数组维都在执行期间确定。

四、一维变长数组实例

一维变长数组实例如下所示

在下面的程序中,一维变长数组是可以用的。

	size_t nGrades = 10;                    // Number of grades
    printf("Enter the number of grades: ");
    scanf("%zd", &nGrades);

    int grades[nGrades];                         // Array storing nGrades values
    long sum = 0L;                               // Sum of the numbers
    float average = 0.0f;                        // Average of the numbers

    printf("\nEnter the %u grades:\n", nGrades); // Prompt for the input

    // Read the ten numbers to be averaged
    for (size_t i = 0; i < nGrades; ++i)
    {
        printf("%2zd> ", i + 1);
        scanf("%d", &grades[i]);                   // Read a grade
        sum += grades[i];                          // Add it to sum
    }

    printf("The grades you entered are:\n");
    for (size_t i = 0; i < nGrades; ++i)
    {
        printf("Grade[%2zd] = %3d  ", i + 1, grades[i]);

        if ((i + 1) % 5 == 0)                         // After 5 values
            printf("\n");                            // Go to a new line                    
    }

    average = (float)sum / nGrades;                // Calculate the average

    printf("\nAverage of the %zd grades entered is: %.2f\n", nGrades, average);

下面是一些输出:

Enter the number of grades: 12
Enter the 12 grades:
1> 56
2> 67
3> 78
4> 67
5> 68
6> 56
7> 88
8> 98
9> 76
10> 75
11> 87
12> 72
The grades you entered are:
Grade[1]=56Grade[2]=67Grade[3]=78Grade[4]=67Grade[5]=68
Grade[6]=56Grade[7]=88Grade[8]=98Grade[9]=76Grade[10]=75
Grade[11] = 87 Grade[12] = 72
Average of the 12 grades entered is: 74.00

本例定义了一个变量nGrades来存储要输入的分数个数,并从键盘上读取数值:

    size_t nGrades = 10;                    // Number of grades
    printf("Enter the number of grades: ");
    scanf("%zd", &nGrades);

再使用读入nGrades的值,来定义包含所需元素个数的grades数组:

int grades[nGrades];                         // Array storing nGrades values

显然,数组的长度值必须在这个语句之前定义。.

五、完整程序

本文的完整程序如下所示

5.1 Main.h 文件程序

#ifndef MAIN_H
#define MAIN_H

#include <stdio.h>
#include <stdlib.h>

#endif

5.2 Main.c 文件程序

#define _CRT_SECURE_NO_WARNINGS

#include "Main.h"

int main()
{
	system("color 3E");

    size_t nGrades = 10;                    // Number of grades
    printf("Enter the number of grades: ");
    scanf("%zd", &nGrades);

    int grades[nGrades];                         // Array storing nGrades values
    long sum = 0L;                               // Sum of the numbers
    float average = 0.0f;                        // Average of the numbers

    printf("\nEnter the %u grades:\n", nGrades); // Prompt for the input

    // Read the ten numbers to be averaged
    for (size_t i = 0; i < nGrades; ++i)
    {
        printf("%2zd> ", i + 1);
        scanf("%d", &grades[i]);                   // Read a grade
        sum += grades[i];                          // Add it to sum
    }

    printf("The grades you entered are:\n");
    for (size_t i = 0; i < nGrades; ++i)
    {
        printf("Grade[%2zd] = %3d  ", i + 1, grades[i]);

        if ((i + 1) % 5 == 0)                         // After 5 values
            printf("\n");                            // Go to a new line                    
    }

    average = (float)sum / nGrades;                // Calculate the average

    printf("\nAverage of the %zd grades entered is: %.2f\n", nGrades, average);

	system("pause");
	return 0;
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们