Output array gets repeated twice, with loops
Nov 3, 2017 at 7:58pm UTC
I am supposed to do a void that sorts the user's input array ascendingly/descendingly according to the input (true/false).
It works fine on our homework checking system (I have no idea what they're using to test our code though) except for the fact that my output array is always repeating twice,
Ex: supposedly output 1 2 3 4, my output 1 2 3 4 1 2 3 4.
Would really appreciate if anyone can elaborate on what's wrong with my code!
Apologize for my English skill.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
void sort(float array[], int size, bool isAscending)
{
float temp = 0;
if (isAscending == true )
{
for (int i = 0; i < size; i++)
{
for (int j = i+1; j < size; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for (int i = 0; i < size; i++)
{
cout << fixed << setprecision(2) << array[i] << " " ;
}
}
else if (isAscending == false )
{
for (int i = 0; i < size; i++)
{
for (int j = i+1; j < size; j++)
{
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for (int i = 0; i < size; i++)
{
cout << fixed << setprecision(2) << array[i] << " " ;
}
}
}
Last edited on Nov 3, 2017 at 7:59pm UTC
Nov 4, 2017 at 7:52am UTC
I works fine to me:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include <iomanip>
#include <iostream>
#include <limits>
void sort(float array[], int size, bool isAscending);
void waitForEnter();
int main()
{
float myarr[] { 5.0, 8.0, 7.0, 6.0, 8.0 };
std::cout << "Sorted ascending: " ;
sort(myarr, 5, true );
std::cout << "\nSorted descending: " ;
sort(myarr, 5, false );
std::cout << '\n' ;
waitForEnter();
return 0;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n" ;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
}
void sort(float array[], int size, bool isAscending)
{
float temp = 0;
if (isAscending == true )
{
for (int i = 0; i < size; i++)
{
for (int j = i+1; j < size; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for (int i = 0; i < size; i++)
{
std::cout << std::fixed << std::setprecision(2) << array[i] << " " ;
}
}
else if (isAscending == false )
{
for (int i = 0; i < size; i++)
{
for (int j = i+1; j < size; j++)
{
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for (int i = 0; i < size; i++)
{
std::cout << std::fixed << std::setprecision(2) << array[i] << " " ;
}
}
}
Output:
Sorted ascending: 5.00 6.00 7.00 8.00 8.00
Sorted descending: 8.00 8.00 7.00 6.00 5.00
Press ENTER to continue...
Anyway, I'd improve indentation, prefer "double" instead of "float" and add spaces to std::cout this way
' '
instead of
" "
.
Nov 4, 2017 at 1:55pm UTC
@Enoizat thank you for the review! Turned out it was my school's system testcase that was at fault and not mine though, but I really appreciate your help!
Topic archived. No new replies allowed.