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. You can assume that the list will have at least 2 integers and fewer than 20 integers. Ex: If the input is: 5 10 5 3 21 2

Answers

Answer 1

Answer:

Follows are the code to the given question:

#include <iostream>//header file  

using namespace std;

int main() //main method

{

int nums[20];//defining an array

int n,i,k;//defining integer variables

cout<<"Enter total number you want to insert: ";//print message

cin>>n;//input value of n  

cout << "Enter array numbers: " << endl;//print message

for (i = 0;i<n;i++) //defining for loop for input values from user-end

{

   cin >> nums[i];//input values

}

for (i = 0; i < n;i++) //defining for loop for count array values

{

   for (k =i+1;k<n;k++)//defining for loop for arrange value in ascending  order  

   {

       if (nums[i] > nums[k])//checking first and second value

       {

           int t = nums[i];//defining integer variable that hold first element value in t

           nums[i] = nums[k];//holding second element value in first element

           nums[k] = t;//assign value in t

       }

   }

}

cout<<"Two smallest number in list are:";//print message

 for (i = 0; i <2; ++i)//defining for loop that prints first two smallest value  

     cout<<nums[i]<<" ";//print value

   return 0;

}

Output:

Enter total number you want to insert: 6

Enter array numbers:  

5

10

5

3

21

2

Two smallest number in list are:2 3  

Explanation:

In this code, an integer array "nums" is defined, and in the next step multiple integer variable is defined, that uses the for loop input value from the user-end, and in the next step, another two for loop is declared, that uses if block to arrange value into the ascending order at which it stores two smallest value in first and second position in the array element, and in the next step, it uses another for loop to print its element value.


Related Questions

Please read this lab exercise thoroughly, before attempting to write the program.

Write a program that reads the pairs of group number and student count from the text file (user must enter name of file) and:

Displays the number of groups in the file (5 %)
Displays the number of even and odd student count (5%)
Displays the sum of the even student count and the sum of the odd student count as well as the sum of all the student count (5%)
Displays the group numbers of the largest student count and the smallest student count (5 %)
Computes the average of the largest and smallest student count (10 %)
Compute the sum of ALL the digits in the largest student count (i.e. 952 – Sum is 9+5+2 = 16) (15 %)
Assume the contents of the file are as follows but that the pairs of data can change later:

1 25

2 123

3 475

4 61

5 77

6 910

7 234

8 138

9 134

10 95

11 674

12 345

13 31

14 211

15 952

16 873

17 22

18 7

19 876

20 347

21 450

The following is a sample output: User input in red

What is name of the input file? integers.dat

The number of groups in the file is 21

There are 12 odd student count and 9 even student count

The sum of the odd student count is 2645

The sum of the even student count is 4390

The sum of all the student count is 7035

The group number of the largest student count is 15

The group number of the smallest student count is 18

The average of 952 and 7 is 479.5

The sum of all the digits in the largest student count is 16

NOTE:

Use two decimal point precision for the average
To check your code, adjust one of the student count in the file, recompile your code and run again (suggest changing the largest or smallest student count )
Even though there are 21 pairs of numbers in the file, the program should NOT presume there will always be 21 pairs of numbers. In other words, you should not solve this problem by declaring 21 pairs of values. If I choose to use another integers.dat file other than the one provided with the assignment, your program should still work correctly with that file as well. All you can assume is that a series of integers will be read in from the file. Therefore, you need to implement a solution that uses the repetition control structures (for, while, do…while) in the reading and processing of these values to produce your output.

Answers

Answer:

filename = input("Enter input file? ")

with open(filename, 'r') as data:

num_groups = []

num_counts = []

for line in data:

 cols = line.split()

 num_groups.append(int(cols[0]))

 num_counts.append(int(cols[1]))

print("The number of num_groups in the file is "+str(len(num_groups)))

c_even = 0; c_odd = 0; s_odd = 0; s_even = 0

for i in num_counts:

if i%2 == 0:

 c_even=c_even+1

 s_even = s_even + i

else:

 c_odd=c_odd+1

 s_odd = s_odd + i

print("There are "+str(c_odd)+" odd student and "+str(c_even)+" even student")

print("The sum of odd student is "+str(s_odd))

print("The sum of even student is "+str(s_even))

print("The sum of all student is "+str(s_even+s_odd))

max_count = num_groups[num_counts.index(max(num_counts))]

print("The group number of the largest student is "+str(max_count))

min_count = num_groups[num_counts.index(min(num_counts))]

print("The group number of the smallest student is "+str(min_count))

print("The average of "+str(max(num_counts))+" and "+str(min(num_counts))+" is:",end=" ")

print("%.2f" % ((max(num_counts)+min(num_counts))*0.5))  

bigkount = str(max(num_counts))

bigsum = 0

for i in range(len(bigkount)):

bigsum += int(bigkount[i])

 

print("The sum of all the digits in the largest student is: "+str(bigsum))

Explanation:

The program was written in Python.

Because of the length, I added an attachment where I used comments to explain the lines of the code

Other Questions
Please answer ASAP i need this immediately If two parents do not have an autosomal recessive trait, what can you say about their children? Can autosomal recessive traits skip generations? How did Lincoln's loss tum Into a victory? He lost the first election for president but won on the second attempt. O He lost the state position but won the national seat. He lost the local election but won the state position. He lost the debates but won the state election. . Porqu el valor del coeficiente de la variable al cuadrado de una funcin cuadratica no puede ser cero? Sales for a business were 3 million dollars more the second year than the first, and sales for the third year were double the sales for the second year. If sales for the third year were 38 million dollars, what were sales, in millions of dollars, for the first year?1617.520.52235 Aspen has 10 pairs of white socks and 1 pair of red socks. What is the chance that Aspen wears a pair of black socks? Find the 18th term of the arithmetic sequence whose common difference is d=5 and whose first term is a = 4. Why do you think it was hard for Melody to tell rose about her test score Please solve needed as soon as possible thank you I really appreciate it What is the relationship between atoms and mass? Roger jarred 15 liters of jam after 2 days. How much jam did Roger jar if he spent 6 days making jam? What is a difference between the Wounded Warrior Project and the VA? 1. The Wounded Warrior Project is a nonprofit organization; the VA is run by the government.2. The Wounded Warrior Project provides mental health care; the VA provides only physical health care. 3. The Wounded Warrior Project has problems with doctor shortages; the VA has trained plenty of doctors. 4. The Wounded Warrior Project is run by Democrats; the VA is run by Republicans. Which method of heat transfer is energy that is teansferred by electromagnetic waves? PLEASE I NEED THE ANSWER NOW 11 POINTS FOR CORRECT ANSWER BRAINLIEST TOO Direction: List down nine measuring tools on you answer sheet. 1. ____________2.____________3.____________4.____________5.____________6.____________7.____________8.____________9.____________10.____________ Booker T. Washington argued for African Americans to strive for more civil rights by Demonstrating hard work and pursing higher education I think that Walton's main purpose in the novel is to let us see how bad the monster feels after Frankenstein dies.A. It is an effective thesis statement.B. It is not specific.C. It is not clear and concise.D. It is not interpretive. Please help please please A tank for storing liquid as a base of area 1.5 meters square. What is the pressure on the base of the tank when it contains water weighting 4500n what tip should you consider when vacationing somewhere with a culture different than yours when have you use their tips in your own photography practice if you have not traveled outside of your home area describe a place you'd like to go for Photograph and how these tips might apply there