473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Bubble sort

Sheepman
I've been working on this since Wednesday and am at a total loss. I need to bubble sort a randomly generated list of numbers. I got the numbers in about 15 minutes. The remainder of the 3 days have been spent on trying to sort it. Generating the random numbers and sorting them has to be done with two seperate functions outside of main().

Here is my function to generate my numbers:

Expand|Select|Wrap|Line Numbers
  1. void randFunc() {
  2. int firstArray [100];//Declares an array of 100 items
  3.     int min = -50;
  4. int max = 50;
  5. int randNum;
  6. for (randNum = 0; randNum < 100; randNum++) {
  7. firstArray[randNum] = rand()%(max - min + 1) + min;
  8. cout << setw (5) << firstArray[randNum] ;
  9. }//Close for
  10.  
  11. }//close randfunc
  12.  
The above was working when called from main().

My problems I'm sure are from a weak understanding of how to call functions and call arrays with functions. At this point I'm absoluting just guessing, with no rhyme or reason to changes in code. I'm simply throwing things against the wall to see if anything sticks.


Now here is my poor attempt at trying to sort it or at least the current version. My errors are down to three, that's better than the 17 I started with.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib> 
  4. #include <ctime>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8. int randFunc();
  9. //int firstArray[100];
  10.  
  11. int main () {
  12.  
  13. //Generate random variable seed
  14. srand ((unsigned)time(0));//Seed random number generator
  15.  
  16. // char yourFilename [80];//User supplied file name
  17.  
  18. //Call random variable generator
  19. int randFunc(); 
  20. //Prompt user for name of file in which to store array
  21. cout << " Enter name the name of your file where you would like your array sorted array stored " << endl;
  22. // cin << yourFileName; 
  23. return (0);
  24. }//close main
  25.  
  26. int randFunc(int firstArray[100])
  27. {
  28.  
  29. //int firstArray [100];
  30. //Declares an array of 100 items
  31.  
  32. int min = -50;
  33. int max = 50;
  34. int randNum;
  35. for (randNum = 0; randNum < 100; randNum++)
  36. {
  37.  
  38. firstArray[randNum] = rand()%(max - min + 1) + min;
  39. cout << setw (5) << firstArray[randNum] ;
  40. }//Close for
  41.  
  42. return firstArray[100];
  43. }//close randfunc
  44.  
  45. void bubbleSort (int firstArray [100])
  46. {
  47. int swap;
  48. for(int pass = 0; pass < 100; pass++)
  49. {
  50. for (int randNum = 0; randNum < 99 - pass; randNum++)
  51. {
  52. if (firstArray[randNum] > firstArray[randNum + 1])
  53. {
  54.     swap = firstArray[randNum];
  55.     firstArray [randNum] = firstArray [randNum + 1];
  56.     firstArray[randNum + 1] = swap;
  57.     cout << firstArray[randNum];
  58.     cout << endl;
  59. }
  60. }
  61. }
  62. }
  63.  
Eventually I have to write this to the screen and a file, formatted in 10 rows of 10. Also done with another function. Thats why there is some extra stuff in main that I'm not using yet.

Yes this is homework, I'm not this sadistic normally!
Jul 6 '07 #1
5 3544
I actually got all the errors to clear but now the sorted numbers don't print to the screen. I'm not even sure they are being sorted. Just the prompt for the file name appear which should come after the sorting.
Jul 6 '07 #2
r035198x
13,262 8TB
I actually got all the errors to clear but now the sorted numbers don't print to the screen. I'm not even sure they are being sorted. Just the prompt for the file name appear which should come after the sorting.
Here is an article explaining the bubble sort.
Jul 7 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
You have several errors.

1) You did not declare an array to sort.
2) You did not call the function to populate the array. What you believed to be the call was a function prototype.
3) You re-invented the wheel by coding a sort instead of using the sort in the C++ Standard Library.

Compare your code to the version below which works.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib> 
  4. #include <ctime>
  5. #include <iomanip>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9. int randFunc(int*);
  10. int firstArray[100];
  11.  
  12. int main () {
  13.  
  14. //Generate random variable seed
  15. srand ((unsigned)time(0));//Seed random number generator
  16.  
  17. // char yourFilename [80];//User supplied file name
  18.  
  19. //Call random variable generator
  20. randFunc(firstArray); 
  21. //Prompt user for name of file in which to store array
  22. cout << " Enter name the name of your file where you would like your array sorted array stored " << endl;
  23. // cin << yourFileName; 
  24.  
  25. //sort the array
  26. sort(&firstArray[0], &firstArray[100]);
  27.  
  28. for (int i = 0; i <100; ++i)
  29. {
  30.     cout << firstArray[i] << endl;
  31. }
  32.  
  33. return (0);
  34. }//close main
  35.  
  36. int randFunc(int firstArray[100])
  37. {
  38.  
  39. //int firstArray [100];
  40. //Declares an array of 100 items
  41.  
  42. int min = -50;
  43. int max = 50;
  44. int randNum;
  45. for (randNum = 0; randNum < 100; randNum++)
  46. {
  47.  
  48. firstArray[randNum] = rand()%(max - min + 1) + min;
  49. cout << setw (5) << firstArray[randNum] ;
  50. }//Close for
  51.  
  52. return firstArray[100];
  53. }//close randfunc
  54.  
