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
Which term describes a visual object such as a picture, a table, or a text box?
Nat2105 [25]

Answer:

A) Caption

Explanation:

This option makes the most sense. It's not Word Art or Comment. An illustration is like a drawing, so that's incorrect as well, leaving only caption as the correct answer.

Hope it helps!

8 0
2 years ago
Read 2 more answers
to troubleshoot an output component or input device, the first step is to activate the input device that controls the first outp
Firdavs [7]

to troubleshoot an output component or input device, the first step is to activate the input device that controls the first output component It is TRUE statement.

<h3>What does troubleshooting mean?</h3>

The act of discovering, planning, and resolving a problem, mistake, or malfunction in a software or computer system is known as troubleshooting. When a computer or program becomes broken, unresponsive, or behaves abnormally, it can be repaired and restored.

Troubleshooting is the final stage of a complicated process. In order to be able to talk about something, we must bear that the solution was positive and that the whole problem was not positive. There are many types of conflicts and many ways to resolve them.

Learn more about input and output devices:brainly.com/question/17964714

#SPJ4

8 0
2 years ago
Read 2 more answers
What is a layer, in the context of this lesson?
pshichka [43]

Answer:

A logical unit of a system.

Explanation:

i'm thinking your are talking about system layers such as the ISO? Layers help identify the order of how the whole system works.

7 0
3 years ago
Read 2 more answers
Declare a character variable letterStart. Write a statement to read a letter from the user into letterStart, followed by stateme
oee [108]
  1. Answer: import java.util.Scanner; public class CharTestt {     public static void main(String[] args) {     System.out.println("Please enter a character ");     Scanner input = new Scanner(System.in);     char letterStart = input.next().charAt(0);     char thenextChar = (char)(letterStart+1);     System.out.print(letterStart);     System.out.println(thenextChar); } } Explanation: Import Scanner Class Prompt user to enter a character Read and save user's input in a variable char letterStart = input.next().charAt(0); Knowing that the next character using ASCII is +1, create a new character variable and add 1 print the character entered and the new character all on same line without spaces
5 0
3 years ago
_______ is the protocol suite for the current Internet.
Gala2k [10]
A? not very sure sorry
8 0
3 years ago
Other questions:
  • The n modifier after the tilde forces the index variable to expand only to the ______
    15·1 answer
  • What is special about the f and j keys on a standard keyboard?
    15·2 answers
  • True / False<br> Registers are generally optimized for capacity instead of speed.
    14·1 answer
  • Jennifer recently bought a new computer to type a new manuscript she’s been working on. She also stored a lot of movies on it to
    10·1 answer
  • Discuss anomaly detection.
    5·1 answer
  • Write a program that asks the user for three strings. Then, print out whether the first string concatenated to the second string
    8·1 answer
  • What are the pros and cons of using a linked implementation of a sparse matrix, as opposed to an array-based implementation?
    11·1 answer
  • Danica is creating a flyer for her cookies that she will sell during her school fair. She wants to add a registered
    7·1 answer
  • When you press the ____ key without entering any text after the automatic bullet character, Word turns off the automatic bullets
    11·1 answer
  • Define artificial intelligence?​
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!