6 Replies - 769 Views - Last Post: 03 October 2010 - 07:23 AM Rate Topic: -----

#1 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • 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
  • +

Replies To: bubblesort troubles

#2 Guest_Tsengel*


Reputation:

Re: bubblesort troubles

Posted 02 October 2010 - 11:14 PM

Are you doing ascending or descending sorting?
Was This Post Helpful? 0

#3 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: bubblesort troubles

Posted 03 October 2010 - 06:52 AM

im just starting to learn how to program...
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: bubblesort troubles

Posted 03 October 2010 - 07:23 AM

View Postheyoman1, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1