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 =...
1) Create a flowchart for the program below and Trace the program below with input of...
1) Create a flowchart for the program below and Trace the program below with input of 3 and then again with input of 5. #include <iostream> using namespace std; int Fall(int x, int m) { int Xm = 1, i=x; while(i>=x-m+1) { Xm = Xm*i; i=i-1; } return Xm; } int Delta(int x, int m) { int ans = 0; ans = Fall(x+1,m); ans = ans - Fall(x,m); return ans; } void main() { int x = 0, m =...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Code here } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to manipulate the elements...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
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...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik //...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik // // This class specifies the functions to solve the n-queens // puzzle. //*************************************************************** class nQueensPuzzle { public: nQueensPuzzle(int queens = 8);     //constructor     //Postcondition: noOfSolutions = 0; noOfQueens = queens;     // queensInRow is a pointer to the array     // that store the n-tuple.     // If no value is specified for the parameter queens,     // the default value, which is 8, is assigned to it. bool...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
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 ";   ...