1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Naya [18.7K]
3 years ago
15

Second Largest, Second Smallest Write a program second.cpp that takes in a sequence of integers, and prints the second largest n

umber and the second smallest number. Note that in the case of repeated numbers, we really mean the second largest and smallest out of the distinct numbers (as seen in the examples below). You may only use the headers: and . Please have the output formatted exactly like the following examples: (the red is user input) Enter integers (Q to quit): 1 2 1 8 8 9 Q Second largest: 8 Second smallest: 2 Enter integers (Q to quit): -7 -101 0 -2 17 Q Second largest: 0 Second smallest: -7
Computers and Technology
1 answer:
Ulleksa [173]3 years ago
3 0

Answer:

The program in C++ is as follows:

#include <vector>

#include <iostream>

using namespace std;

int main(){

   vector <int> my_num;

   string sentinel;

   int n = 0;

   cout<<"Enter integers (Q to quit): ";

   cin>>sentinel;

   while(sentinel != "Q"){

       my_num.push_back(stoi(sentinel));

       n++;

       cin>>sentinel;    }

      int n1, n2;

      n1 = my_num.at(0);      n2 = my_num.at(1);

      if(my_num.at(0)<my_num.at(1)){     n1 = my_num.at(1);  n2 = my_num.at(0);   }

      for (int i = 2; i< n ; i ++) {

          if (my_num.at(i) > n1) {

              n2 = n1;

              n1 = my_num.at(i);     }

           else if (my_num.at(i) > n2 && my_num.at(i) != n1) {

               n2 = my_num.at(i);     }  }

       cout<<"Second Largest: "<<n2<<endl;

       n1 = my_num.at(1);       n2 = my_num.at(0);

       if(my_num.at(0)<my_num.at(1)){ n1 = my_num.at(0);  n2 = my_num.at(1);   }

       for(int i=0; i<n; i++) {

           if(n1>my_num.at(i)) {  

               n2 = n1;

               n1 = my_num.at(i);            }

           else if(my_num.at(i) < n2){

               n2 = my_num.at(i);     }  }

 cout<<"Second Smallest: "<<n2;

 return 0;

}

Explanation:

See attachment for explanation

Download cpp
You might be interested in
What would be used by a business to assess how the business is working within its organization goals? O A. Information systems B
gulaghasi [49]

Answer:

Management plan

Explanation:

3 0
3 years ago
Answer this if you can help me!
kobusy [5.1K]

Answer:

dsaxd

Explanation:

7 0
3 years ago
Read 2 more answers
To protect yourself from the risk of product failure, what strategy could you use?
Sloan [31]

I would move the file into a folder and zip it or move the it into a flash drive

5 0
3 years ago
Ada lovelace designed the first computer
muminat

Answer:      yes.                        

this was A very good question and after alot of digging up i finally found the answer by searching who was the first computer programmer on google :3

8 0
3 years ago
Which statement is false? Select one: a. A class is to an object as a blueprint is to a house. b. Classes are reusable software
kow [346]

Answer:

A class is an instance of its object

Explanation:

6 0
3 years ago
Other questions:
  • Which socket type is soldered to the motherboard, along with the cpu?
    6·1 answer
  • What is the difference between First Person Shooters and Construction Simulations?
    12·2 answers
  • Filtering removes data from the spreadsheet. A. True B. False
    9·2 answers
  • Which step is common to both creating a new document and saving a document?
    11·2 answers
  • Which of the following sentences uses correct punctuation? A. I am a good communicator, and I am a strong team member. B. I comm
    12·2 answers
  • Which of the following is not hardware? Question 7 options: A) Wireless network router B) Printer C) Virus Scanner D) Flat-panel
    11·1 answer
  • Write a function that takes an integer value and returns the number with its digits reversed. for example, given the number 7631
    10·1 answer
  • A logistics company's vulnerability scan identifies the following vulnerabilities on Internet-facing devices in
    8·1 answer
  • What is Smart Content Profits all about?
    10·2 answers
  • In your opinion., which Zelda character is the hottest? Be honest.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!