Bubble Sort Crashing
Apr 6, 2013 at 5:27pm UTC
This project is for my intro programming course. After entering the numbers it crashes. I would assume the nested for loops or the bubble sort portions are causing this. I just need a 2nd pair of eyes to tell me what I'm doing wrong.
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
// Pseudocode PLD Chapter 8 #1 pg. 350
// Start
// Declarations
// num SIZE = 10
// num NUMBERS[10]
// num i
// num j
// num temp
// for i = 0 to SIZE - 1
// output "Please enter a number: "
// input NUMBERS[i]
// endfor
// for i = 0 to SIZE - 2
// for j = 0 to SIZE - 2
// if (NUMBERS[j] > NUMBERS[j+1])
// temp = NUMBERS[j]
// NUMBERS[j] = NUMBERS[j+1]
// NUMBERS[j+1] = temp
// endif
// endfor
// endfor
// output "Sorted List"
// output "==========="
// for i = 0 to SIZE - 1
// output "Number ", i + 1, ": ", NUMBERS[i]
// endfor
// Stop
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
#include <iostream>
#include <list>
using namespace std;
int main()
{
int SIZE = 10;
int NUMBERS[10];
int i;
int j;
int temp;
for (i = 0; i <= SIZE-1; i++)
{
cout << "Please enter a number: " ;
cin >> NUMBERS[i];
}
for (i = 0; i <= SIZE-2; i++)
{
for (j = 0; i <= SIZE-2; j++)
{
if (NUMBERS[j] > NUMBERS[j+1])
{
temp = NUMBERS[j];
NUMBERS[j] = NUMBERS[j+1];
NUMBERS[j+1] = temp;
}
}
}
cout << "Sorted List" << endl;
cout << "===========" << endl;
for (i = 0; i <= SIZE-1; i++)
{
cout << "Number " << i + 1 << ": " << NUMBERS[i] << endl;
}
system ("PAUSE" );
return 0;
}
Apr 6, 2013 at 5:59pm UTC
change this for (j = 0; i <= SIZE-2; j++)
to this
for (j = 0; j <= SIZE-2; j++)
Apr 6, 2013 at 5:59pm UTC
for (j = 0; i <= SIZE-2; j++)
Spot the error!
Apr 6, 2013 at 6:12pm UTC
HA!
I knew it was something ridiculously easy and stupid on my part. TYVM!
Apr 8, 2013 at 1:40pm UTC
@Giggidy
Your loop should be like this>
for (j = 0; j <= SIZE-2-i; j++)
Topic archived. No new replies allowed.