C language Exercise: Second largest integer

Source: Internet
Author: User

Problem description

Write a program that reads in a set of integers (no more than 20), and when the user enters 0 o'clock, the input ends. The program will then find the second largest integer from this set of integers and print it out. Note: (1) 0 indicates the end of the input, which itself is not counted in this set of integers. (2) In this set of integers, both positive and negative numbers are possible. (3) The number of integers in this group is not less than 2.

Input format: Enter only one line, including several integers, separated by a space, the last integer is 0.

Output format: Output the second largest integer.

Input/Output sample

Sample input

5 8-12 7 0

Sample output

7

Experience: The first idea is not very good, want to use bubble sort and then output the second, forget the maximum can be repeated, the second experiment, forget the minimum value can be repeated. Finally look at the method to find the maximum value, and then the two most important method is out, you can realize that the largest value is always accompanied by the maximum value. You are too weak. Do not know what the reason, the following code is always not get full marks, do not know which test error. Enthusiastic netizens can try to write to help me see it t.t

Bubbles are not a very sensible way to find the smallest possible.

Ps:1, "66,66,66,66,66", there is no second largest number, nothing output.

2, "99,99,88,86,68,66", the maximum number is 88.

3. "0" does not output anything.

#include <stdio.h>
int main (void)
{
int x, y;
int a[20]={0};
int i=-1;
int n;
Do
{
i++;
scanf ("%d", &a[i]);


}while (a[i]!=0&&i<19);

if (i>=1)
{

X=A[0];Y=A[0];
for (n=0;n<i;n++)//assigns the maximum value in the array to X
if (X<a[n])
X=a[n];
for (n=0;n<i;n++)//assigns the second large value to Y
{
if (a[n]==x)
Continue
else if (a[n]>y)
Y=a[n];
}
if (x!=y)
printf ("%d", y);}
return 0;
}

C language Exercise: Second largest integer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.