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
vivado [14]
3 years ago
12

Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input

begins with an integer indicating the number of integers that follow.Ex: If the input is 5 10 5 3 21 2, the output is:2 3 You can assume that the list of integers will have at least 2 values.To achieve the above, first read the integers into a vector.Hint: Make sure to initialize the second smallest and smallest integers properly.
Computers and Technology
1 answer:
kompoz [17]3 years ago
8 0

Answer:

Following are the program in the C++ programming Language.

//header files

#include <iostream>

#include <vector>

using namespace std;//name space

// define main method

int main()

{

//set integer variables

int size, num;

//get input the size of the list

cout<<"Enter size of list: ";

cin >> size;

//set integer type vector variable

vector<int> vecs;

//Set the for loop

for (int index = 0; index < size; ++index)

{//get elements of the list from the user

cout<<" :";

cin >> num;

//push back the elements of the list

vecs.push_back(num);

}

//storing the first two elements in variable

int n = vecs[0], n1 = vecs[1], current , temp;

//set if conditional statement

if (n > n1)

{

//perform swapping

temp = n;

n = n1;

n1 = temp;

}

//Set for loop

for (int index = 2; index < size; ++index)

{

//store the value of the vector in the variable

current = vecs[index];

//set if conditional statement

if (current < n)

{

//interchange the elements of the variable

n1 = n;

n = current;

}

else if(current < n1)

{

n1 = current;

}

}

//print the value of first two smallest number.

cout <<"\n" <<n << " " << n1 << endl;

return 0;

}

<u>Output:</u>

Enter size of list: 5

:10

:5

:3

:21

:2

2 3

Explanation:

Here, we define the required header files and namespace then, we define "main()" function inside the main function.

  • Set two integer data type variable "size", "num".
  • Print message and get input from the user in variable  "size".
  • Then, we set integer vector type variable "vecs".
  • Set the for loop to get the number of input in the variable "num" from user.
  • Then, we push back the elements of the list and store first two elements of the list in the variable "n", "n1".
  • Set the conditional statement to perform swapping.
  • Define the for loop to store the list of the vector in the integer type variable "current".
  • Finally, we print the value of the two smallest numbers of the list.
You might be interested in
Why is it grey? i recently dropped it , the switch is on!
iragen [17]

Answer:

because it is what it is

7 0
3 years ago
To ensure that the original value of an argument passed to a method remains unchanged after returning from the method, you shoul
lakkis [162]

Answer:

false

Explanation:

A reference parameter passed into the method can be changed by the method, when the method changes all of a parameter's variables, the parameter will keep the changes after the method is returned.

4 0
3 years ago
Agriscience in the United States?
Mrac [35]

Answer:

The history of agriculture in the United States covers the period from the first English settlers to the present day. In Colonial America, agriculture was the primary livelihood for 90% of the population, and most towns were shipping points for the export of agricultural products. Most farms were geared toward subsistence production for family use. The rapid growth of population and the expansion of the frontier opened up large numbers of new farms, and clearing the land was a major preoccupation of farmers. After 1800, cotton became the chief crop in southern plantations, and the chief American export. After 1840, industrialization and urbanization opened up lucrative domestic markets. The number of farms grew from 1.4 million in 1850, to 4.0 million in 1880, and 6.4 million in 1910; then started to fall, dropping to 5.6 million in 1950 and 2.2 million in 2008

Explanation:

8 0
3 years ago
Technician A uses a steel gasket scraper on aluminum engine parts. Technician B uses a plastic or wooden scraper on the same par
timama [110]

Technician B is correct. A steel scrapper could damage or gouge the aluminum.

5 0
3 years ago
What is the purpose of using variables in programming?
rosijanka [135]

Answer:

3rd one I believe. Depends what your programming Its to print for Java. Such as system.out.println:(x);

5 0
3 years ago
Read 2 more answers
Other questions:
  • Why has unicode become the standard way of converting binary to text
    7·1 answer
  • Need help answering this
    7·1 answer
  • After class, Anita and Bev make plans to study for their psychology exam together but cannot decide on a time or location. In ad
    14·1 answer
  • Your friend called and told you that he saw information about the classified XYZ program on the Internet. As a cleared employee
    14·1 answer
  • What i have to care about transmitting data packet from one controller to other controller?
    8·1 answer
  • What is the approximate area of the figure? A. 129 square inches B. 113 square inches C. 110 square inches D. 44 square inches
    15·1 answer
  • The part of the eye thats similar to the film of a camera is the
    7·1 answer
  • Which tab do you select to change how you see your Word document on screen?
    9·2 answers
  • Calculator and clocks are examples of -------------- in windows 7
    14·1 answer
  • Running away from home
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!