# Answer to Question #3010 in C++ for livy

Question #3010

How to write a program that accepts 6 pairs of values from the user and then calculates and stores the difference of each pair of values in an array.The array of calculated values should then be sorted into ascending order and printed on screen?

Expert's answer

#include <iostream>

using namespace std;

const int ARR_SIZE = 6;

void Sort (float array[]) // Function for bubble sort of the array

{

bool sorted;& // Flag for indicating while the array is sorted

float temp;& // Temp variable for storig values

do

{

& sorted = true;

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

& {

if (array[i+1] < array[i])

{

sorted = false;

temp = array[i];

array[i] = array[i+1];

array[i+1] = temp;

}

& }

}

while (!sorted);

}

void Display (float array[])& // Function for displaing array of values

{

system("cls");

cout<< "Elements of the array in ascending order: "<< endl;

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

{

& cout<< array[i]<< endl;

}

}

int main ()

{

float x, y;& // Variables for storing pairs of values

float array[ARR_SIZE]; // Arrays for storing differences between values

cout<< " **Enter values separating it with spaces\n";

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

{

& cout<< "Enter "<< i+1<< " pair of numbers\n > ";

& cin >> x >> y; // Getting values from user (values must be split by space)

& array[i] = x - y; // Calculating of difference

}

Sort (array); // Sorting calculated values

Display (array); // Displaing array

system ("pause");

return 0;

}

using namespace std;

const int ARR_SIZE = 6;

void Sort (float array[]) // Function for bubble sort of the array

{

bool sorted;& // Flag for indicating while the array is sorted

float temp;& // Temp variable for storig values

do

{

& sorted = true;

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

& {

if (array[i+1] < array[i])

{

sorted = false;

temp = array[i];

array[i] = array[i+1];

array[i+1] = temp;

}

& }

}

while (!sorted);

}

void Display (float array[])& // Function for displaing array of values

{

system("cls");

cout<< "Elements of the array in ascending order: "<< endl;

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

{

& cout<< array[i]<< endl;

}

}

int main ()

{

float x, y;& // Variables for storing pairs of values

float array[ARR_SIZE]; // Arrays for storing differences between values

cout<< " **Enter values separating it with spaces\n";

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

{

& cout<< "Enter "<< i+1<< " pair of numbers\n > ";

& cin >> x >> y; // Getting values from user (values must be split by space)

& array[i] = x - y; // Calculating of difference

}

Sort (array); // Sorting calculated values

Display (array); // Displaing array

system ("pause");

return 0;

}

## Comments

## Leave a comment