# bubblesort troubles

Page 1 of 1

## 6 Replies - 1134 Views - Last Post: 03 October 2010 - 07:23 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=193224&amp;s=b6f56e64c31be96b9152b502f0d55353&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 heyoman1

Reputation: 64
• Posts: 735
• Joined: 30-November 09

# bubblesort troubles

Posted 02 October 2010 - 10:40 PM

I am almost complete with my bubblesort. But now i have ran into another problem: after sorting, it's dropping the last element in the array created.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#define stay; cin.get();
using namespace std;

void bubble(int set[], int size); // params to be determined

int main(int argc, char *argv[]) {
time_t seconds;
time(&seconds);

srand((unsigned int)seconds);

again:

cout << "Pick a number between 2-7: ";
int number;
cin >> number;
const int numbeh = number;
if (numbeh > 7 || numbeh < 0 || numbeh == 1){
cout << "You can only choose numbers from 2-7!" << endl;
goto again;
}
cout << "Pick another number: ";
int secondnum;
cin >> secondnum;
const int secNum = secondnum;

int array[numbeh];

for(int i = 0; i < numbeh; i++) {
array[i] = (int)(rand())* secNum / (int)3;

cout << "Here is magical number " << i+1 << "! :" << array[i] << endl;
}

cout << "Want to see this list sorted? (yes/no): ";
string yon;
cin >> yon;

if(yon == "no" || yon == "No"){
cout << "Well then you missed out. Sorry... :'(";
return 0;
}

if(yon == "yes" || "Yes"){
bubble(array, numbeh);
}
return 0;
}

void bubble(int set[], int size){
const int numLength = sizeof(set);
for(int i = 0; i < numLength; i++){
for(int j = 0; j<numLength-1;j++){
if(set[j]>set[j+1]){
int temp = set[j+1];
set[j+1] = set[j];
set[j] = temp;
}
}

cout << set[i] << endl;
}
stay;
}

After compiling and running, you enter 2 numbers. Then is creates an array of the first number. Then uses the second number to get some random number. And after accepting to sort it, the program only shows 4 (if the first number was 5+) of the elements, in order, and sometimes duplicated. How can i fix this problem?

Is This A Good Question/Topic? 0

Reputation:

## Re: bubblesort troubles

Posted 02 October 2010 - 11:14 PM

Are you doing ascending or descending sorting?

### #3 aaa111

• D.I.C Regular

Reputation: 88
• Posts: 284
• Joined: 21-February 07

## Re: bubblesort troubles

Posted 02 October 2010 - 11:16 PM

Did you ever compile this code....or you just copied it.
Its giving me error.

### #4 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: bubblesort troubles

Posted 03 October 2010 - 12:44 AM

This is wrong:

const int numLength = sizeof(set);

What is the size of int set[]?

Hint, it's a pointer.

You are passing in the size of the array. Why are you not using it?

This post has been edited by n8wxs: 03 October 2010 - 12:46 AM

### #5 baavgai

• Dreaming Coder

Reputation: 7163
• Posts: 14,929
• Joined: 16-October 07

## Re: bubblesort troubles

Posted 03 October 2010 - 12:52 AM

void bubble(int set[], int size){
const int numLength = sizeof(set);

You pass the parameter size for a reason. The "sizeof(set)" will always be 4. Use the size you passed:
void bubble(int set[], int numLength){

Also, this is a not quite a bubble sort. Why two loops? At first it looked like a poor selection sort. Right now, it's probably the worst possible sort.

Gotos? Seriously?

### #6 heyoman1

Reputation: 64
• Posts: 735
• Joined: 30-November 09

## Re: bubblesort troubles

Posted 03 October 2010 - 06:52 AM

im just starting to learn how to program...

### #7 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: bubblesort troubles

Posted 03 October 2010 - 07:23 AM

heyoman1, on 03 October 2010 - 10:52 PM, said:

im just starting to learn how to program...

Well until you know better just take it on trust that you shouldn't be using "goto" in any program you write. Go to your code now and remove it and replace it with a loop.

What do you think you are achieving by taking input from the user and storing in constants? I assume you had a great thought there but I doubt its doing whatever it is you think you were trying to do.

Get rid of this
#define stay; cin.get();

It's trying to be clever but it is just silly.

You are trying to declare a dynamic array in the middle of the code but what you are trying won't work. And it isn't working.
Two choices here:
1 - Do a google on "C++ dynamic array" to see how you should be doing
2 - Use a vector instead of an array. These are the C++ provided dynamic array and the obvious answer to use here.

My main tip would be to go back three steps in your coding and try and do things that you understand. You are getting ahead of yourself and you are going to 'learn' a lot of junk that you have to throw away before you can progress at some stage. Better to go at a more sensible pace and build a solid foundation to move onwards from at each stage.