0

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.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Atanu Mondal
  • 61
  • 1
  • 12
  • 1
    your above code is not correct for given input. – roottraveller Aug 02 '17 at 18:17
  • this is not giving the right answer. please, can you correct it? – Atanu Mondal Aug 02 '17 at 18:18
  • 1
    it is giving the correct answer, what do you think the correct answer is? – pm100 Aug 02 '17 at 18:18
  • for the first test case, the actual output is showing this 32764 – Atanu Mondal Aug 02 '17 at 18:21
  • ah - we though you were showing your output. – pm100 Aug 02 '17 at 18:24
  • 2
    how would you do this if you were asked to do it in a conversation. I will recite a sequence of numbers to you, you cannot write them all down. When I finish you must tell me the value of the second highest number. How would you do it. Note , you can write 2 numbers down. – pm100 Aug 02 '17 at 18:26
  • Think of doing this with cards. Separate out the Hearts. Shuffle them. Draw two cards. Now find the two highest cards; the rules are you can only draw one card at a time, and every time you draw one you must discard one so you're left with two cards in your hand. – Ken White Aug 02 '17 at 20:30

4 Answers4

4

I won't solve your homework, but will offer some hints:

  1. You should give your variables clear names that reflect their purpose. Names like i, a, temp and sec make the code unnecessarily hard to reason about.

  2. You should initialize all variables. For example, the initialization of sec is a bit iffy.

  3. You don't need two loops.

  4. The following if is a no-op (which means that the break that follows is executed unconditionally):

    if (a == -1) ;

Finally, and most importantly, there are three cases you need to handle when iterating over the numbers:

  1. The current number is larger than the largest number seen thus far.

  2. The current number is not larger than the largest but is larger than the second-largest number seen thus far.

  3. The current number is not larger than the second-largest number.

Hopefully, this'll give you enough direction to make more progress. If you get stuck, please post another question.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

Try this.

#include <stdio.h>
#define INT_MIN -999999;  //define minimum value

int main() {
  int input, firstMax = INT_MIN;
  int secMax = INT_MIN;
  do {
    scanf("%d", &input);
    if (input == -1)
    break;

    if(input > firstMax) {  //find new first max value
        secMax = firstMax;  //set previous max to second max
        firstMax = input;   //update first max to new value
    }
    else if(input > secMax) {
        secMax = input;     //new second max value is found. update it
    }
  } while(1); 

  printf("%d", secMax);
  return 0;
}
roottraveller
  • 7,942
  • 7
  • 60
  • 65
0

How about this?

EDIT: works for me in GCC version 4.3.4 on SUSE Linux 11

#include <stdio.h>
#include <limits.h>

int main(void) {
  int i, a, temp, sec, largest;
  sec = INT_MIN;
  largest = INT_MIN;
  while (1) {
    scanf("%d", &a);
    if (a == -1) {
        break;
    }
    if (a > largest) {
        sec = largest;
        largest = a;
    } else if (a > sec) {
        sec = a;
    }
  }

  printf("%d", sec);
  return 0;
}

I haven't corrected some bad code smells but I've just made a quick and dirty answer

Srini
  • 1,619
  • 1
  • 19
  • 34
  • please provide a comment so that I can improve my answer , don;t just downvote – Srini Aug 02 '17 at 18:32
  • NMDV, `int sec = -INFINITY;` is _undefined behavior_. – chux - Reinstate Monica Aug 02 '17 at 18:33
  • I don't understand what that means, I just compiled this and it works. INFINITY is defined in math.h which I have included. this answer also corroborates this [here](https://stackoverflow.com/questions/1923837/how-to-use-nan-and-inf-in-c) – Srini Aug 02 '17 at 18:36
  • Unless you meant to say it is undefined because it is implementation dependent , the OP did not specify his platform , I've now edited my answer to specify mine – Srini Aug 02 '17 at 18:38
  • Which part do you not understand the meaning: NMDV, `INFINITY`, _undefined behavior_, or something else? – chux - Reinstate Monica Aug 02 '17 at 18:38
  • about INFINITY being undefined, could you please elaborate – Srini Aug 02 '17 at 18:39
  • 1
    `INFINITY` is defined in ``, it is some large value well outside the `int` range. Assigning a value outside the `int` range to an `int` is the problem. It is either _undefined behavior_ or _implementation defined behavior_ - I'll need to check that. Why not use `sec = INT_MIN;` - no issues with that? – chux - Reinstate Monica Aug 02 '17 at 18:43
  • 1
    `sec = -INFINITY;` --> "Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised." – chux - Reinstate Monica Aug 02 '17 at 18:46
-1

You can try this way :

#include <stdio.h>
#include <limits.h>


int main() {
  int a, first = INT_MIN, sec = INT_MIN;
  while(1){
    scanf("%d", &a);
    if (a == -1)
      break;

    if(a > first) {
        sec = first;
        first = a;
    }
    else if(a > sec) {
        sec = a;
    }
  }

  printf("%d", sec);
  return 0;
}
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45