Question

in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...

1. Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps.

0 1 2 3 4 5

|------|------|------|------|------|-

10 3 6 8 20 7

0 1 2 3 4 5

|------|------|------|------|------|-

0 1 2 3 4 5

|------|------|------|------|------|-

0 1 2 3 4 5

|------|------|------|------|------|-

0 1 2 3 4 5

|------|------|------|------|------|-

0 1 2 3 4 5

|------|------|------|------|------|-

12 points

QUESTION 24

#include<iostream>

using namespace std;

void shownumbers(int, int);

int main()

{

int x, y;

cout << "Enter first number : ";

cin >> x;

cout << "Enter second number : ";

cin >> y;

shownumbers(x, y);

return 0;

}

void shownumbers(int a, int b)

{

bool flag;

for (int i = a + 1; i <= b; i++)

{

flag = false;

for (int j = 2; j<i; j++)

{

if (i%j == 0)

{

flag = true;

break;

}

}

if (flag == false && i>1)

cout << i << endl;

}

}

1. What does the function shownumbers implement?

2. If the inputs for x and y are 3 and 25 respectively, what are the outputs if running the program?

0 1 2 3 4 5
|------|------|------|------|------|-
10 3 6 8 20 7
Swapping index position is: 0
Smallest element is: 3   Smallest index position: 1
Swaps the data between 0th and samllest index position.

0 1 2 3 4 5
|------|------|------|------|------|-
3 10 6 8 20 7
Swapping index position is: 1
Smallest element is: 6   Smallest index position: 2
Swaps the data between 1st and samllest index position.

0 1 2 3 4 5
|------|------|------|------|------|-
3 6 10 8 20 7
Swapping index position is: 2
Smallest element is: 7   Smallest index position: 5
Swaps the data between 2nd and samllest index position.

0 1 2 3 4 5
|------|------|------|------|------|-
3 6 7 8 20 10
Swapping index position is: 3
Smallest element is: 8   Smallest index position: 3
No swapping required.

0 1 2 3 4 5
|------|------|------|------|------|-
3 6 7 8 20 10
Swapping index position is: 4
Smallest element is: 10   Smallest index position: 5
Swaps the data between 4th and samllest index position.

0 1 2 3 4 5
|------|------|------|------|------|-
3 6 7 8 10 20

The function will display all the prime numbers between two given numbers x and y.

5
7
11
13
17
19
23

Explanation:

for (int i = a + 1; i <= b; i++)

Loops from 4 to 25.

flag = false; Flag is set to false for each iteration.

for (int j = 2; j<i; j++)

The above loop will iterate from 2 to i (now i is 4)

if (i%j == 0)

i.e., 4 % 2 is 0

So, sets the flag to true and comes out of the inner loop for j.

if (flag == false && i>1)

Checks if flag is false and i value is greater than 1

flag is false results false. So it will not display the i value.

Now in outer loop i value is 5.

flag is again false.

Inner loop will iterate 2 to 4

i%j = 5 % 2 = 0 is false. So increase the j value by one becomes 3.

i % j = 5 % 3 = 0 false. So increase the j value by one becomes 4.

now the condition is false j<i.

flag == false results true and i > 1 is true

So, it will display 5.

Earn Coins

Coins can be redeemed for fabulous gifts.