-3

I'm having some issues on my code to get the highest number on an array of 5 elements, this is my code:

#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;
}

This works perfectly, the output is:

Give me the time of runner 1:  14
Give me the time of runner 1:  3
Give me the time of runner 1:  10
Give me the time of runner 1:  5
Give me the time of runner 1:  2

How can I get the highest and lowest number of the array? Maybe using a for or if.. How?

Thanks!

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Daniel
  • 378
  • 1
  • 6
  • 20
  • 3
    Your declaration `timetunner1[4]` creates an array of __four__ elements, not five. The valid indices are 0..4 so your `for` loop skips the first entry and addresses an element past the high end of the array. Please pick up a good beginner [C book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Blastfurnace Oct 07 '12 at 23:15
  • 1
    You can compute this either while the numbers are being input or afterwards. Yes, you need a loop. Please actually think about it before asking on SO. If you *have* thought about it, then please *have a go at it*. If you pursue programming professionally, you are setting yourself up for a life that involves much solitary explorative coding. If you are scared to compile and run a program that might not work, you will not get far. – paddy Oct 07 '12 at 23:16
  • They are five... 0,1,2,3,4 Please I have no time for book recommendations, just a quick help from anyone who knows ANSI C, I'm a begginer. – Daniel Oct 07 '12 at 23:17
  • I don't want recommendations please, you know or not. Don't take it as a bad comment but I'm not here for programming stories just an answer. – Daniel Oct 07 '12 at 23:20
  • @Daniel, when you declare an array, you are passing it the *size* of the array, not the index size so you will end up with 0..4 as Blastfurnace suggested. – Ben Neill Oct 07 '12 at 23:27
  • 1
    Do you see that `[4]` in the variable declaration? You've created an array of __four__ elements. Do you know that `C` array indices are zero-based? That's why your `for` loop is wrong. I suggested a good book because you're missing some very fundamental concepts. – Blastfurnace Oct 07 '12 at 23:29
  • -1 The downvote arrow says "This question does not show any research effort" which I take as an instruction to click it. – Jim Balter Oct 08 '12 at 00:15
  • 'the output is:' -- well, no, the variable part of that is *input* ... you typing things. – Jim Balter Oct 08 '12 at 00:17

2 Answers2

1

It doesn't work actually, you need to use the address of operator '&' to store the value in the array.

scanf("%f", &timerunner1[x]);

Also, your array isn't large enough to store the 6 integers that your loop is requiring and subscripting of an array starts at zero and ends at 5 (for 6 elements).

You can then either have another loop AFTER reading all your values to calculate the maximum or calculate it on the fly as below:

#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;
}

Also, this code only works on positive values because maximum is initialised to zero and will always be larger than any negative value, if you need negative values, you should be able to experiement and figure that out.

goji
  • 6,911
  • 3
  • 42
  • 59
1

Ok, in this program you will have to load the time of each player manually.

/* 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;
}
Alberto Bonsanto
  • 17,556
  • 10
  • 64
  • 93