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
Ima go to sleep but I ask a favor can u guys go on eBay Amazon and find the cheapest pre bilt that is actually good
katrin2010 [14]

Explanation:

what do specifically want to buy??

8 0
3 years ago
Read 2 more answers
A cell reference with only one dollar sign before either the column or the row is called an absolute reference.
BlackZzzverrR [31]
False, absolute references have two dollar signs
4 0
3 years ago
What is outlook used for?
Arte-miy333 [17]
Outlook is used for mainly emails. It also keeps track of your calendar, has contact and tasks.
4 0
2 years ago
Read 2 more answers
Named constants should be used when they serve to improve readability and understanding. T or F
prisoha [69]
The answer is true.

Let's say we are calculating the volume of a grain silo where the the width is a constant, but the height can be changed.

In our code we would calculate the volume using something like:

PI * (WIDTH / 2)^2 * height

The variables in all caps would be named constants. Using them makes the code more readable to other people than if we were to just use their values like:

3.14 * (145.75 / 2)^2 * height




8 0
3 years ago
Read 2 more answers
Each vertex in a graph of n bertices can be the origin of at most ____edges
EastWind [94]

Answer:

B. n-1

Explanation:

If there are n vertices then that vertex can be origin of at most  n-1 edges.Suppose that you have a graph with 8 vertices you can select a vertex from these 8 vertices now you have 7 other vertices.So the vertex you selected can have at most 7 edges or it can be origin of at most 7 edges.So we conclude that the answer is n-1.

6 0
3 years ago
Other questions:
  • The most common solution to supply chain uncertainties is to build inventories or __________ as insurance.
    6·1 answer
  • Compose a program to examine the string "Hello, world!\n", and calculate the total decimal numeric value of all the characters i
    7·1 answer
  • Una bombilla de 40 w de potencia está encendida durante 10 horas. Calcular la energía que ha consumido en julios y en kw·h.
    12·1 answer
  • MS Excel is a powerful spreadsheet program that helps people with complex mathematical calculations. In what ways could you use
    10·1 answer
  • Many languages do not use the characters of U.S. English. Suppose you wanted to be able to
    15·1 answer
  • Recursion occurs when a function (calls itself/ calls another function/ calls a function from the python standard library)
    5·1 answer
  • Drag each label to the correct location on the image.
    7·1 answer
  • Heeeeeeeelp :)<br> thx<br> jfdyiusjmkdsuiho;dmcvrvho;j
    5·2 answers
  • Python Coding:
    15·1 answer
  • The variable points stores data that the user has input. what is the data type of this variable?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!