I want to find the second highest number from a sequence without using an array.
The instructions are the following.
You are given a sequence of integers as input, terminated by a -1. (That is, the input integers may be positive, negative or 0. A -1 in the input signals the end of the input.)
-1 is not considered as part of the input.
Find the second largest number in the input. You may not use arrays.
What I did is this.
#include <stdio.h>
int main(void) {
int i, a, temp, sec;
while (1) {
scanf("%d", &a);
if (a == -1) ;
break;
temp = a;
while (1) {
scanf("%d", &a);
if (a == -1) {
break;
}
if (a > temp)
sec = temp;
else
sec = a;
}
}
printf("%d", sec);
return 0;
}
Sample test cases will be like the following ones.
Sample Test Cases
Input
Test Case 1
-840 -288 -261 -337 -335 488 -1
Output
-261
Test Case 2
-840 -335 -1
Output
-840
I don't know where to get started and where I am getting wrong.