-8

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.

Test cases pass but score is 0.0

Here's the code

int main()
{
    int a=-32768,b=-32768,temp=-32768;
    while(1)
    {
        scanf("%d",&a);
        if(a==-1)
            break;
        else if(a>0)
        {
            temp=b;
            b=a;

        }
        else
        {
            if(a>b)
            {
                temp=b;
                b=a;
            }
            else
            {
                temp=b;
            }
        }

    }
    printf("%d",temp);
    return 0;
}
wkl
  • 77,184
  • 16
  • 165
  • 176
Gaurav
  • 296
  • 1
  • 6
  • 21
  • This looks like homework. What have you tried so far and what's not working? – Code Different Jul 20 '15 at 16:02
  • Your code doesn't work for the very simple input like this :: `7 6 3 4 1 2 3 5 -1` Expected output `6` your code output `3`http://ideone.com/yHacZG – user007 Jul 20 '15 at 16:05
  • Look at your `if-else` statements. Work through an example like what user007 posted and see what happens because of how your statements are structured. You'd rarely enter the final part of the outer `if-else` chain, do you see that? – wkl Jul 20 '15 at 16:08
  • Previous iterations of this exact same question: [last week](http://stackoverflow.com/questions/31476653/finding-the-second-largest-elementyou-may-not-use-arrays-in-c), [last year](http://stackoverflow.com/questions/25490946/how-to-find-the-second-largest-element-using-c-program-without-using-array) – Steve Summit Jul 20 '15 at 16:10
  • Your code is actually printing second last number entered. – ameyCU Jul 20 '15 at 16:11
  • 1
    Have a downvote because of 'a', 'b'. Next time you write homework, try using descriptive var names like 'largest', 'secondLargest'. – Martin James Jul 20 '15 at 16:16
  • Debugger, debugger.... – Martin James Jul 20 '15 at 16:17
  • As the numbers input may be negative (except for the flag `-1`) I don't see the relevance of the test `if(a>0)` or indeed why it's preceded by an `else`. – Weather Vane Jul 20 '15 at 16:36
  • Just in case you are still in trouble, u might want to look at some answers to this! Might help you http://stackoverflow.com/questions/25490946/how-to-find-the-second-largest-element-using-c-program-without-using-array?lq=1 Exactly the same question! – user007 Jul 20 '15 at 17:05

1 Answers1

2

Bad check of value in your way. For example, like this

#include <stdio.h>

#define EOI -1 //End Of Input

int main(void){
    int input, first, second;

    first = second = EOI;

    while(1){
        if(1 != scanf("%d", &input)){
            fprintf(stderr, "invalid input!\n");
            return -1;
        }
        if(input == EOI)
            break;
        if(first == EOI){
            first = input;
        } else if(first <= input){
            second = first;//slide
            first = input;
        } else if(second == EOI || second < input){
            second = input;
        }
    }
    if(second == EOI){//first == EOI || second == EOI
        fprintf(stderr, "No valid value is entered more than one!\n");
        return -2;
    }
    printf("%d\n", second);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70