Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions

Question

2. What type of team would you recommend?

Answered: 1 week ago

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago