Question

In: Computer Science

In C++ prototype functions above "main" and define them below "main"; Write a program that uses...

In C++

prototype functions above "main" and define them below "main";

Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep 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 arrays. It should also keep count of the number of exchanges it makes. Display these values on the screen.

  1. No global variables
  2. No labels or go-to statements
  3. No infinite loops, examples include:
    • for(;;)
    • while(1)
    • while(true)
    • do{//code}while(1);
  4. No break statements to exit loops

Solutions

Expert Solution

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

#include <iostream>

using namespace std;

int BubbleSort(int values[], int numValues){

int i, j;

int temp;

int swaps=0;

// outer loop to travel through the all elements

for (i = 0; i < numValues - 1; i++) {

// inner loop to compare the outer loop elements

for (j = 0; j < numValues - i - 1; j++)

// if element at j< than j+1 than swap both

if (values[j] > values[j + 1]) {

// swap logic

temp = values[j];

values[j] = values[j+1];

values[j+1] = temp;

swaps++;

}

}

return swaps;

}

int SelectionSort(int values[], int numValues)

{

int swaps=0;

// One by one move boundary of unsorted subarray

for (int i = 0; i < numValues-1; i++)

{

// Find the minimum element in unsorted array

int min_idx = i;

for (int j = i+1; j < numValues; j++)

if (values[j] < values[min_idx])

min_idx = j;

// Swap the found minimum element with the first

// element

swaps++;

int temp = values[min_idx];

values[min_idx] = values[i];

values[i] = temp;

}

return swaps;

}
void printArray(int arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";

}
cout<<endl;
}
int main() {

int arr1[]={20,10,15,14,29,28,30,39,32,45,98,78,56,91,54,12,45,65,76,90};

int arr2[]={20,10,15,14,29,28,30,39,32,45,98,78,56,91,54,12,45,65,76,90};
cout<<"Original array 1: ";
printArray(arr1,20);

int s1=BubbleSort(arr1, 20);
cout<<"After bubble sort array 1: ";
printArray(arr1,20);
cout<<"Swap count using BubbleSort : "<<s1<<endl;

cout<<"Original array 2: ";
printArray(arr2,20);

int s2=SelectionSort(arr2, 20);
cout<<"After Selection sort array 2: ";
printArray(arr2,20);

cout<<"Swap count using SelectionSort : "<<s2<<endl;

}

Kindly revert for any queries

Thanks.


Related Solutions

In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep 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 arrays. It should also keep count...
Write a program that uses the defined structure and all the above functions. Suppose that the...
Write a program that uses the defined structure and all the above functions. Suppose that the class has 20 students. Use an array of 20 components of type studentType. Other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the...
Assume the following functions have already been defined, write a main() using c++ that uses them...
Assume the following functions have already been defined, write a main() using c++ that uses them to fill a vector with random integers, remove the smallest and largest integers from the vector, save the result in a file called "trimmed.txt" void fillRandom(vector & nums, int howMany); // fill with specified number of integers int minValue(vector nums); // return smallest value in vector int maxValue(vector <;int> nums); // return largest value in vector void writeFile(string outFileName, vector nums ); // writes...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
Write a C program with call to functions to produce the output given below. // the...
Write a C program with call to functions to produce the output given below. // the requirements are that there should be 5 files; intList.h, intList.c, hw3.h, hw3.c, and main.c. please only C and use Linked List. thank you. For the 5 different files, he wants it this way: 1) main.c This file just consists of the main() function, which only consists of the displayClassInfo() function call, and the runMenuHw3() function call. 2) intList.h This file would have the IntNode...
Write a program in c++, with at least four functions, including main, which must do the...
Write a program in c++, with at least four functions, including main, which must do the following: Ask user whether they want to encode or decode a message – if no, then terminate Take the input string from the user, store it in dynamic memory (use new) As appropriate, encode or decode the message using Rot13. Output the encoded/decoded message Delete the input string from dynamic memory (use delete) Input will be a string of no more than 25 characters....
Write a small C++ program with 4 functions (and main(): getNumbers()- what is the return, what...
Write a small C++ program with 4 functions (and main(): getNumbers()- what is the return, what are the parameters? findMax()- what is the return, what are the parameters? findMin()-what is the return, what are the parameters? find()- should return the index of the element or a -1 indicating not found The main function will call those methods and print the results of each. 1 // declare necessary variables 2 // declare array 3 double numbers[SIZE]; 4 // Function prototypes 5...
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
In the main(), below, write a program that uses a switch-statement to choose between three numbers....
In the main(), below, write a program that uses a switch-statement to choose between three numbers. The number, choice, should be prompted for and each choice should print out a unique statement. Also, the prompt should continue indefinitely (i.e. keep prompting until the user force-quits the program), and there should be a general catch-all if the user enters other than the three possible choices. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes here> return 0;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT