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