Question

In: Computer Science

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 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

Please find the code below::::

#include <iostream>
using namespace std;

int bubbleSort(int bubble_el[20],int n)

{

   int i, j;
   int count = 0;

   for (i = 0; i < n - 1; i++)

   {

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

       {

           if (bubble_el[j] > bubble_el[j + 1])

           {
               count++;

               int t = bubble_el[j];

               bubble_el[j] = bubble_el[j + 1];

               bubble_el[j + 1] = t;

           }

       }

   }
   return count;
}

int selectionSort(int sel_el[20],int n)

{
   int count = 0;

   int i, j;

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

   {

       for (j = i; j < n; j++)

       {

           if (sel_el[i] > sel_el[j])

           {
               count++;

               int temp = sel_el[i];

               sel_el[i] = sel_el[j];

               sel_el[j] = temp;

           }

       }

   }
   return count;

}


int main()

{
   srand(time(NULL));
   int size =20;
   int rand_val;
   int bubble_el[size];
   int sel_el[size];
   for (int j = 0; j < size; j++)
   {
       rand_val = rand() % 100;
       bubble_el[j] = rand_val;
       sel_el[j] = rand_val;

   }
   int count1=bubbleSort(bubble_el,size);
   int count2=selectionSort(sel_el,size);
   cout<<"Number of exchange in bubble sort : "<<count1<<endl;
   cout<<"Number of exchange in selection sort : "<<count2<<endl;
   return 0;

}

output:


Related Solutions

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...
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...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
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...
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...
Write push and pop functions on C/C++. Have a program having 10 random integers(hardcoded)and put them...
Write push and pop functions on C/C++. Have a program having 10 random integers(hardcoded)and put them on your stack. Also, print them. Read a character from the keyboard. If the character is an “o” then pop from the stack but don’t print. If the character is a “p”, then pop from the stack and print that number. If the character is an “e” or the stack is empty, end the program.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT