亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C語言高級(jí)教程之變長(zhǎng)數(shù)組詳解

 更新時(shí)間:2023年02月06日 10:11:01   作者:編程愛好者-阿新  
這篇文章主要介紹了C語言中變長(zhǎng)數(shù)組的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、本文的編譯環(huán)境

本文的編譯環(huán)境使用的是集成開發(fā)環(huán)境:Visual Studio 2019

Visual Studio 2019官網(wǎng)鏈接如下

Visual Studio 2019官網(wǎng)鏈接

Visual Studio 2019集成的開發(fā)環(huán)境的特點(diǎn)有

  • Visual Studio 2019默認(rèn)安裝Live Share代碼協(xié)作服務(wù)。
  • 幫助用戶快速編寫代碼的新歡迎窗口、改進(jìn)搜索功能、總體性能改進(jìn)。
  • Visual Studio IntelliCode AI幫助。
  • 更好的Python虛擬和Conda支持。
  • 以及對(duì)包括WinForms和WPF在內(nèi)的.NET Core 3.0項(xiàng)目支持等 。

前面文章的所有數(shù)組都在代碼中指定了固定的長(zhǎng)度。也可以定義其長(zhǎng)度在程序運(yùn)行期間確定的數(shù)組。下面是一個(gè)示例:

二、一維數(shù)組在執(zhí)行期間確定長(zhǎng)度

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

在這段代碼中,把從鍵盤上讀取的一一個(gè)值放在size中。接著使用size的值指定數(shù)組array的長(zhǎng)度。

因?yàn)閟ize_t是用實(shí)現(xiàn)代碼定義的整數(shù)類型,所以如果嘗試使用%d讀取這個(gè)值,就會(huì)得到一個(gè)編譯錯(cuò)誤。

%zd中的z告訴編譯器,它應(yīng)用于size_t, 所以無論整數(shù)類型size_t是什么,編譯器都會(huì)使說明符適用于讀取操作。

三、二維數(shù)組在執(zhí)行期間確定長(zhǎng)度

還可以在執(zhí)行期間確定二維或多維數(shù)組中的任意維或所有維。

例如:

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] ;

這里從鍵盤讀取二維數(shù)組中的兩個(gè)維。

這兩個(gè)數(shù)組維都在執(zhí)行期間確定。

四、一維變長(zhǎng)數(shù)組實(shí)例

一維變長(zhǎng)數(shù)組實(shí)例如下所示

在下面的程序中,一維變長(zhǎng)數(shù)組是可以用的。

	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

本例定義了一個(gè)變量nGrades來存儲(chǔ)要輸入的分?jǐn)?shù)個(gè)數(shù),并從鍵盤上讀取數(shù)值:

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

再使用讀入nGrades的值,來定義包含所需元素個(gè)數(shù)的grades數(shù)組:

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

顯然,數(shù)組的長(zhǎng)度值必須在這個(gè)語句之前定義。.

五、完整程序

本文的完整程序如下所示

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;
}

以上就是C語言高級(jí)教程之變長(zhǎng)數(shù)組詳解的詳細(xì)內(nèi)容,更多關(guān)于C語言 變長(zhǎng)數(shù)組的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • strcat 函數(shù)的使用指南

    strcat 函數(shù)的使用指南

    strcat是連接字符串的函數(shù)。函數(shù)返回指針,兩個(gè)參數(shù)都是指針,第一個(gè)參數(shù)所指向的內(nèi)存的地址必須能容納兩個(gè)字符串連接后的大小。
    2015-09-09
  • C++的matlab接口轉(zhuǎn)換方法詳解

    C++的matlab接口轉(zhuǎn)換方法詳解

    這篇文章主要為大家詳細(xì)介紹了C++的matlab接口轉(zhuǎn)換方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 詳解C語言中的自定義類型

    詳解C語言中的自定義類型

    這篇文章主要為大家詳細(xì)介紹了C語言中的四大自定義類型(結(jié)構(gòu)體、位段、枚舉和聯(lián)合)的相關(guān)知識(shí),文中的示例代碼簡(jiǎn)潔易懂,需要的可以參考一下
    2023-07-07
  • 約瑟夫環(huán)問題(數(shù)組法)c語言實(shí)現(xiàn)

    約瑟夫環(huán)問題(數(shù)組法)c語言實(shí)現(xiàn)

    這篇文章主要介紹了約瑟夫環(huán)問題(數(shù)組法)c語言實(shí)現(xiàn),有需要的朋友可以參考一下
    2013-12-12
  • 一文搞懂C++多態(tài)的用法

    一文搞懂C++多態(tài)的用法

    C++多態(tài)是在繼承的基礎(chǔ)上實(shí)現(xiàn)的,了解多態(tài)之前我們需要掌握一定的C++繼承的知識(shí),本文將介紹C++中多態(tài)的概念,構(gòu)成條件以及用法,感興趣的可以學(xué)習(xí)一下
    2022-04-04
  • 在matlab中實(shí)現(xiàn)for循環(huán)的方法

    在matlab中實(shí)現(xiàn)for循環(huán)的方法

    for循環(huán)用來循環(huán)處理數(shù)據(jù),break用于終止離它最近的一層for循環(huán),continue用于跳過離它最近的一層for循環(huán),接著執(zhí)行下一次循環(huán),本文重點(diǎn)給大家介紹在matlab中實(shí)現(xiàn)for循環(huán)的方法,感興趣的朋友一起看看吧
    2021-11-11
  • 詳解C++中基類與派生類的轉(zhuǎn)換以及虛基類

    詳解C++中基類與派生類的轉(zhuǎn)換以及虛基類

    這篇文章主要介紹了詳解C++中基類與派生類的轉(zhuǎn)換以及虛基類,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • C++淺析數(shù)據(jù)在內(nèi)存中如何存儲(chǔ)

    C++淺析數(shù)據(jù)在內(nèi)存中如何存儲(chǔ)

    使用編程語言進(jìn)行編程時(shí),需要用到各種變量來存儲(chǔ)各種信息。變量保留的是它所存儲(chǔ)的值的內(nèi)存位置。這意味著,當(dāng)您創(chuàng)建一個(gè)變量時(shí),就會(huì)在內(nèi)存中保留一些空間。您可能需要存儲(chǔ)各種數(shù)據(jù)類型的信息,操作系統(tǒng)會(huì)根據(jù)變量的數(shù)據(jù)類型,來分配內(nèi)存和決定在保留內(nèi)存中存儲(chǔ)什么
    2022-08-08
  • 淺談c++11閉包的實(shí)現(xiàn)

    淺談c++11閉包的實(shí)現(xiàn)

    閉包有很多種定義,一種說法是,閉包是帶有上下文的函數(shù)。說白了,就是有狀態(tài)的函數(shù)。更直接一些,不就是個(gè)類嗎?換了個(gè)名字而已。本文將介紹c++11閉包的實(shí)現(xiàn),感興趣的同學(xué),可以參考下。
    2021-06-06
  • C語言基于回溯算法解決八皇后問題的方法

    C語言基于回溯算法解決八皇后問題的方法

    這篇文章主要介紹了C語言基于回溯算法解決八皇后問題的方法,簡(jiǎn)單描述了八皇后問題,并結(jié)合實(shí)例形式分析了C語言使用回溯算法解決八皇后問題的相關(guān)操作技巧,需要的朋友可以參考下
    2018-06-06

最新評(píng)論