Answer to Question #173073 in C++ for Vimal D

Question #173073

Write a C++ Program to sort array of integers. Get the user input and display it in the sorted order.


1
Expert's answer
2021-03-18T13:40:10-0400
#include<iostream>
#include<string>
using namespace std;


void sortIntegers(int values[],int n);


//The start point of the program
int main (){
	//array of integers
	int values[1000];
	int n;
	//Get the user input and display it in the sorted order.
	cout<<"Enter the number of values: ";
	cin>>n;
	for(int i = 0; i<n; i++) {
		cout<<"Enter the value ["<<(i+1)<<"]: ";
		cin>>values[i];
	}
	//sort integers
	sortIntegers(values,n);
	//display integer values
	cout<<"Integer values in the sorted order:\n";
	for(int i = 0; i<n; i++) {
		cout<<values[i]<<" ";
	}
	
	cout<<"\n\n";




	system("pause");
	return 0;
}




//This function "sortIntegers" takes array of integers and sorts integers values using a bubble sort. 
void sortIntegers(int values[],int n){
	for(int i = 0; i<n; i++) {
		for(int j = i+1; j<n; j++)
		{
			if(values[j] < values[i]) {
				int temp = values[i];
				values[i] = values[j];
				values[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