user1479645
user1479645

Reputation: 51

How to find max int value from array

  1. User inputs positive int value (number);
  2. User prints number int values;
  3. Need to find max value and print it.

My code is

#include <stdio.h>

int main() {
    int number;
    int max;
    int temp;

    scanf("%d", &number);
    scanf("%d", &max);

    for ( int i = 1; i < number; i++ ) {
        scanf("%d", &temp);
        if ( temp > max ) {
            max = temp;
        }
    }

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

This works but online test tool says i need to optimize code, because it uses too many operations. Arrays are forbidden. Can only use stdio.

Upvotes: 3

Views: 1654

Answers (6)

Kijewski
Kijewski

Reputation: 26043

By using Duff's device you could save some comparisons in the for-loop. It'd be a bad practice, but maybe that's what you are expected to do.

#include <stdio.h>

int main (void) {
    unsigned max = 0;
    unsigned length;
    scanf("%u", &length);

    unsigned temp = 0;
    unsigned iterations = (length+8-1) / 8;
    switch (length % 8) {
        case 0: do { scanf("%u", &temp); if (temp > max) max = temp;
        case 7:      scanf("%u", &temp); if (temp > max) max = temp;
        case 6:      scanf("%u", &temp); if (temp > max) max = temp;
        case 5:      scanf("%u", &temp); if (temp > max) max = temp;
        case 4:      scanf("%u", &temp); if (temp > max) max = temp;
        case 3:      scanf("%u", &temp); if (temp > max) max = temp;
        case 2:      scanf("%u", &temp); if (temp > max) max = temp;
        case 1:      scanf("%u", &temp); if (temp > max) max = temp;
                } while (--iterations > 0);
    }

    printf("%u\n", max);
    return 0;
}

I used unsigned ints, because you said you only have positive numbers. The code assumes that the sequence has at least one element.

Update 1:

Example using manual loop unrolling. That's an even worse practice than Duff's device. Maybe the testing tool you got will like it, but you should never use this code to impress a potential employer!

#include <stdio.h>

int main (void) {
    signed max = -0x80000000;
    unsigned length;
    scanf("%u", &length);

    signed temp;
    for (; length >= 8; length -= 8) {
        scanf("%d", &temp); if (temp > max) max = temp;
        scanf("%d", &temp); if (temp > max) max = temp;
        scanf("%d", &temp); if (temp > max) max = temp;
        scanf("%d", &temp); if (temp > max) max = temp;
        scanf("%d", &temp); if (temp > max) max = temp;
        scanf("%d", &temp); if (temp > max) max = temp;
        scanf("%d", &temp); if (temp > max) max = temp;
        scanf("%d", &temp); if (temp > max) max = temp;
    }
    if (length > 4) {
        scanf("%d", &temp); if (temp > max) max = temp;
        scanf("%d", &temp); if (temp > max) max = temp;
        scanf("%d", &temp); if (temp > max) max = temp;
        scanf("%d", &temp); if (temp > max) max = temp;
        length -= 4;
    }
    for (; length > 0; --length) {
        scanf("%d", &temp); if (temp > max) max = temp;
    }

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

Update 2:

You stated that your evaluation tool likes it if scanf is less often called, so:

#include <stdio.h>
int main (void) {
    signed max = -0x80000000;
    unsigned length;
    scanf("%u", &length);

    signed t1, t2, t3, t4, t5, t6, t7, t8;
    for (; length >= 8; length -= 8) {
        scanf("%d%d%d%d%d%d%d%d", &t1, &t2, &t3, &t4, &t5, &t6, &t7, &t8);
        if (t1 > max) max = t1;
        if (t2 > max) max = t2;
        if (t3 > max) max = t3;
        if (t4 > max) max = t4;
        if (t5 > max) max = t5;
        if (t6 > max) max = t6;
        if (t7 > max) max = t7;
        if (t8 > max) max = t8;
    }
    if (length > 4) {
        scanf("%d%d%d%d", &t1, &t2, &t3, &t4);
        if (t1 > max) max = t1;
        if (t2 > max) max = t2;
        if (t3 > max) max = t3;
        if (t4 > max) max = t4;
        length -= 4;
    }
    for (; length > 0; --length) {
        scanf("%d", &t1); if (t1 > max) max = t1;
    }

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

Upvotes: 4

Ling Tze Hock
Ling Tze Hock

Reputation: 41

#include <stdio.h>

int main() {
    int number;
    int max;
    int temp,i;

    scanf("%d", &number);

    for ( i = 0; i < number; i++ ) {
        scanf("%d", &temp);
        if(i==0)
            max=temp;
        if ( temp > max ) {
            max = temp;
        }
    }

printf("max=%d\n", max);
return 0;

}

Upvotes: -1

user1479645
user1479645

Reputation: 51

Should have used 1 scanf() instead of two:

#include <stdio.h>

int main() {
    int number;
    int max;
    int temp;

    scanf("%d %d", &number, &max);
    for ( int i = 1; i < number; i++ ) {
        scanf("%d", &temp);
        if ( temp > max ) {
            max = temp;
        }
    }
    printf("%d\n", max);
    return 0;
}

Sorry, and thanks to everyone! Hugs and kisses!

Upvotes: 2

pmg
pmg

Reputation: 108986

You don't really need the i variable: you can use number itself.

Also you may want to abuse the for statement :)

#include <stdio.h>

int main(void) {
    int number;
    int max;
    int temp;

    for (scanf("%d", &number), scanf("%d", &max)
       ; --number && scanf("%d", &temp)
       ; )
    {
        if (temp > max) max = temp;
    }

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

Upvotes: 1

ArjunShankar
ArjunShankar

Reputation: 23680

Your 'test tool' is probably broken, or expects you to do meaningless micro-optimizations while making code hard to read, which the compiler will do anyway.

  1. You need to ask the user for the number of numbers => 1 scanf. You did this.
  2. Next you need to ask the user for number numbers => scanf, number times. You did this.
  3. Next you need to find the largest => loop number times, and compare. You already achieved this in the same loop as the previous one. And you did it well because you did the minimum possible number - 1 comparisons.
  4. Next you need to print the result => 1 printf. You did this too.

This is the fastest you can possibly get[1]. There is no scope for 'optimization'.

[1] If you had 2 cores, and were writing a multi-threaded program, you could do step 2 and 3 faster by pipelining them (see unkulunkulu's comments for why this is the fastest you can get).

Upvotes: 2

SpacedMonkey
SpacedMonkey

Reputation: 2773

I'd like to know what test tool says you have too many operations that isn't some code golf competition. Also, what the number of acceptable operations is and how they define 'operation'.

#include <stdio.h>

int main() {
    int numbersLeft,
        number,
        max = 0;

    scanf("%d", &numbersLeft);

    while ( numbersLeft-- ) {
        scanf("%d", &number);
        max = number > max? number: max;
    }

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

Upvotes: 2

Related Questions