Hi,
Have you used a debugger? Hopefully there is GUI one in your IDE. You should be able to step through your code 1 line at a time and keep an eye on the values of your variables, thereby deduce where it all goes wrong.
There is poor person's debugging (
ppd), but it is a pain - try to use a proper debugger.
ppd consists of putting
std::cout
statements in your code to see what the values are. Hint there is a problem with the values of
i
and
n
There is a very powerful debugging tool called rubber duck debugging. Purchase a small rubber duck, and place it near your computer. When you have a problem, explain out loud to the duck how your code works. This leads to solving the problem. I am not taking the piss - this really works!! :+D
I think you are going to need more than one for loop to sort this info.
Use constant variables instead of magic numbers like 9 throughout the code
1 2 3 4 5 6 7 8 9 10 11
|
const unsigned int SIZE = 10;
// hard code the values, until you have it working, then get it to accept input
int arr1[SIZE] = {7, 98, 6, 43, 2, 22, 94, 3, 8, 4};
// canonical for loop, use < with SIZE, not <=
// I prefer not to use i and j for variable names, they look too similar - this can cause problems
for (int pos = 0; pos < SIZE; pos++) {
if (arr1[pos] < low) { // this is always true, what is the purpose of it?
// need to compare something else
}
|
Lines 16 to 18 is a swap function, so investigate making it an actual function.
Try to come up with better variable names - call them by a name for what they actually are.