Question

In: Computer Science

In C++, write a program that uses two identical arrays of ten randomly ordered integers. It...

In C++, write a program that uses two identical arrays of ten randomly ordered integers. It should display the contents of the first array, then call a function to sort it using the most efficient descending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using descending order selection sort, modified to print out the array contents after each pass of the sort.

Solutions

Expert Solution

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
const int SIZE = 10;
void print(int arr[]) // display array
{ cout<<"{ ";
   for (int i = 0; i < SIZE; i++)
   {
       cout << arr[i] << " ";
   }
   cout<<" }";
   cout << endl;
}
void bubbleSort(int arr[]) //bubble sorting
{
   int temp;
   bool swap;
   do
   {   swap = false;

       for (int i = 0; i < (SIZE - 1); i++)
       {
           if (arr[i] < arr[i + 1]) // compare element with every next and check if current is lesser update it
           {
               temp = arr[i]; //swap them
               arr[i] = arr[i + 1];
               arr[i + 1] = temp;
               swap = true;
           }
           print(arr);
       }
   } while (swap);
}
void selectionSort(int arr[])
{
   int curr;
   int large;
   for (int i = 0; i < (SIZE - 1); i++)
   {
       curr = i;
       large = arr[i];
       for (int j = i + 1; j < SIZE; j++)
       {
           if (arr[j] > large) //compare if current is lesser than next swap them
           {
               large = arr[j];
               curr = j;
           }
       }
       arr[curr] = arr[i];
       arr[i] = large;
       print(arr);
   }
}

int main()
{
srand(time(NULL)); //setting random number
   int Array1[SIZE];
   int Array2[SIZE];
   for(int i=0;i<SIZE;i++){
Array1[i]=(rand()%100+1); //set random number from 1 to 100
Array2[i]=(rand()%100+1); // set random number from 1 to 100
   }
   cout<<":::::::::: 1st Array Elements ::::::::::"<<endl;
   print(Array1);
   cout<<":::::::::: ARRAY 1 data after every Bubble Sort ::::::::::"<<endl;
   bubbleSort(Array1);
cout<<":::::::::: 2nd Array Elements ::::::::::"<<endl;
   print(Array2);
   cout<<":::::::::: SELECTION SORT IN DESCENDING ORDER ::::::::::"<<endl;
   selectionSort(Array2);
   return 0;
}

OUTPUT:

IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP


Related Solutions

using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers....
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using an ascending order selection sort, modified to print out the...
Write a program that uses two identical arrays of at least 25 integers. It should call...
Write a program that uses two identical arrays of at least 25 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in descending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it makes. Display these values on...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Write a program in C which randomly generates two integers X and Y between 10 and...
Write a program in C which randomly generates two integers X and Y between 10 and 50, and then creates a dynamic array which can hold as many numbers as there are between those two random numbers. Fill the array with numbers between X and Y and print it on screen. Do not forget to free the allocated memory locations whenever they are no longer needed. Example: Randomly generated numbers are: 43 23 Expected Output : 23 24 25 26...
Write a C++ program to do the following USING ARRAYS 1) input 15 integers (input validation...
Write a C++ program to do the following USING ARRAYS 1) input 15 integers (input validation , all numbers must be between 0 and 100) 2) find the largest number 3) Find the Smallest Number 4) Find the Sum of all numbers in the Array Display: as per the user's section (menu using switch or if else ) Largest Number Smallest Number Sum of all numbers the original Array DO NOT USE METHODS OR FUNCTIONS Submit: Source code (C++) output...
C++ Write a function called gen_dates() that generates random dates. It takes two arrays of integers...
C++ Write a function called gen_dates() that generates random dates. It takes two arrays of integers called months and days to store the month and day of each date generated, a constant array of 12 integers called num_of_days that specify the number of days of each of the 12 months and an integer called size that specifies how many dates to generate and randomly generates size dates, storing the generated months in months array and generated days in days array....
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT