Question
RE WRITE THIS CODE FROM C TO C++: #include #include void swap(int array[], int i, int j){ int temp = array[i]; array[i] = array[j]; array[j]
RE WRITE THIS CODE FROM C TO C++:
#include
void swap(int array[], int i, int j){
int temp = array[i]; array[i] = array[j]; array[j] = temp; }
void bubble_sort(int array[], int n){
int i, j = 0,swapped = 1;
while (swapped) { swapped = 0; for (i = 0; i < n - 1 - j; i++) { if (array[i] > array[i + 1]) { swap(array, i, i + 1); swapped = 1; } } ++j; } } void bubble_sort_descending(int array[], int n){
int i, j = 0,swapped = 1;
while (swapped) { swapped = 0; for (i = 0; i < n - 1 - j; i++) { if (array[i] < array[i + 1]) { swap(array, i, i + 1); swapped = 1; } } ++j; } }
void print_array(int array[], int n){
int i; for (i = 0; i < n; i++) printf("%d%c", array[i], (i == n - 1) ? ' ' : ' '); }
int max_value(int array[], int n){
int i, max=array[0];
for(i=0;i if(max < array[i]){ max=array[i]; } } return max; } int min_value(int array[], int n){ int i, min=array[0]; for(i=0;i if(min > array[i]){ min=array[i]; } } return min; } float find_average(int array[],int n){ int i; float average=0; for(i=0;i average = (float) average/n; return average; } void find_key(int array[],int n, int key){ int i,flag=0; for(i=0;i if(flag==0){ printf("The key: %d entered is not in the array ",key); } } int main() { int i=1,j=0,m=0,s=0,x=1, key; printf("How many numbers would you like to enter "); scanf("%d",&i); int *array = NULL; array = malloc(i * sizeof(int)); for(j=0;j while(x){ printf(" Enter 1 if you want sort in ascending order "); printf("Enter 2 if you want to sort in descending order "); printf("Enter 3 if you want the max value of the values entered "); printf("Enter 4 if you want the min value of the values entered "); printf("Enter 5 if you want to find the average of the numbers entered "); printf("Enter 6 if you want to find the location of a key "); printf("Enter 7 if you want to exit the program "); printf(" "); scanf("%d",&s); printf(" "); switch(s) { case 1: bubble_sort(array,i); print_array(array,i); break; case 2: bubble_sort_descending(array,i); print_array(array,i); break; case 3: printf("The max value is: %d ",max_value(array,i)); break; case 4: printf("The min value is: %d ",min_value(array,i)); break; case 5: printf("The averages of the values entered is : %f ",find_average(array,i)); break; case 6: printf("Please enter the key that you want to find in the array "); scanf("%d",&key); find_key(array,i,key); break; case 7: x=0; break; } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started