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?
1
Expert's answer
2011-06-10T07:47:46-0400
#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;
}

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