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 <stdlib.h>
#include <iostream>




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;
}

Recommended Answers

All 18 Replies

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 <stdlib.h>
#include <iostream>




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;
} 

end quote.

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.

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

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.

Thanks So much. Now I just need the sort function. I appreciate all your help thank you sooooo much!!!

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.

Ok so if I was going to sort the code you posted would this code be correct?

for(top=i; i>1; i--)
{
    i = 1;
    for(i=0;i<n;i++)
    {
        if a[i] < a{i-1])
        {
            i = a[i-1];
            a[i-1] = a[i];
            i++;
        }
        }
    }

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.

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

AHHH I guess Im an Idiot or something. I am just frustrated now and I know how the code is but I dont know how to make it work with my program I have this code. Can you give me some guide on how to make it work with the code you posted?

{
     void bubble_sort(apvector <int> &array)
{
      int i, j, flag = 1;    // set flag to 1 to begin initial pass
      int temp;             // holding variable
      int arrayLength = array.length( ); 
      for(i = 1; (i <= arrayLength) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (arrayLength -1); j++)
         {
               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;
                    flag = 1;               // indicates that a swap occurred.
               }
          }
     }
     return;
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;
    }
  }
}

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 <stdlib.h>
#include <iostream>




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;
    }
  }
}

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 <stdlib.h>
#include <iostream>




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;
    }
  }
} 

end quote.

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.

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 <stdlib.h>
#include <iostream>




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;
    }
  }
}

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 <stdlib.h>
#include <iostream>




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;
    }
  }
} 

end quote.

Code tags make things easier to read:

// paste code here

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.

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 <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;
}

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.

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 <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;
}
//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;
}
#include <stdlib.h>
#include <iostream>

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;
		}
	}
}


**Please wrap your coding......

1. See line 14. Your "i" is declared in the for loop, which means that the int i will only be valid in the for loop. After the for loop exit, it will no longer be valid. If you want to use "i" for several times, I suggest you to declare it as a global variable. Read more on the scope of a declared variable.

2. "Arraylength" is just like the "i" you used just now. To use it, you must first declare and initialize it with a value. Or you can replace the arraylength with sizeof(numbers).

3. You declare your array as numbers at line 9 right? But in line 26 suddenly you refer your array as "array" which is not declared or initialized earlier.

4. Some of the variable you used is not declared as well, such as "temp" and "j". Remember to declare any variable you use and it's a good habit to initialize it with a default value.

Try fix all these errors first..... : )

if u want to just sort an array use this syntax

sort(array_name,array_name+size _of_array);
//in ur case it would be
sort(numbers,numbers+numElements);
// then run this loop for displaying the outout
for(i=0;i<numElements;i++){
cout<<numbers<<endl;

}

u dont need to iterate ..or use while loops if u are interested in sorting only
and include this header file in the begining

#include<algorithm>

usually its not required in my system but as a precaution use it

Tremendous Saga of Bubble Sorting!
Thank you,
all members of this Instant/FastFood C++ society!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.