Answer to Question #3312 in C++ for mukunda

Question #3312
Write a program to arrange the given numbers in ascending order using sort function & display them using display function.
1
Expert's answer
2011-07-07T10:17:03-0400
#include <iostream>
using namespace std;

void sort (int numbers[], int size)
{
for (int i = 0; i < size-1; ++i) {
& for (int j = i+1; j < size; ++j) {
& if (numbers[i] > numbers[j]) {
& int temp = numbers[i];
& numbers[i] = numbers[j];
& numbers[j] = temp;
& }
& }
}
}

void display (int numbers[], int size)
{
for (int i = 0; i < size; ++i) {
& cout << numbers[i] << " ";
}
cout << endl;
}

int main()
{
int numbers[] = { 10, 20, 5, 12, -8, 17 };
int size = sizeof(numbers) / sizeof(int);

display (numbers, size);
sort (numbers, size);
display (numbers, size);

return 0;
}

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
APPROVED BY CLIENTS