Hi everyone,
I have a program where I need to print the values of an array of float values. Then I must sort the array and print the sorted values and THEN print the original values again.
I am able to get the originals and sorted values printed but I am having trouble printing out the originals again after they are sorted. Instead I am getting the sorted values being printed out twice instead of having the originals being printed, then the sorted ones and then the original values again. The trick is the original values must be sorted BEFORE the original values are printed for the second time and the sorted version of the array has to be stored in an array of pointers to float which I think is what I have below and have it's own function.
I am not allowed to modify the original array. How can I go about doing this so that the first the original values are printed, then the sorted values and then the original values are printed again?
BTW, don't worry about the poor alignment of the output. I'll deal with that later. Right now, what is supposed to be the second printing of the originals is printing out as the sorted values which shouldn't be.
Here is my code so far:
If anyone can help me figure out how I can get the originals printed to the screen AFTER the sorted values, that would be greatly appreciated. Thanks.Code:
#include <iostream>
#include <conio.h>
#include <stdlib.h>
int main() {
void sort(float *, int);
int j;
const int num = 10;
float x[num] = {2.3, 5.66, 1.22, 5, 6.777, 3.2, 5.13, 2.862, 1, 5.01};
cout << "Original" << endl;
for (j = 0; j < num; j++) {
cout << x[j] << endl;
}
cout << endl;
sort(x, num);
cout << "Originals" << endl;
for (j = 0; j < num; j++) {
cout << x[j] << endl;
}
getche();
return 0;
}
void sort(float *ptr, int n) {
int j, k;
float temp;
for (j = 0; j < n - 1; j++) {
for (k = j + 1; k < n; k++) {
if (ptr[j] > ptr[k]) {
temp = ptr[j];
ptr[j] = ptr[k];
ptr[k] = temp;
}
}
}
cout << "Sorted" << endl;
for (j = 0; j < n; j++) {
cout << ptr[j] << endl;
}
cout << endl;
}