介绍
在这个实验中,你将学习如何编写一个 C 程序,用于找出用户输入的一组数字中的最大值。这是一个基础的编程练习,它将教会你几个重要的概念:
- 获取用户输入
- 使用循环
- 进行比较
- 在迭代过程中跟踪最大值
我们要实现的算法很简单:首先询问用户想要输入多少个数字,然后依次处理每个输入的数字。在处理每个数字时,将其与目前找到的最大值进行比较,必要时更新“最大值”变量。
完成这个实验后,你将创建一个可以处理任意数量输入,并能可靠地找出其中最大值的程序。
创建我们的程序文件
让我们先为程序创建一个新的 C 文件。在 WebIDE 中,我们将在项目目录下创建一个名为 main.c 的文件。
在终端面板中导航到项目目录:
cd ~/project现在,在 WebIDE 中,点击左侧资源管理器面板中的“新建文件”按钮,或者在资源管理器面板中右键单击并选择“新建文件”。
将文件命名为
main.c并按回车键。让我们开始在新创建的文件中添加 C 程序的基本结构:
#include <stdio.h> int main() { // We will add our code here return 0; }按 Ctrl+S 或从菜单中选择“文件” > “保存”来保存文件。
这个结构包括:
#include <stdio.h>指令,它引入了标准输入/输出库,我们在使用printf()和scanf()等函数时需要用到这个库main()函数,它是任何 C 程序的入口点return 0;语句,它表示程序成功执行
stdio.h 头文件提供了用于输入和输出操作的函数。main() 函数是程序执行的起点,return 0; 向操作系统表明程序已无错误地终止。
处理用户输入
接下来,我们需要让程序与用户进行交互。你需要:
- 声明变量来存储数据
- 提示用户输入元素的数量
- 准备好接收第一个数字的流程
让我们更新 main.c 中的代码:
#include <stdio.h>
int main() {
// Declare variables
int n; // To store the number of elements
float big; // To store the largest number found
// Prompt the user for the number of elements
printf("Enter the number of elements you wish to find the greatest element of: ");
scanf("%d", &n);
// Check if the input is valid
if (n <= 0) {
printf("Please enter a positive number of elements.\n");
return 1; // Exit with error code
}
// Prompt for the first number and initialize 'big' with it
printf("Enter %d numbers:\n", n);
printf("Enter element 1: ");
scanf("%f", &big);
return 0;
}
让我们来理解一下新增的代码:
变量声明:
int n:一个整数变量,用于存储用户想要输入的数字数量float big:一个浮点型变量,用于存储找到的最大数字
获取元素数量的用户输入:
- 使用
printf()显示提示信息,询问元素的数量 - 使用
scanf("%d", &n)从用户那里读取一个整数,并将其存储在n中 n前面的&是“取地址”运算符,它告诉scanf()将输入值存储在哪里
- 使用
输入验证:
- 检查用户是否输入了一个正数
- 如果不是,显示错误信息并以返回码 1(表示错误)退出程序
第一个数字的输入:
- 提示用户输入第一个数字
- 将这个第一个数字存储在
big中,因为此时它是我们唯一(也是最大)的数字
当你运行这段代码时,它会询问元素的数量,然后询问第一个元素,但目前还不会对这些信息做任何处理。下一步,我们将添加处理所有数字并找出最大值的逻辑。
找出最大的数字
现在,我们将实现程序的核心逻辑——找出输入数字中的最大值。我们将使用 for 循环来完成以下操作:
- 遍历剩余的数字(从第 2 个到第 n 个)
- 将每个数字与当前的最大值进行比较
- 如果找到更大的数字,则更新最大值
用以下代码更新你的 main.c 文件:
#include <stdio.h>
int main() {
// Declare variables
int n; // To store the number of elements
float big; // To store the largest number found
// Prompt the user for the number of elements
printf("Enter the number of elements you wish to find the greatest element of: ");
scanf("%d", &n);
// Check if the input is valid
if (n <= 0) {
printf("Please enter a positive number of elements.\n");
return 1; // Exit with error code
}
// Prompt for the first number and initialize 'big' with it
printf("Enter %d numbers:\n", n);
printf("Enter element 1: ");
scanf("%f", &big);
// Process remaining numbers using a loop
for (int i = 2; i <= n; i++) {
float current; // Variable to store the current number
// Prompt for the current number
printf("Enter element %d: ", i);
scanf("%f", ¤t);
// Compare with the current largest
if (current > big) {
big = current; // Update 'big' if current number is larger
}
}
// Display the result
printf("The largest of the %d numbers is %.2f\n", n, big);
return 0;
}
让我们来理解新增的代码:
For 循环:
- 我们从
i = 2开始,因为第一个元素已经处理过了 - 持续循环直到处理完
n个元素 - 每次迭代,
i增加 1
- 我们从
处理每个数字:
- 声明一个新变量
current来存储每个输入的数字 - 提示用户输入当前元素
- 使用
scanf()读取输入
- 声明一个新变量
找出最大值:
- 将当前输入
current与当前最大值big进行比较 - 如果
current更大,则更新big以存储这个新的最大值 - 如果不是,则保持
big不变,继续处理下一个输入
- 将当前输入
显示结果:
- 处理完所有输入后,显示找到的最大值
%.2f格式说明符将浮点数显示为保留 2 位小数
这个实现遵循了在序列中查找最大值的常见模式:
- 用第一个值初始化最大值
- 遍历剩余的值
- 每当找到更大的值时,更新最大值
- 最后,该变量将包含序列中的最大值
编译并测试程序
现在我们已经编写好了完整的 C 程序,需要对其进行编译和运行,以验证它是否能正常工作。
要编译程序,请在终端中运行以下命令:
gcc ~/project/main.c -o ~/project/main此命令调用 GNU C 编译器(gcc)来编译源文件
main.c,并创建一个名为main的可执行文件。-o标志用于指定输出文件名。如果代码中没有错误,该命令将在执行时不产生任何输出。这意味着你的程序已成功编译。
如果你看到任何错误消息,请仔细阅读以了解问题所在。常见的错误包括:
- 缺少分号 (
;) - 括号 (
{和}) 不匹配 - 变量名或类型错误
- 缺少或错误的包含语句
- 缺少分号 (
一旦程序成功编译,使用以下命令运行它:
~/project/main使用不同的输入来测试你的程序。以下是一个示例测试用例:
输入:
Enter the number of elements you wish to find the greatest element of: 5 Enter 5 numbers: Enter element 1: 12.5 Enter element 2: 9.7 Enter element 3: 25.8 Enter element 4: 15.2 Enter element 5: 4.9预期输出:
The largest of the 5 numbers is 25.80尝试另一个包含负数的测试用例:
输入:
Enter the number of elements you wish to find the greatest element of: 3 Enter 3 numbers: Enter element 1: -10.5 Enter element 2: -2.3 Enter element 3: -15.7预期输出:
The largest of the 3 numbers is -2.30还要测试只输入一个数字的情况:
输入:
Enter the number of elements you wish to find the greatest element of: 1 Enter 1 numbers: Enter element 1: 42.0预期输出:
The largest of the 1 numbers is 42.00
如果你的程序针对这些测试用例都能产生预期的输出,恭喜你!你已经成功实现了一个用于找出 N 个输入数字中最大值的程序。
编译和测试的过程是软件开发中不可或缺的一部分。它能让你验证代码是否按预期工作,并帮助你识别和修复任何错误或问题。
总结
在本次实验中,我们成功实现了一个 C 程序,用于找出用户输入的一组数字中的最大值。让我们回顾一下所完成的工作:
- 问题理解:我们明确了需要处理一组数字,并找出其中最大的那个。
- 程序结构:我们创建了一个结构良好的 C 程序,包含了正确的头文件包含、变量声明和合理的逻辑流程。
- 用户输入处理:我们实现了从用户获取输入的代码,并进行了输入验证,以确保数据的正确性。
- 算法实现:我们使用了一个简单而有效的算法来找出最大值:
- 用第一个值进行初始化
- 将后续的每个值与当前最大值进行比较
- 当找到更大的值时,更新最大值
- 测试与执行:我们编译了程序,并使用各种输入进行测试,以验证其是否能正常工作。
本次实验展示了在许多场景中都非常有用的基本编程概念:
- 顺序执行
- 条件语句
- 循环结构
- 变量跟踪
- 输入/输出操作
完整代码
以下是我们在本次实验中开发的完整代码:
#include <stdio.h>
int main() {
// Declare variables
int n; // To store the number of elements
float big; // To store the largest number found
// Prompt the user for the number of elements
printf("Enter the number of elements you wish to find the greatest element of: ");
scanf("%d", &n);
// Check if the input is valid
if (n <= 0) {
printf("Please enter a positive number of elements.\n");
return 1; // Exit with error code
}
// Prompt for the first number and initialize 'big' with it
printf("Enter %d numbers:\n", n);
printf("Enter element 1: ");
scanf("%f", &big);
// Process remaining numbers using a loop
for (int i = 2; i <= n; i++) {
float current; // Variable to store the current number
// Prompt for the current number
printf("Enter element %d: ", i);
scanf("%f", ¤t);
// Compare with the current largest
if (current > big) {
big = current; // Update 'big' if current number is larger
}
}
// Display the result
printf("The largest of the %d numbers is %.2f\n", n, big);
return 0;
}
你可以通过以下几种方式扩展这个程序:
- 同时找出最大和最小的数字
- 计算所有数字的平均值
- 按升序或降序对数字进行排序
- 处理更复杂的数据结构,如数组
我们希望本次实验能帮助你理解 C 编程和算法思维的基础知识。这些概念是更高级编程主题和解决问题技巧的基础。



