Question #3312

Write a program to arrange the given numbers in ascending order using sort function & display them using display function.

Expert's answer

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

}

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;

}

## Comments

## Leave a comment