Jul 7 '07 #4
You have several errors.

1) You did not declare an array to sort.
2) You did not call the function to populate the array. What you believed to be the call was a function prototype.
3) You re-invented the wheel by coding a sort instead of using the sort in the C++ Standard Library.

Compare your code to the version below which works.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib> 
  4. #include <ctime>
  5. #include <iomanip>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9. int randFunc(int*);
  10. int firstArray[100];
  11.  
  12. int main () {
  13.  
  14. //Generate random variable seed
  15. srand ((unsigned)time(0));//Seed random number generator
  16.  
  17. // char yourFilename [80];//User supplied file name
  18.  
  19. //Call random variable generator
  20. randFunc(firstArray); 
  21. //Prompt user for name of file in which to store array
  22. cout << " Enter name the name of your file where you would like your array sorted array stored " << endl;
  23. // cin << yourFileName; 
  24.  
  25. //sort the array
  26. sort(&firstArray[0], &firstArray[100]);
  27.  
  28. for (int i = 0; i <100; ++i)
  29. {
  30.     cout << firstArray[i] << endl;
  31. }
  32.  
  33. return (0);
  34. }//close main
  35.  
  36. int randFunc(int firstArray[100])
  37. {
  38.  
  39. //int firstArray [100];
  40. //Declares an array of 100 items
  41.  
  42. int min = -50;
  43. int max = 50;
  44. int randNum;
  45. for (randNum = 0; randNum < 100; randNum++)
  46. {
  47.  
  48. firstArray[randNum] = rand()%(max - min + 1) + min;
  49. cout << setw (5) << firstArray[randNum] ;
  50. }//Close for
  51.  
  52. return firstArray[100];
  53. }//close randfunc
  54.  
Thank you very much for the response. I believe have have to reinvent the wheel for the sort as that was specifically stated in my assignment.

I know my biggest problem is with calling for the information I need. That's the part I really don't understand. I've read ever tutorial on line I can find plus my textbook. The syntax kills me. As you mentioned in your first two comment. I think I'm calling something when I'm not.

Would it be possible for you to show just the set up for the calling of the data. Not the statements that do the work. Since my second post yesterday I've worked my way back up to 5 errors. All revolving around , this isn't declared, that isn't declared, you can't change an int to a void. I totally confused on calling functions and arrays!

One last question at some point should I be expecting a lightbulb type moment? Because right now I feel like I'm stumbling around blindfoldd in a cave.

Again thanks for your help
Jul 7 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
One last question at some point should I be expecting a lightbulb type moment? Because right now I feel like I'm stumbling around blindfoldd in a cave.
Yes. I had one. Took two years. Lost most of my hair. There is no learning of C++ without the necessary tears. These tears occur at 2:15am as you are slamming your keyboard into your monitor so mad with frustration you are ready to pitch the lot in the trash. This has happened to all of us. That's why we are so smug about it.

Now to your code:
1) you define variables before you use them.
2) variables passed to functions are copied. The function uses the copy. There are exceptions to this, but this is where to start.
3) for a function to change a variable in the calling function, you need to pass the address of the variable and not the variable itself. The address is passed using a pointer variable.

Expand|Select|Wrap|Line Numbers
  1. void Init(int* arr, int numelements)
  2. {
  3.      //code here to intialize the 100 ints.
  4.      //the int* is the address of array[0].
  5.       //here arr[0] is array[0] in main(), arr[1] is array[1], etc.
  6.  
  7. }
  8. void MySort(int* arr, int numelements)
  9. {
  10.       //here you sort the array.
  11.       //here arr[0] is array[0] in main(), arr[1] is array[1], etc.
  12. }
  13. int main()
  14. {
  15.    int array[100];     //defines 100 ints named array[0], array[1], etc
  16.  
  17.    Init(array, 100);          //calls the Init function. Look above main()
  18.  
  19.    MySort(array, 100);   //sort the array using MySort function. Look above main()
  20. }
  21.  
The secret to using arrays as function arguments is to be clear in your mind that the name of the array is the address of element 0. So:
Expand|Select|Wrap|Line Numbers
  1. char stuff[10];   //stuff is address of stuff[0]. stuff[0] is a char so stuff is a char*
  2.                        //you use stuff as the address of the array
  3.                        //the function would need a char* argument.
  4.  
Jul 8 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Gram | last post by:
Hello, Can anyone help out with a bubble sort for strings using ASP? I have a text file of words id like to sort and count. Thanks in advance for any help. Gram.
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
4
by: Chris | last post by:
I have a bubble sort for a 2-dimensional array that sorts a string,number pair based on the number. The code for the sort is as follows: Private Sub SortArray(ByRef roundarray(,) As String)...
0
by: Slowjam | last post by:
How do I correct the program below to search all the anagrams in a given file. First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for...
1
by: guest | last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for...
9
by: mosullivan | last post by:
I can't figure out how to print after every pass through the bubble sort. I'm supposed to display the sort after every pass through the loop. Below is what I have so far. #include <stdio.h>...
12
by: midknight5 | last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold...
14
by: xtheendx | last post by:
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The...
7
by: mahdiahmadirad | last post by:
Hi dears! I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.