Bubble Sort[help!]

Using a rand # generator and array

Page 1 of 1

4 Replies - 1089 Views - Last Post: 28 October 2009 - 07:39 PM Rate Topic: -----

#1 kn336a  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-July 09

Bubble Sort[help!]

Post icon  Posted 27 October 2009 - 07:51 PM

Hi everyone, I was wondering if somebody could assist me in my program. Here are the guidelines:

-Ask user to give size of array

-Then asks again if user wants to enter the array or generate random numbers

-if user wants to enter the numbers to fill array, then ask number and receive the value to fill the array.
--------------------------------------------------------

I'm trying to figure out a couple of things. For one, what kind of statement I should use. For instance, if/else do/while etc. when asking the user for entering an array or using a rand # generator. I thought maybe I should use a switch statement, but it wasn't working very well.

Also, I am running into some odd errors after execution. The bubble sort organizes the numbers, but it creates a whole bunch of random numbers at the end.

For example:

Enter size of array: 5
1:5
2:4
3:3
4:2
5:1

The sorted values are:

1 2 3 4 5 23934 39204302 34902302 20340234 3939 etc.



Here is my code so far...


#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void sortArray(int [], int);
void showArray(int [], int);


int main()
{
   
	unsigned seed = time(0);
	srand(seed);
	
	int values[1000];
	int size;
	
   
	cout << "Enter size of array: ";
	cin >> size;
	 
	
	for(int i=0; i < size; i++)
	{
			cout << i+1 << ":";
			cin >> values[i];
	}
	
  
	
	sortArray(values,100);
	
	cout << "The sorted values are:\n";
	
	showArray(values,100);
	
	system("pause");
	return 0;
}

void sortArray(int array[], int size)
{
	 bool swap;
	 int temp;
	 
	 do
	 {
		 swap = false;
		 for(int count = 0; count < (size-1); count++)
		 {
				 if(array[count] > array[count+1])
				 {
								 temp = array[count];
								 array[count] = array[count+1];
								 array[count+1] = temp;
								 swap = true;
				 }
		 }
	 } while(swap);
}

void showArray(int array[], int size)
{
	 for(int count = 0; count < size; count++)
			 cout << array[count] << " ";
	 cout << endl;
}






Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Bubble Sort[help!]

#2 zhanfeng  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 75
  • Joined: 09-November 08

Re: Bubble Sort[help!]

Posted 27 October 2009 - 08:04 PM

I would suggest you to use while loop to get a VALID choice of either fill in the array manually or automatically. Secondly, you are passing a hardcoded value of the array size to sort and show functions. You should pass the size of the array(the # user entered) instead. One question for you, have you learned how to allocate an array dynamically?

This post has been edited by zhanfeng: 27 October 2009 - 08:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 kn336a  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-July 09

Re: Bubble Sort[help!]

Posted 27 October 2009 - 08:18 PM

View Postzhanfeng, on 27 Oct, 2009 - 07:04 PM, said:

I would suggest you to use while loop to get a VALID choice of either fill in the array manually or automatically. Secondly, you are passing a hardcoded value of the array size to sort and show functions. You should pass the size of the array(the # user entered) instead. One question for you, have you learned how to allocate an array dynamically?


I'm not quite familiar with a dynamic array. Sorry, it's hard for me to visualize what you're saying. =/ This is a hard class for me. I'll try to look up what you're talking about in my text book.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Bubble Sort[help!]

Posted 28 October 2009 - 03:54 AM

Your error is simple. You've probably just been staring at it too long. ;)
// ok
cout << "Enter size of array: ";
cin >> size;
	 
// enter data

// um, you know they only entered "size" amount, right?	
//sortArray(values,100);
sortArray(values, size);



My fear is, given the nature of the error, that you might have ripped off some of this code. Trying to fudge programming will make it much, much harder. The fundamentals are the hard part, if you don't have them down then you're not going anywhere. Once you have a good grasp of the basics then the problems, even the language, doesn't matter so much.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#5 kn336a  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-July 09

Re: Bubble Sort[help!]

Posted 28 October 2009 - 07:39 PM

View Postbaavgai, on 28 Oct, 2009 - 02:54 AM, said:

Your error is simple. You've probably just been staring at it too long. ;)
// ok
cout << "Enter size of array: ";
cin >> size;
	 
// enter data

// um, you know they only entered "size" amount, right?	
//sortArray(values,100);
sortArray(values, size);



My fear is, given the nature of the error, that you might have ripped off some of this code. Trying to fudge programming will make it much, much harder. The fundamentals are the hard part, if you don't have them down then you're not going anywhere. Once you have a good grasp of the basics then the problems, even the language, doesn't matter so much.

Good luck.


Thanks for your help! This is code shown in our class on how to use bubble sort. She wants us to incorporate things into it =]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1