Question

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

in C++ Please and thanks

  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

  1. Read the following problem, and answer questions. (12 points)

    #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?

Homework Answers

Answer #1

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

Answer 1:

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

Answer 2:

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #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 =...
First, understand the Selection-sort algorithm below: Selection-sort(A: Array [1..n] of numbers) 1       for i=n down to...
First, understand the Selection-sort algorithm below: Selection-sort(A: Array [1..n] of numbers) 1       for i=n down to 2 2                position=i 3                for j=1 to (i–1) 4                          if A[j]>A[position] then position=j 5                if position ≠ i then 6                          temp=A[i] 7                          A[i]=A[position] 8                          A[position]=temp (4 points) If input A=[12,5,11,6,10,7,9,8], what will A be after the 3rd iteration of the outermost for loop of the Selection-sort algorithm completes? A=[__, __, __, __, __, __, __, __] (8 points) Modify the algorithm to solve the...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
This is my code and can you please tell me why it's not working? By the...
This is my code and can you please tell me why it's not working? By the way, it will work if I reduce 10,000,000 to 1,000,000. #include <iostream> using namespace std; void radixSort(int*a, int n) { int intBitSize = sizeof(int)<<3; int radix = 256; int mask = radix-1; int maskBitLength = 8;    int *result = new int[n](); int *buckets = new int[radix](); int *startIndex = new int[radix]();    int flag = 0; int key = 0; bool hasNeg =...
[Lab Task HTML/JAVASCRIPT] Please read 10 numbers that are list below, sort the numbers and then...
[Lab Task HTML/JAVASCRIPT] Please read 10 numbers that are list below, sort the numbers and then print those numbers. 10 numbers = { 9, 3, 2, 1, 10, 30, 4, 6, 7, 8} Output should have a sorted list [Reference JavaScript code] <html> <body>     <H1>prompt()</h1>     <p id="pro"></p>     <script>        // Array creation        var num= new Array();               var ix = 0;        // Read 10 numbers        for (ix = 0; ix < 10; ix++)...
Selection-Sort( A[1..n] ) 1 2 3 4 5 6 7 8 9 10 // INPUT: A[1..n],...
Selection-Sort( A[1..n] ) 1 2 3 4 5 6 7 8 9 10 // INPUT: A[1..n], an array of any n numbers in unknown order integer i, j, m fori=1ton−1 do swap A[i] ↔ A[m] // OUTPUT: A[1..n], its numbers now sorted in non-decreasing order m=i for j = i to n do if A[j] < A[m] then m = j Give a proof that this algorithm sorts its input as claimed.
How do I make this code not include negative numbers in the total or average if...
How do I make this code not include negative numbers in the total or average if they are entered? (C++) For example, if you enter 4, 2, and -2, the average should not factor in the -2 and would be equal to 3. I want the code to factor in positive numbers only. ----- #include using namespace std; int main() {    int x, total = 0, count = 0;       cout << "Type in first value ";   ...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT