日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C語言高級教程之變長數組詳解_C 語言

作者:編程愛好者-阿新 ? 更新時間: 2023-04-07 編程語言

一、本文的編譯環境

本文的編譯環境使用的是集成開發環境: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;
}

原文鏈接:https://blog.csdn.net/m0_47419053/article/details/128869234

欄目分類
最近更新