answersLogoWhite

0

#include<iostream>

#include<time.h>

#include<iomanip>

#include<string>

void swap(int& x, int& y)

{

x^=y^=x^=y;

}

void bubble_sort(int* A, int size)

{

while(size)

{

int n=0;

for(int i=1; i<size; ++i)

{

if(A[i-1]>A[i])

{

swap(A[i-1], A[i]);

n=i;

}

}

size=n;

}

}

void insertion_sort(int* A, int size)

{

for(int i=1; i<size; ++i)

{

int value=A[i];

int hole=i;

while( hole && value<A[hole-1] )

{

A[hole]=A[hole-1];

--hole;

}

A[hole]=value;

}

}

void selection_sort(int* A, int size)

{

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

{

int j=i;

for(int k=i+1; k<size; ++k)

if(A[k]<A[j])

j=k;

if( i!=j )

swap(A[i],A[j]);

}

}

void sort(int* A, int size, int sort_type)

{

switch(sort_type)

{

case(0): bubble_sort( A, size );

case(1): insertion_sort( A, size );

case(2): selection_sort( A, size );

}

}

int* copy_array(int* A, int size)

{

int* copy=new int[size];

memcpy(copy, A, size*sizeof(int));

return(copy);

}

void print_array(int* A, int size, char* prompt)

{

std::cout<<prompt<<"\t";

for(int i=0; i<size; ++i)

std::cout<<std::setw(2)<<A[i]<<" ";

std::cout<<std::endl;

}

int get_rand(int range_min=0, int range_max=RAND_MAX)

{

return((int) ((double)rand() / (RAND_MAX + 1) * ((range_max + 1) - range_min) + range_min));

}

int input_char(std::string prompt, std::string input)

{

char ch;

do

{

std::cout<<prompt<<": ";

std::cin>>ch;

}

while(input.find(ch)==std::string::npos);

return(input.find(ch)%(input.size()/2));

}

int main()

{

srand((unsigned) time(NULL));

int size = get_rand( 10, 80);

if( int* A = new int[size] )

{

for( int i=0; i<size; ++i )

A[i]=get_rand( 1, size );

int choice=input_char("Please select a sorting method:\n[B]ubble, [I]nsert, [S]election", "bisBIS");

std::cout<<"You chose ";

switch(choice)

{

case(0): std::cout<<"bubble"; break;

case(1): std::cout<<"insertion"; break;

case(2): std::cout<<"selection"; break;

}

std::cout<<" sort...\n"<<std::endl;

print_array( A, size, "Before sorting" );

sort(A, size, choice);

print_array( A, size, "After sorting" );

delete [] A;

}

return(0);

}

User Avatar

Wiki User

11y ago

What else can I help you with?

Continue Learning about Engineering

Menu driven program in c programming?

You can find the source code of the menu driven graphics program here: http://www.start2code.com/Cresources/menu-driven-graphics-program-c.html


What is the meaning of the menu driven program?

Menu-driven simply means you execute the application's commands by selecting them from a menu, as opposed to command-driven whereby you enter (type) the commands manually.


What are the advantage and disadvantage of organizing an event?

There are many advantages of event driven programming. This type of programming is the simplest and flexible forms of programming languages. This means the programmer has more control over the program when the user does something. Also, making an event driven program is easy. There are only a few disadvantages that come with the event driven programming. Firstly, sometimes classes are not often reusable and are hard to carry out in other applications. It is also very complex and hard to master.


How write the algorithm of a menu-driven C program?

To write the algorithm for a menu-driven C program, start by defining a loop that displays the menu options to the user. Use a switch-case structure to handle user input, allowing each case to correspond to a different option. Inside each case, implement the functionality for that option, and include a break statement to exit the case. Finally, provide an exit option to terminate the program cleanly when the user selects it.


What does it mean that VBA is objcet and event driven programming language?

Object driven programming languages are all based around objects on a 'form' or a 'room'. For example the program will be ran around buttons or other controls. For example if a user clicks a button a set of defined steps will occur. Event driven is similar. For example if this occurs then do this. For example while counter = 1 do .... Basically it is more simple for novice programmers to use and get their head around.

Related Questions

Menu driven program in c programming?

You can find the source code of the menu driven graphics program here: http://www.start2code.com/Cresources/menu-driven-graphics-program-c.html


What is the formation of new species by natural selection?

We call this speciation, and is largely driven by evolution and natural selection.


What is the difference between sexual and natural selection?

Natural selection is survival of the fittest, while sexual selection is a preference for a given trait made by the limiting sex. It is strangely, but usually in opposition to natural selection (E.G., male peacock's tail feathers that garner attention from predators and prevent the peacock from fleeing well).


Is operating system an event driven program?

yes


Why is target market selection important for a customer driven marketing strategy?

xzfhgalds'hg[a'pjovbdfohpgjdsaiog


Write c program of menu driven by calculator?

dcdfsadf


What is meant by event driven?

event driven programs do not have a specific pathway in which the programs instructions are executed. different parts of the program are evoked by events that take place during the running of the program.


What has the author Jonathan Bryan Snape written?

Jonathan Bryan Snape has written: 'Physical and biological factors in bubble-driven bioreactors'


Does natural selection deal with only plants?

No, all life evolves. Bacteria evolve, viruses evolve, protists evolve, plants evolve, fungi evolve and animals evolve. Evolution is driven by Natural Selection. So, no. The evolution of all life on Earth is driven by Natural Selection: all bacteria, plants, animals, mammals, fish, insects, biochemical pathways, behaviours et cetera evolve by Natural Selection.


What is the meaning of the menu driven program?

Menu-driven simply means you execute the application's commands by selecting them from a menu, as opposed to command-driven whereby you enter (type) the commands manually.


Are changes in allelic frequency driven by natural selection?

Changes in allelic frequency can be driven by a variety of factors, including natural selection, genetic drift, gene flow, and mutation. Natural selection is a process where certain alleles provide a fitness advantage in a specific environment, leading to their increase in frequency over time. However, the relative contribution of natural selection to changes in allelic frequency can vary depending on the specific circumstances.


Is artificial selection and natural selection the same at the molecular level?

Technically, yes, but only because they are using virtually the same processes, the only real difference between the two, is like the difference between choosing your own mate and having someone choose your mate for you.