Question

In: Computer Science

Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...

Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using SELECTION SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_SelectionSort.cpp" and submit your .cpp file. NOTE: This assignment needs only one .cpp file

Solutions

Expert Solution

/* C++ program to do selection sort*/                                                           

/* include necessary header files */
#include <iostream>

using namespace std; 

void selectionSort(int arr[], int n){ 
        /* 
                function for performing selection sort
                @Param: An array and an integer n which is size of array     
                Return:     
        */
        
        /* declare variables */
        int i, j, min, t; 

        /* for each in element in the array move the element towards bondary */
        for (i = 0; i < n-1; i++){ 
                /* Find the minimum element */
                min = i; 
                for(j = i+1; j < n; j++){
                        if (arr[j] < arr[min]) 
                                min = j; 
                }
                /* swap the elements */
                t = arr[min];
                arr[min] = arr[i];
                arr[i] = t;
        } 
} 

/* Function to print an array */
void display(int arr[], int n){ 
        /* 
                function to display array
                @Param: An array and an integer n which is size of array 
                Return:     
        */
        /* display */
        for (int i=0; i < n; i++) 
                cout << arr[i] << " "; 
        cout << endl; 
} 

/* Driver code */
int main() { 
        /* declare variable(s) */
        int n = 0;
        
        /* keep asking until no of elements >= 5 */ 
        while(n < 5){
                cout<<"Enter no of elements (at least 5): ";
                cin>>n;
        }       
        
        /* declare an array of size  n */
        int arr[n] = {0}; 
        
        /* take input of numbers */
        cout<<"Enter the numbers: ";
        for(int i = 0; i < n; i++)
                cin>> arr[i];
        
        /* display the array */
        cout << "\nInput array: "; 
        display(arr, n); 
        cout<<endl;
        
        /* calll function to do selection sort */
        selectionSort(arr, n); 
        
        /* display the sorted array */
        cout << "\nSorted array: "; 
        display(arr, n); 
        
        return 0; 
} 

___________________________________________________________________


___________________________________________________________________

Enter no of elements (at least 5): 2
Enter no of elements (at least 5): 4
Enter no of elements (at least 5): 6
Enter the numbers: 10 7 8 4 20 5

Input array: 10 7 8 4 20 5


Sorted array: 4 5 7 8 10 20

___________________________________________________________________


Note: If you have queries or confusion regarding this question, please leave a comment. I would be happy to help you. If you find it to be useful, please upvote.


Related Solutions

Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using BUBBLE SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_BubbleSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Program specifics: Write a C++ program that does the following: a. Asks the user for the...
Program specifics: Write a C++ program that does the following: a. Asks the user for the distance to the pin and the depth of the green (both in yards). (Note: The pin is the hole in the green and the depth is the diameter of the green, assuming it is circular.) b. Asks the user for an integer club number from 2 to 10, where 10 is the pitching wedge (this club lifts the ball out of rough, sand, etc)....
2. Write a program C++ that asks the user for a number (not necessary to force...
2. Write a program C++ that asks the user for a number (not necessary to force any particular requirements). Write a function with the following signature: double square(double x) that returns the square of the user's number (x * x). 3. Write a C++ program that asks the user for an integer. Write a function that returns 1 of the number is even, and 0 if the number is odd. Use this function signature: int isEven(int x). 4. Write a...
Implement a program in C++ that does the following: Ask the user and read at least...
Implement a program in C++ that does the following: Ask the user and read at least 10 numbers as input from the keyboard and stores them in a an array Displays the array of numbers on the screen (as is, before sorting) Sorts those numbers in the array using Selection Sort Algorithm Displays the array of numbers on the screen (AFTER sorting)
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football...
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football League list of current players. It's going to be a big list(over 1000 names). Please identify, in comments, which part of the code is for the Selection Sort and which part of the code is for Insertion Sort. It must have both. Inputting data from a text file named "Players.txt" Only want the name of the player(first name then last name), their team name,...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
Must implement a simple program that asks the user for a width and a height, storing...
Must implement a simple program that asks the user for a width and a height, storing them as double values. Afterwards, you compute area and perimeter as if the shape were a rectangle. Output the results. Then, you compute the area and perimeter as if the shape were a right triangle and you have been given the base and height. Area and Perimeter Calculator Enter width: 3 Enter height: 5 If your shape is a rectangle, its area is 15.0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT