C程序以找到的最大元素中的一个阵列

C程序以找到的最大元素中的一个阵列

例如:最大元素中的一个阵列

#include <stdio.h>
int main() {
  int n;
  double arr[100];
  printf("Enter the number of elements (1 to 100): ");
  scanf("%d", &n);

  for (int i = 0; i < n; ++i) {
    printf("Enter number%d: ", i + 1);
    scanf("%lf", &arr[i]);
  }

  // storing the largest number to arr[0]
  for (int i = 1; i < n; ++i) {
    if (arr[0] < arr[i]) {
      arr[0] = arr[i];
    }
  }

  printf("Largest element = %.2lf", arr[0]);

  return 0;
}

Output

Enter the number of elements (1 to 100): 5
Enter number1: 34.5
Enter number2: 2.4
Enter number3: -35.5
Enter number4: 38.7
Enter number5: 24.5
Largest element = 38.70

This program takes n number of elements from the user and stores it in the arr array.

To find the largest element,

  • the first two elements of array are checked and the largest of these two elements are placed in arr[0]
  • the first and third elements are checked and largest of these two elements is placed in arr[0].
  • this process continues until the first and last elements are checked
  • the largest number will be stored in the arr[0] position
// storing the largest number at arr[0]
for (int i = 1; i < n; ++i) {
  if (arr[0] < arr[i]) {
    arr[0] = arr[i];
  }
}

本文来自投稿,不代表亲测学习网立场,如若转载,请注明出处:https://www.qince.net/array-largest-element.html

郑重声明:

本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。 若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。

我们不承担任何技术及版权问题,且不对任何资源负法律责任。

如遇到资源无法下载,请点击这里失效报错。失效报错提交后记得查看你的留言信息,24小时之内反馈信息。

如有侵犯您的版权,请给我们私信,我们会尽快处理,并诚恳的向你道歉!

(0)
上一篇 2023年11月30日 下午11:26
下一篇 2023年12月1日 上午12:19

猜你喜欢