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

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...
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 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...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to call these functions instead of the inline code in your main(). In addition, add error checking for the user input. If the user inputs an incorrect value prompt the user to re-enter the number. The flow of the code should look something like: /** Calculate the mean of a vector of floating point numbers **/ float mean(vector& values) /** Calculate the standard deviation from...
Define Loan Class – Add to your project. And write program in main method to test...
Define Loan Class – Add to your project. And write program in main method to test it. Note: Assume year is number of years, rate is the annual interest rate and P is principle is loan amount, then the total payment is Total payment = P *(1+ rate/12)^ year*12; Monthly Payment = TotalPayment/(year*12); java
Write an IPO diagram and Python program that has two functions, main and determine_grade. main –...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main – Should accept input of five numeric grades from the user USING A LOOP.   It should then calculate the average numeric grade.    The numeric average should be passed to the determine_grade function. determine_grade – should display the letter grade to the user based on the numeric average:        Greater than 90: A 80-89:                 B 70-79:                 C 60-69:              D Below 60:           F Modularity:...
*****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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT