C 语言实例 - 查找数组中最大的元素值
C 语言教程
· 2019-02-21 06:14:15
查找数组中最大的元素值。
实例 1
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int loop, largest;
largest = array[0];
for(loop = 1; loop < 10; loop++) {
if( largest < array[loop] )
largest = array[loop];
}
printf("最大元素为 %d", largest);
return 0;
}
输出结果为:
最大元素为 9
用户自定义输出:
实例 2
#include <stdio.h>
int main()
{
int i, n;
float arr[100];
printf("输入元素个数(0~100): ");
scanf("%d", &n);
printf("\n");
// 接收用户输入
for(i = 0; i < n; ++i)
{
printf("输入数字 %d: ", i+1);
scanf("%f", &arr[i]);
}
// 循环,并将最大元素存储到 arr[0]
for(i = 1; i < n; ++i)
{
// 如果要查找最小值可以将 < 换成 >
if(arr[0] < arr[i])
arr[0] = arr[i];
}
printf("最大元素为 = %.2f", arr[0]);
return 0;
}
输出结果为:
输入元素个数(0~100): 4 输入数字 1: 12 输入数字 2: 23 输入数字 3: 1 输入数字 4: 3 最大元素为 = 23.00
点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html
Beautiful Code
Greg Wilson、Andy Oram / O'Reilly Media / 2007-7-6 / GBP 35.99
In this unique work, leading computer scientists discuss how they found unusual, carefully designed solutions to difficult problems. This book lets the reader look over the shoulder of major coding an......一起来看看 《Beautiful Code》 这本书的介绍吧!