ANSI C-数组上的更高元素

ANSI C-数组上的更高元素,c,arrays,numbers,ansi,C,Arrays,Numbers,Ansi,我在代码中遇到了一些问题,无法在5个元素的数组中获得最大值,这是我的代码: #include <stdio.h> #include <stdlib.h> #include <time.h> float timerunner1[4]; int x; int main() { for(x=1;x<6;x++) { printf("Give me the time of runner 1: "); scanf("%f",timerunne

我在代码中遇到了一些问题,无法在5个元素的数组中获得最大值,这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

float timerunner1[4];
int x;

int main() {

for(x=1;x<6;x++) {
    printf("Give me the time of runner 1: ");
    scanf("%f",timerunner1[x]);
}
return 0;
}
如何获得阵列的最高和最低数目? 可能使用for或if。。怎么做


谢谢

它实际上不起作用,您需要使用运算符“&”的地址将值存储在数组中

scanf(“%f”和timerunner1[x])

此外,数组不够大,无法存储循环所需的6个整数,数组的下标从0开始,到5结束(对于6个元素)

然后,您可以在读取所有值后使用另一个循环来计算最大值,也可以动态计算,如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

float timerunner1[6];
int x;
float maximum = 0.0f;

int main() {

for (x = 0; x < 6; x++) {
    printf("Give me the time of runner 1: ");
    scanf("%f", &timerunner1[x]);
    maximum = maximum > timerunner1[x] ? maximum : timerunner1[x];
}

printf("%f\n", maximum);

return 0;
}
#包括
#包括
#包括
浮点计时器1[6];
int x;
浮动最大值=0.0f;
int main(){
对于(x=0;x<6;x++){
printf(“给我跑步者1的时间:”);
scanf(“%f”和timerunner1[x]);
最大值=最大值>timerunner1[x]?最大值:timerunner1[x];
}
printf(“%f\n”,最大值);
返回0;
}

此外,此代码仅适用于正值,因为最大值初始化为零,并且始终大于任何负值。如果您需要负值,您应该能够进行实验并计算出来。

好的,在此程序中,您必须手动加载每个玩家的时间

/* StackFlow

Find the highest of an array of 5 numbers */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main(void) {

    float timerunner1[ 5 ] ={ 0 };
    float highest;
    int highestindex, i;

    /* Data input*/
    for( i = 0; i < 5; i++ ){
            printf( "\nEnter the %d element of the array: ", i );
            scanf( %f, timerunner1[ i ] );
    }

    /* Considering that the first generated number is the highest*/
    highest = timerunner1[ 0 ];
    highestindex = 0;

    /* The first element of an array is [0] not [1]*/
    for( i = 1; i < 5; i++ ) {

        /* if the next element in the array is higher than the previous*/
        if ( highest < timerunner1[ i ]){
            highest = timerunner1[ i ];
            highestindex = i;
        }

    }

    printf("\nThe highest time of the runner %d is: %f \n", highestindex, highest);
    return 1;
}
/*堆栈流
查找5个数字数组中的最高值*/
#包括
#包括
#包括
内部主(空){
float timerunner1[5]={0};
浮动最高;
int highestindex,i;
/*数据输入*/
对于(i=0;i<5;i++){
printf(“\n输入数组的%d元素:”,i);
scanf(%f,timerunner1[i]);
}
/*考虑到第一个生成的编号是最高的*/
最高=timerunner1[0];
highestindex=0;
/*数组的第一个元素是[0]而不是[1]*/
对于(i=1;i<5;i++){
/*如果数组中的下一个元素高于上一个元素*/
if(最高
您的声明
timetunner1[4]
创建一个由四个元素组成的数组,而不是五个元素。有效的索引是0..4,因此您的
for
循环跳过第一个条目,并寻址超过数组高端的元素。请找一个好的初学者。你可以在输入数字时或之后计算。是的,你需要一个循环。请在提问之前仔细考虑一下。如果你已经考虑过了,那就试试吧。如果你追求专业的编程,你将为自己的生活做准备,这将涉及到很多孤独的探索性编码。如果你害怕编译和运行一个可能不起作用的程序,你就走不了多远了。。。0,1,2,3,4拜托,我没有时间推荐书,只要知道ANSI C的人帮个忙,我是个乞丐。我不想要推荐书,不管你知道与否。不要认为这是一个不好的评论,但我来这里不是为了编写故事,只是为了一个答案。你看到变量声明中的
[4]
了吗?您已经创建了一个包含四个元素的数组。你知道
C
数组索引是基于零的吗?这就是为什么您的
for
循环是错误的。我建议写一本好书,因为你缺少一些非常基本的概念。
/* StackFlow

Find the highest of an array of 5 numbers */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main(void) {

    float timerunner1[ 5 ] ={ 0 };
    float highest;
    int highestindex, i;

    /* Data input*/
    for( i = 0; i < 5; i++ ){
            printf( "\nEnter the %d element of the array: ", i );
            scanf( %f, timerunner1[ i ] );
    }

    /* Considering that the first generated number is the highest*/
    highest = timerunner1[ 0 ];
    highestindex = 0;

    /* The first element of an array is [0] not [1]*/
    for( i = 1; i < 5; i++ ) {

        /* if the next element in the array is higher than the previous*/
        if ( highest < timerunner1[ i ]){
            highest = timerunner1[ i ];
            highestindex = i;
        }

    }

    printf("\nThe highest time of the runner %d is: %f \n", highestindex, highest);
    return 1;
}