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...
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.
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...
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
c++ In this program ask the user what name they prefer for their file, and make...
c++ In this program ask the user what name they prefer for their file, and make a file, the file name should za file. Get a number from the user and save it into the create file. should be more than 20 number in any order. print out all 20 numbers in a sorted array by using for loop, last print out its total and average. At last create a function in which you will call it from main and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT