Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:38:10+00:00 2026-06-11T14:38:10+00:00

Help me understand the last portion of this code that aims to print the

  • 0

Help me understand the last portion of this code that aims to print the largest and second largest number in a sequence. What I am not fully understanding is, what is the need for the else if statement? with the code:

 if (input > largest) {
        secondLargest = largest;
        largest = input;

should that not do the job proper? it checks if input is greater than the largest number, sets the second Largest to the previous largest number. and update the new largest number with the one user inputed.

So what exactly is the purpose of this line of code then? and any reason the integers largest and secondLargest is set to -1and not just 0, has it to do with the sentinel that breaks the program is set to 0?

} else if (input > secondLargest) {
    secondLargest = input;

.

 int largest = -1;
  int secondLargest = -1;
  while (true) {
     int input = readInt(" ? ");
     if (input == SENTINEL) break;
     if (input > largest) {
        secondLargest = largest;
        largest = input;
     } else if (input > secondLargest) {
        secondLargest = input;
     }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:38:11+00:00Added an answer on June 11, 2026 at 2:38 pm

    Try your program with the sequence

        1 2 3 5 4
    

    If you omit else if (input > secondLargest) { secondLargest = input; } then result will be largest=5 and secondLargest=3, that is incorrect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me understand the following code snippet :- def any(l): whether any number
Can someone help me understand how to write this case statement properly its not
Please help me understand the meaning of this code. I have seen this kind
Please help me understand this. Here you can see that I have PYTHONPATH set
Please help me understand this recursive function... var stack = Array; function power(base, exponent){
Please help me understand where I am going wrong so much so that I
Can you help me understand how I should debug this errors? What information would
can anybody help me understand how Honda achieved this effect: http://testdays.hondamoto.ch/ I mean the
I got segmentation fault for the following code, could someone help me understand why?
Please help me understand why add1() and add4() report errors and why add2() and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.