Question

"you will write a program in C++ which will read a list of up to 100...

"you will write a program in C++ which will read a list of up to 100 integers from the user, and print them back to the screen in sorted order. The user can indicate that they are done entering numbers by entering any negative value. Your program will store the entered numbers into an array. It will then sort the array, using the selection sort algorithm. After each iteration of the sorting algorithm, it will print the current state of the array. Selection Sort Sorting is a very common and popular problem in computer science. There are numerous algorithms for sorting an array of values. Each algorithm has pros and cons, but ultimately, the most distinguishing factors deal with simplicity, memory usage, and efficiency. The algorithm that you will implement is called selection sort. In this algorithm, you will scan the numbers in your array and look for the the index of the element that has the minimum value. When found, you will swap the value at the first index in the array with the min value. After one iteration, you will have the smallest value in the first location of the array. In the next iteration, you will begin your minimum search from the second second element in the array. You will find the new minimum and swap with this location. You will continue this process of finding the next minimum values and putting them in order. Slowly, your array will become sorted. After completing this process count - 1 times, where count is the number of items needing to be sorted, your array will be sorted. An illustration of the algorithm can be found on Wikipedia. The following demonstrates what your array will look like after each iteration: Begin: 4 12 2 90 0 24 15 12 Iteration 1: 0 12 2 90 4 24 15 12 Iteration 2: 0 2 12 90 4 24 15 12 Iteration 3: 0 2 4 90 12 24 15 12 Iteration 4: 0 2 4 12 90 24 15 12 Iteration 5: 0 2 4 12 12 24 15 90 Iteration 6: 0 2 4 12 12 15 24 90 Iteration 7: 0 2 4 12 12 15 24 90 Example Session $ ./sorter Please enter up to 100 different numbers. Indicate that you are done by entering a negative value. => 4 => 12 => 2 => 90 => 0 => 24 => 15 => 12 => -1 Begin: 4 12 2 90 0 24 15 12 Iteration: 0 12 2 90 4 24 15 12 Iteration: 0 2 12 90 4 24 15 12 Iteration: 0 2 4 90 12 24 15 12 Iteration: 0 2 4 12 90 24 15 12 Iteration: 0 2 4 12 12 24 15 90 Iteration: 0 2 4 12 12 15 24 90 Iteration: 0 2 4 12 12 15 24 90 End: 0 2 4 12 12 15 24 90 $ $ ./sorter Please enter up to 100 different numbers. Indicate that you are done by entering a negative value. => -1 Begin: End: $ The Code In order to get you started, I am providing you with your main function along with all of the function declarations that are necessary to complete this lab successfully. #include #include using namespace std; // Reads in integer values from the user up to max_size times or a negative // value. The values are stored into the numbers array. // The function will return the number of positive integers read in. int read_numbers(int numbers[], int max_size); // Prints the title which is set to a width of 10 for formatting purposes. // It will then print the contents of the array numbers up to count values. void print_numbers(string title, const int numbers[], int count); // Sorts the numbers array using the selection sort algorithm void sort(int numbers[], int size); // Swaps two integer variables void swap(int &a, int &b); // Returns the INDEX of the element that contains the minimum value in the // array numbers. The search will begin from start_index and will proceed // up to element (size - 1) int min_index(const int numbers[], int size, int start_index); const int MAX_NUMBERS = 100; int main() { int numbers[MAX_NUMBERS]; int count = read_numbers(numbers, MAX_NUMBERS); print_numbers("Begin:", numbers, count); sort (numbers, count); print_numbers("End:", numbers, count); return 0; } "

How would I code, so that the iterations for each swap is shown up until the complete project for C++ 2.0?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is a simple C++ program for selection sort without using functions

#include<iostream>
using namespace std;
int main()
{
    int i,j,l,n,input,m,loc,temp,min,a[100];
    cout<<"Enter the number of elements:";
    cin>>n;
    cout<<"\nEnter the elements\n";

    for(i=0;i<n;i++)
    {
        cin>>input;
        if(input < 0){
        break;}
    else{
        a[i] = input;
    }
  
    }
    l=i-1;

    for(i=0;i<l+1;i++)
    {
        min=a[i];
        loc=i;
        for(j=i;j<l+1;j++)
        {
            if(min>a[j])
            {
                min=a[j];
                loc=j;
            }
        }

        temp=a[i];
        a[i]=a[loc];
        a[loc]=temp;
      
        cout<< " Iteration "<<i<<":";
      
        for (m=0;m<l+1;m++)
        {
        cout<<a[m]<<" ";
        }
    }

    cout<<"\n Final Sorted list is as follows\n";
    for(i=0;i<l+1;i++)
    {
        cout<<a[i]<<" ";
    }

    return 0;
}

output

Enter the number of elements : 5

Enter the elements

6

0

5

9

-1

iteration 0 : 0 6 5 9

iteration 1 :0 5 6 9

iteration 2 :0 5 6 9

iteration 3 : 0 5 6 9

Final sorted list is as follows

0 5 6 9

Add a comment
Know the answer?
Add Answer to:
"you will write a program in C++ which will read a list of up to 100...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PROJECT TASKS 1. Read the problem definition below and write a C++ program that implements your...

    PROJECT TASKS 1. Read the problem definition below and write a C++ program that implements your solution. Readability of the program will be graded based on variable naming, indentation, commenting (not too little and not too much), spacing, consistency, and styling in general including choice of functions. [NOTE: the code at the end of this document does not completely meet requirements as it uses many single letter variables and has no internal documentation. You need to improve it.] 2. Compile...

  • 1- Write a MIPS assembly program to initialize register Ss0 to 8, register Ssl to 12, regis- ter ...

    1- Write a MIPS assembly program to initialize register Ss0 to 8, register Ssl to 12, regis- ter Ss2 to 5, and register Ss3 to 2. Then, store register Ss0 in the first element of an array, register Ssl in the second element of the array, register Ss2 in the third element of the ar- ray, and register Ss3 in the fourth element of the array. Then, sort the elements of the ar- ray using the selection sort algorithm, and...

  • How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the lin...

    How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't. Here's my code. #include <iostream> using namespace std; void selectionSort(int[], int, int& ); void showSelection(int[], int); void sortArray(int[], int, int&); void showArray(const int[], int); int main() {    int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24...

  • 4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up t...

    4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • ***Please give comments and show it running*** Create a MIPS program that gets a set of...

    ***Please give comments and show it running*** Create a MIPS program that gets a set of numbers from the user, sorts them using selection sort, and displays the sorted numbers on the screen. You should create a subprogram to do the selection sort, sending it the length and address of the array. This is the pseudo code for the structure the selection: for (int i = 0; i < aLength-1; i++) {     /* find the min element in the...

  • Using c++ Write a program that reads in a list of up to 25 first names...

    Using c++ Write a program that reads in a list of up to 25 first names from an input file nameData.txt and allows the user to display the name data, sort it in ascending or descending order, count the number of occurrences of a given name, or exit the program. The input file consists of a single names per line with each line terminated with an end of line (i.e. using Enter key to end the line). Your program should...

  • The program defined in insertionSort.cpp gets a list of numbers as input from the user and...

    The program defined in insertionSort.cpp gets a list of numbers as input from the user and calls the insertion sort algorithm to sort them. It also displays the list before and after the sorting. The insertion sort algorithm should be defined in the InsertionSort.h file, but this definition has been omitted and it is your task to provide it. The appropriate insertion sort function is to be defined in the InsertionSort.h file. The InsertionSort.h file performs an include of Swap.h,...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT