-4
#include<stdio.h>

int main() {
    int i, m1, m2, n, num;
    puts("\n");
    scanf("%d",&n);
    for(i = 0; i < n; i++) {
         scanf("%d", &num);
         if(i == 0) {
              num = m1 = m2;
         }
         if(num > m1) {
              m2 = m1; 
              m1 = num; 
         } else if(num > m2) {
              m2 = num;
         }
     }
    return 0;
}

my stdin: -950 -588 -169 -187 -445 400 -1

I have to get stdout: -169 but its showing stdout: \n

Note: I want to solve this problem without arrays.

Ryan Haining
  • 30,835
  • 10
  • 95
  • 145
Nidhi Murthy
  • 17
  • 2
  • 2
  • 4

5 Answers5

1

the statement:

num = m1 = m2;

is wrong and it does not cause the three variables to have the same value. You need to assign m1 and m2 to num. You are overwriting the variable that you previously had read. Change it to:

m1 = num;
m2 = num;

Then, print out the m2.

EDIT:

As others found out, the -1 states for end of your input. Adding simple if statement solves the problem and for your input -169 is the second largest element.
Full code:

#include<stdio.h>
int main(){
int i, m1, m2, n, num;
puts("\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
     scanf("%d",&num);
     if(i==0)
     {
          m1 = num;
          m2 = num;
     }
     else if(num == -1)  /* if -1 was read, then terminate the loop. */
     {
         break;
     }
     else if(num>m1)
     {
          m2 = m1; 
          m1 = num; 
     }
     else if(num>m2)
     {
          m2=num;
     }
 }
printf("%d\n",m2);
return 0;
}

For input:

7
-950 -588 -169 -187 -445 400 -1

and current code output is -169.


Another EDIT:
Ok, your code is wrong because of the scanf for number of elements. In future It would be helpful if you were more clear about your problems. I hope that following code will work for you.

#include <stdio.h>

int main(void)
{
    int curr, second, first;
    scanf("%d", &curr);
    second = curr;
    first = curr;
    while (1) {
        scanf("%d", &curr);
        if (curr == -1) {
            break;
        }
        if (curr > first) {
            second = first;
            first = curr;
        }
        else if (curr > second) {
            second = curr;
        }
    }
    printf("%d\n",second);
    return 0;
}
macfij
  • 2,868
  • 1
  • 16
  • 23
  • actual output and expected output not matching for your code. it gave me this output \n \n 32767\n – Nidhi Murthy Aug 25 '14 at 20:33
  • Yes. -1 is the second largest element in your input. -169 is the third one. – macfij Aug 25 '14 at 20:36
  • but its showing 32767 instead -169. i tried with other test case also stdin: -840 -288 -261 -337 -335 488 -1 stdout: -32767 instead of -261 – Nidhi Murthy Aug 25 '14 at 20:38
  • do you provide at first a number of elements that will be read (in your case 7)? Also, in this another set of elements, again -1 is the second largest element. – macfij Aug 25 '14 at 20:41
  • Instead of the `continue`, why not just make the next `if` an `else if`? – lurker Aug 25 '14 at 20:44
  • @lurker ok, changed that, but I think with continue it was just fine as it is currently. – macfij Aug 25 '14 at 20:48
  • Yes it works with the `continue`, but the `else if` is better form. :) – lurker Aug 25 '14 at 20:49
  • @NidhiMurthy the solution shown above works fine. If you are seeing `32767` then you have not entered it properly. – lurker Aug 25 '14 at 20:50
  • @lurker ok I agree with you :) – macfij Aug 25 '14 at 20:53
  • @lurker no i checked 3-4 times. same code i entered. its showing 32767. – Nidhi Murthy Aug 25 '14 at 20:53
  • @NidhiMurthy are you under the impression that -1 is supposed to mean, "don't read any more input" rather than being part of the set of numbers? Because you never stated that, and you original code didn't show that either. – JohnH Aug 25 '14 at 20:56
  • @JohnH that might be the case. But then, what would be the point of having `scanf("%d",&n);` ? – macfij Aug 25 '14 at 21:01
  • @NidhiMurthy ok that's a bit confusing. I ran this code and it works fine using the input shown and yielding the result of `-1`. Can you edit your problem statement and show what your command looks like to compile the code? And as JohnH says, if you want `-1` to terminate your date, then you need to check for it in the code. – lurker Aug 25 '14 at 21:02
  • @lurker i dont have credit to upload my screenshots. new user so i cant upload. – Nidhi Murthy Aug 25 '14 at 21:07
  • I've edited my answer and now it does what you're looking for. However, please specify how you compile your code. – macfij Aug 25 '14 at 21:13
1
/* Program to find the second largest number without using array */
        main()
        {
         int num,large=0,slarge=0,i=0;
         clrscr();
         printf("Enter the number:");
         while(i<10)
         {
          scanf("%d",&num);
          if(i==0)
          {
          large=num;
          }
          else if(num>large)
          {
           slarge=large;
           large=num;
          }
          else if(num>slarge)
          {
           slarge=num;
          }
          i++;
         }
         printf("Large number:%d",large);
         printf("\nSecond large=%d",slarge);
         getch();
         return 0;
        }
ROHAN
  • 11
  • 2
0

The problem with your code is you are using m2 uninitialized. To correct the problem, set m2 to some reasonable negative number (like the smallest integer allowed). Here we are just using a negative number for example:

m2 = -1000000;

outout

argument [0]:  -950
argument [1]:  -588
argument [2]:  -169
argument [3]:  -445
argument [4]:  400
argument [5]:  -1

m1: 400
m2: -1

Your code does what you intend. -1 is the second largest number (400 is the largest). If you want -169, then you want the 3rd largest. Remember:

ALWAYS INITIALIZE YOUR VARIABLES

David C. Rankin
  • 69,681
  • 6
  • 44
  • 72
0

here's a working and simpler version :

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

  int main(int argc , char** argv)
  {
    int m1 , m2 , rc = 1;
    m1 = m2 = INT_MIN ;

    while(rc)
    {
            scanf("%d" , &rc);
            if(rc > m1)
                    m1 = rc;
            else if(rc < m1 && rc > m2)
                    m2 = rc;

    }
    printf("%d\n" , m2);

  }
Farouq Jouti
  • 1,605
  • 8
  • 14
0
/*Second largest elements in a given array*/
#include <stdio.h>
int SecondMax(int a[], int n) // n= array size 
{
    int max1, max2;     //assume max1 as largest and max2 is second largest 
    max1= max2= a[0];   //Initialize first element of array
    for(int i=0; i<n; i++)
    {
        if(a[i] > max1) //check each elements of array with max1 
        {
            max2= max1; 
            max1= a[i];
        }
        else if(a[i] > max2)
            max2= a[i];
    }
    return max2;
}

int main()  
{
    int a[10]={2, 54, 8, 9 ,12, 6, 3, 7, 32, -5};

    printf("\nmax2= %d", SecondMax(a, 10)); //print return value 
    return 0;
}
mpromonet
  • 8,785
  • 40
  • 48
  • 80
Kulamani
  • 461
  • 6
  • 13