gpt4 book ai didi

c - else if 语句阻止程序检测最大索引

转载 作者:行者123 更新时间:2023-11-30 21:36:43 25 4
gpt4 key购买 nike

该程序的目的是检测命名数组中的最大数字。但目前它仅检测数组的最后一位数字。如果 else if 被删除或注释掉,那么它可以正确检测除最后一个数字之外的所有数字。

#include <stdio.h>

int main(){
int c,i,max;

printf("Enter the size of the array.");
scanf("%d",&c);

int array[c];

printf("Enter the integers to fill the array.\n");
for(i=0;i<c;i++){
scanf("%d",&array[i]);
}//end for

int last_element = array[c-1];

for(i=0;i<c;i++){
printf("%d",array[i]);
if(array[i-1] > array[i]) //This if statement detects greatest
{ //index of array for all but last index
max = array[i-1];
}//end if
else if(last_element > array[i-1]) //This else if detects greatest
{ //index of array in last index
max = last_element; //This statement is always eval
}//end if //uating true.
}//end for

printf("\n%d",max);
return 0;
}//end main

最佳答案

largest = array[0];            //consider your 1st element as largest element
for (i = 1; i < size; i++) //
{
if (largest < array[i]) //compare largest element with i'th element
largest = array[i]; //if it is, make i'th element as largest
}
printf("\n largest element present in the given array is : %d", largest);

试试这个

关于c - else if 语句阻止程序检测最大索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46101786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com