Question

In: Computer Science

Need hep with this c++ program Declare an array of integers of size 8 and initialize...

Need hep with this c++ program

Declare an array of integers of size 8 and initialize it with any non-duplicate integer values you like. Don’t enter the values in any order. Print the unsorted array. Sort the array using selection sort or insertion sort in descending order. Print what sort you are using. Write any additional functions that you need for sorting. Keep monitoring the number of comparisons and number of swaps performed while sorting. Report both after sorting. Print the sorted array.

Solutions

Expert Solution

#include<iostream>

using namespace std;

int main()

{

    int arr[8];

    int i;

   

    cout<<"Enter 8 numbers : ";

   

    // traverse the array

    for( i = 0 ; i < 8 ; i++ )

        // get user input

        cin>>arr[i];

       

    cout<<"Array : ";

   

    // display array elements

    for( i = 0 ; i < 8 ; i++ )

        cout<<arr[i]<<" ";

       

    int j, min, x;

    int swaps = 0;

    int comparisons = 0;

   

    // traverse the array

    for (i = 0; i < 7; i++)

    {

        // store the index of the smallest element

        min = i;

       

        // traverse the array from the element after i till the end

        for (j = i + 1; j < 8; j++)

        {

            comparisons++;

           

            // if the current element is smaller than the min element

            if (arr[j] > arr[min])

                min = j;

        }

        // Swap first element and min

        x = arr[min];

        arr[min] = arr[i];

        arr[i] = x;

       

        swaps++;

    }

   

    cout<<"\nSorted Array : ";

   

    // display array elements

    for( i = 0 ; i < 8 ; i++ )

        cout<<arr[i]<<" ";

       

    cout<<"\nNo of swaps : "<<swaps<<endl;

    cout<<"No of comparisons : "<<comparisons<<endl;

   

    return 0;

}


Sample Output


Related Solutions

IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The program then calls the foo function. The function will modify the array x of size n by doubling any values in x that are less than the threshold. The function will count how many values were changed and how many were not changed via two reference parameters. The function signature is: void foo(int n, int x[], int threshold, int *pChanged, int *pUnchanged); The function...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
C++ Write a program to declare an array of double of size 25, ask the user...
C++ Write a program to declare an array of double of size 25, ask the user to input all array elements from the keyboard, then write a function named out_of_order that will test this array for the condition a[0] >= a[1] >= a[2] >= ... > a[24] The function returns a -1 if the elements are not out of order, otherwise it returns the index of the first element that is out of order. Explain what you do to avoid...
Declare an integer array of size 20 and assign the array with 20 randomly generated integers...
Declare an integer array of size 20 and assign the array with 20 randomly generated integers in the range [1, 100]. Shuffle the data in the array so that all numbers smaller than 50 are put before the numbers greater or equal to 50. Note you are not allowed to create any other arrays in the program. Finally, print out the shuffled array. (25 points) #include<iostream> using namespace std; const int NUM = 20; int main() {             //...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
Declare and initialize an array to store the course name or code.
Declare and initialize an array to store the course name or code.
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
In C++ Declare an integer array of size 20 and assign the array with 20 randomly...
In C++ Declare an integer array of size 20 and assign the array with 20 randomly generated integers in the range [1, 100]. Shuffle the data in the array so that all numbers smaller than 50 are put before the numbers greater or equal to 50. Note you are not allowed to create any other arrays in the program. Finally, print out the shuffled array. (25 points) #include<iostream> using namespace std; const int NUM = 20; int main() {      ...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
In C++, create a 3-by-3 array that is capable of storing integers and initialize each entry...
In C++, create a 3-by-3 array that is capable of storing integers and initialize each entry to 0. Ask the user to supply a row and a column and a value, then put the value into the array at that row and column. Print the array to the screen as a table with 3 rows and 3 columns, and ask the user for another row, column, and value. Repeat until user inputs values for all entries. Once finished, compute and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT