Question

In: Computer Science

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:

  1. Ask the user and read at least 10 numbers as input from the keyboard and stores them in a an array
  2. Displays the array of numbers on the screen (as is, before sorting)
  3. Sorts those numbers in the array using Selection Sort Algorithm
  4. Displays the array of numbers on the screen (AFTER sorting)

Solutions

Expert Solution

#include<iostream>

using namespace std;

//for maintaining array size
#define SIZE 10

void selection_sort(int arr[]){

        int min_val_idx, temp; 
  
    for(int i = 0; i < SIZE-1; i++){

                min_val_idx = i;                                        //for maintaining index of minimum element

                for(int j = i+1; j < SIZE; j++) 
                        if(arr[j] < arr[min_val_idx]) 
                                min_val_idx = j; 
        
                //swaping min element with current left most element
                temp = arr[min_val_idx];
                arr[min_val_idx] = arr[i];
                arr[i] = temp;
    } 
}

int main(){

        //declare array
        int arr[SIZE];

        cout<<"Please enter 10 numbers\n";

        //input from terminal
        for(int i = 0;i<SIZE;i++)
                cin>>arr[i];

        cout<<"Array before sorting\n";

        //print unsorted array
        for(int i = 0;i<SIZE;i++)
                cout<<arr[i]<<" ";
        cout<<"\n";

        //calling selection sort function
        selection_sort(arr);

        cout<<"Array after sorting\n";

        //print sorted array
        for(int i = 0;i<SIZE;i++)
                cout<<arr[i]<<" ";
        cout<<"\n";       

        return 0;
}

Sample Ouput:

To compile: g++ <filename>

To run: ./a.out

Code is well commented. If you have any query or doubts then let me know in the commnets. Thanks!


Related Solutions

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...
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...
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
In C: Write a complete program that performs the following task: Ask the user for the...
In C: Write a complete program that performs the following task: Ask the user for the number of sequences to display. For each sequence, Ask the user for a starting value Print out the value and double it (multiply by 2). Continue printing and doubling (all on the same line, separated by one space each) as long as the current number is less than 1000, or until 8 numbers have been printed on the line. You may assume that the...
Design and implement a C++ program that performs the following steps:Ask the user to enter a...
Design and implement a C++ program that performs the following steps:Ask the user to enter a positive integer number N; Your program may need to prompt the user to enter many times until it reads in a positive number;Let user to enter N (obtained in the previous step) floating point numbers, and count how many positive ones there are in the sequence and sum up these positive numbers; (Hint: negative numbers or 0 are ignored).Display result.You can and should use...
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
A, B:   Design and Implement a C# windows form application to ask the user for 10...
A, B:   Design and Implement a C# windows form application to ask the user for 10 integer numbers, sort them in ascending order and display the sorted list. Use bubble sort technique to sort the array elements and do not use any built-in sort method to sort the array elements.                                                        [02] C:    Test and evaluate your program by inputting variety of values.
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)....
Using Python write a program that does the following in order: 1. Ask user to enter...
Using Python write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out...
Create a C++ program that will ask the user for how many test scores will be...
Create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. (no fstream) The data needs to include the student’s first name, student number test score the fields should be displayed with a total width of 15. The prompt should be printed with a header in the file explaining what each is: ex. First Name student number Test Score 1) mike 6456464   98 2) phill...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT