網(wǎng)站首頁 編程語言 正文
一、本文的編譯環(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ù)組都在代碼中指定了固定的長度。也可以定義其長度在程序運(yùn)行期間確定的數(shù)組。下面是一個(gè)示例:
二、一維數(shù)組在執(zhí)行期間確定長度
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的長度。
因?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í)行期間確定二維或多維數(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í)行期間確定。
四、一維變長數(shù)組實(shí)例
一維變長數(shù)組實(shí)例如下所示
在下面的程序中,一維變長數(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ù)組的長度值必須在這個(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;
}
原文鏈接:https://blog.csdn.net/m0_47419053/article/details/128869234
相關(guān)推薦
- 2022-05-10 FactoryBean配置文件定義的 類型 調(diào)用時(shí)返回 不同的類型
- 2022-09-13 go開源項(xiàng)目用戶名密碼驗(yàn)證的邏輯鬼才寫法_Golang
- 2023-01-27 Python?Flask利用SocketIO庫實(shí)現(xiàn)圖表的繪制_python
- 2023-10-15 el-input有時(shí)候添加不了有時(shí)候刪不了
- 2024-03-25 使用HttpURLConnection發(fā)送POST請(qǐng)求并攜帶請(qǐng)求參數(shù)
- 2022-08-16 C/C++函數(shù)的調(diào)用約定的使用_C 語言
- 2023-04-11 Go使用協(xié)程批量獲取數(shù)據(jù)加快接口返回速度_Golang
- 2023-10-11 lambda Collectors類的靜態(tài)工廠方法
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支