Ok I am new to C++ and I am having a hard time trying to figure out how to Sort the array I have created. I dont know if I am on the wrong track or if I am on the right track. Someone PLEASE help!! I have to sort some integers that are given by the user using a sort method like bubble sort. Please HELP!!!
Thanks for your help in advance. Below is what I have so far.
#include
#include
using namespace std;
int main()
{
int number1;
cout << "Please enter number1: ";
cin >> number1;
int number2;
cout << "Please enter number2: ";
cin >> number2;
int number3;
cout << "Please enter number3: ";
cin >> number3;
int number4;
cout << "Please enter number4: ";
cin >> number4;
cout << number1 << endl;
cout << number2 << endl;
cout << number3 << endl;
cout << number4 << endl;
system( "PAUSE");
return 0;
}
Life will be easier if you create an array rather than hard-code number1, number2, number3, number4. You'll want to create a boolean flag variable to keep track of whether there have been any swaps in a Bubble Sort pass. Within an outer do-while loop, you'll want a for-loop that compares neighboring array values and swaps them if necessary.
Repeat the do-while loop until you go through the entire for-loop without making any swaps. At that time all numbers will be in order and you'll be done, so exit the do-while loop. So your do-while condition will be checking that boolean flag variable to see if there have been any swaps. If there have, repeat the do-while loop. If not, don't.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Thanks for the advice. I had a feeling I was making this harder then it should be. I have a habit of that :). The problem I have with what you suggested is that I dont know how to make an array that will allow user input. Do you know of any good links or tutorials that I can look at that will help me?
Thanks Again
Not much to it. Declaring the array and reading numbers into the array are separate problems. Assuming you haven't learned dynamic arrays yet, you can make an array bigger than it possibly needs to be:
const int MAX_SIZE = 1000;
int numbers[MAX_SIZE];
int numElements;
cout << "Enter number of elements (2 - 1000): ";
cin >> numElements;
for (int i = 0; i < numElements; i++)
{
cout << "Enter number " << i << " : ";
cin >> numbers[i];
}
// numbers array is filled with data. Now sort it.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Not quite. Your outer loop is going down and the inner loop is going down, both loops should go the same way. Your inner loop would be a good outer loop to control which element of the array do you start with on any given sequence of the inner loop if n is 1 less than the number of elements in the loop.
You also have a typo, it's a[n - 1] instead of a{n - 1], and most of the implementations I've seen compar a[x] to a[x + 1] rather than a[x - 1].
The sequence to swap two elements would be:
temp = a[x + 1]; //store one element in a third variable of the same type
a[x + 1] = a[x]; //assign the second element to the first element
a[x] = temp; //assign the value of the third variable to the second element
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
for(i = 0; i < arrayLength - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
for (j = i; j < arrayLength; j++) //start at i
{
if (array[j + 1] > array[j]) // ascending order simply changes to <
{
temp = array[j]; // swap elements
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
Ok I have combined that with the code you posted first just to see what it would do and I get a lot of undeclared identifiers. Now to declare them would just simply be the :
int arrayLenght;
int j;
etc... Right? Here is the whole code.
#include
#include
using namespace std;
int main()
{
const int MAX_SIZE = 10;
int numbers[MAX_SIZE];
int numElements;
cout << "Enter number of elements (2 - 10): ";
cin >> numElements;
for (int i = 0; i < numElements; i++)
{
cout << "Enter number " << i << " : ";
cin >> numbers[i];
}
//Sort
for(i = 0; i < arrayLength - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
for (j = i; j < arrayLength; j++) //start at i
{
if (array[j+1] > array[j]) // ascending order simply changes to <
{
temp = array[j]; // swap elements
array[j] = array[j+1];
array[j+1] = temp;
}
}
}for(i = 0; i < arrayLength - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
for (j = i; j < arrayLength; j++) //start at i
{
if (array[j+1] > array[j]) // ascending order simply changes to <
{
temp = array[j]; // swap elements
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
Lerner and I used different variable names in our examples so you can't just combine them. You have to pick variable names and stick with them. You can't declare variable names of numElements and numbers, then use variable names of arrayLength and array and expect the compiler to realize what they refer to, so pick one set of names (say numElements and numbers) and change the other set of names (arrayLength and array) to those names. Or vice versa.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Ok I changed that so now all the elements are the same. But there is still the undeclared i and J's do I just declare those or do I need to change them also?
Here is the code:
#include
#include
using namespace std;
int main()
{
const int MAX_SIZE = 10;
int numbers[MAX_SIZE];
int numElements;
cout << "Enter number of elements (2 - 10): ";
cin >> numElements;
for (int i = 0; i < numElements; i++)
{
cout << "Enter number " << i << " : ";
cin >> numbers[i];
}
//Sort
for(i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
for (j = i; j < numElements; j++) //start at i
{
if (array[j+1] > numbers[j]) // ascending order simply changes to <
{
temp = numbers[j]; // swap elements
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}for(i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
for (j = i; j < numElements; j++) //start at i
{
if (numbers[j+1] > numbers[j]) // ascending order simply changes to <
{
temp = numbers[j]; // swap elements
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
Code tags make things easier to read:
[code=cplusplus]
// paste code here
[/code]
Every variable you use needs to be declared. The name change had to do with the fact that you had two variables with different names which were referring to the same thing. The compiler had no idea of that. i and j are counters and in your case don't have that problem. Just be careful not to confuse i and j. Just declare them as integers. There is nothing to be renamed. You need to change arrayLength in your comment too because no one reading will know that that means numElements. They'll probably be able to figure it out by the similar name, but you should change it to the real variable name.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Part of learning how to code is to try to take what you've already learned and try to use it in a new situation.
It looks like you've recopied the sorting code instead of trying to output the values of array, at least you have two copies of the sorting algorithm and nothing that would output the results of the sorting.
To output the sorted array elements to the screen just use a loop to access every valid element of the array and write code to output each element of array one at a time each time through the loop from within the body of the loop. It's actually a lot shorter to write the code than it is to tell you how to do it!
EDIT: and learn how to use code tags soon if you're going to continue to post code to the board.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
WOW its amazing what happens when someone smart knows :). Ok I got that working but the problem I have now is that the sorted list is not showing on the screen. Here is the code:
#include
#include
using namespace std;
int main()
{
const int MAX_SIZE = 10;
int numbers[MAX_SIZE];
int numElements;
int i;
int j;
int temp;
cout << "Enter number of elements (2 - 10): ";
cin >> numElements;
for (int i = 0; i < numElements; i++)
{
cout << "Enter number " << i << " : ";
cin >> numbers[i];
}
//Sort
for(i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
for (j = i; j < numElements; j++) //start at i
{
if (numbers[j+1] < numbers[j]) // ascending order simply changes to <
{
temp = numbers[j]; // swap elements
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}for(i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is numElements - 1
{
for (j = i; j < numElements; j++) //start at i
{
if (numbers[j+1] < numbers[j]) // ascending order simply changes to <
{
temp = numbers[j]; // swap numbers
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
system("Pause");
return 1;
}
You need to format your code so it's readable and use code tags (see how above). The reason it's not displaying is because you aren't telling it to display anywhere. There are no cout statements after everything is sorted. Plus it seems like you are sorting twice with two back to back nested for loops.
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
const int MAX_SIZE = 10;
int numbers[MAX_SIZE];
int numElements;
int i;
int j;
int temp;
cout << "Enter number of elements (2 - 10): ";
cin >> numElements;
for (int i = 0; i < numElements; i++)
{
cout << "Enter number " << i << " : ";
cin >> numbers[i];
}
//Sort
for (i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is arrayLength - 1
{
for (j = i; j < numElements; j++) //start at i
{
if (numbers[j + 1] < numbers[j]) // ascending order simply changes to <
{
temp = numbers[j]; // swap elements
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
for (i = 0; i < numElements - 1; i++) //start at 0 and stop if i == last valid index of array, which is numElements - 1
{
for (j = i; j < numElements; j++) //start at i
{
if (numbers[j + 1] < numbers[j]) // ascending order simply changes to <
{
temp = numbers[j]; // swap numbers
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
system("Pause");
return 1;
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Tremendous Saga of Bubble Sorting!
Thank you,
all members of this Instant/FastFood C++ society!
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348