Answer to Question #173305 in C++ for Vigneshwaran. B

Question #173305

Write overloaded function named Sort that takes array of integers, float numbers and

characters. Get the user input and display it in the sorted order.


1
Expert's answer
2021-03-19T06:34:47-0400
#include<iostream>
#include<string>
using namespace std;


void sort(int integers[]);
void sort(float floats[]);
void sort(char characters[]);
//The start point of the program
int main (){
	//input data
	int integers[]={5,6,2,4,8};
	float floats[]={2.6,6.9,1.3,6.5,9.5};
	char characters[]={'c','a','z','k','d'};
	//sort integers
	sort(integers);
	//sort floats
	sort(floats);
	//sort characters
	sort(characters);
	//display integer values
	cout<<"Sorted integer values:\n";
	for(int i = 0; i<5; i++) {
		cout<<integers[i]<<" ";
	}
	//display floats values
	cout<<"\nSorted float values:\n";
	for(int i = 0; i<5; i++) {
		cout<<floats[i]<<" ";
	}
	//display character values
	cout<<"\nSorted character values:\n";
	for(int i = 0; i<5; i++) {
		cout<<characters[i]<<" ";
	}
	cout<<"\n\n\n";


	system("pause");
	return 0;
}


//function named "sort" that takes array of integers. 
void sort(int integers[]){
	//using a bubble sort
	for(int i = 0; i<5; i++) {
		for(int j = i+1; j<5; j++)
		{
			if(integers[j] < integers[i]) {
				int temp = integers[i];
				integers[i] = integers[j];
				integers[j] = temp;
			}
		}
	}
}
//overloaded function named "sort" that takes array of float numbers.
void sort(float floats[]){
	//using a bubble sort
	for(int i = 0; i<5; i++) {
		for(int j = i+1; j<5; j++)
		{
			if(floats[j] < floats[i]) {
				float temp = floats[i];
				floats[i] = floats[j];
				floats[j] = temp;
			}
		}
	}
}
//overloaded function named "sort" that takes array of float characters. 
void sort(char characters[]){
	//using a bubble sort
	for(int i = 0; i<5; i++) {
		for(int j = i+1; j<5; j++)
		{
			if(characters[j] < characters[i]) {
				char temp = characters[i];
				characters[i] = characters[j];
				characters[j] = temp;
			}
		}
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS