Generate 10 random numbers (1-100) using bubblesort

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Generate 10 random numbers (1-100) using bubblesort

 
0
  #11
Aug 30th, 2008
  1. #include<iostream>
  2. using namespace std;
  3. void BubbleSort(int ar[10], int n, int & compare_costs, int & swap_costs)
  4. {
  5. int t; int swaps=-1;
  6. while(swaps)
  7. {
  8. swaps++;
  9. compare_costs++;
  10. for(int i=0; i<n-1; i++)
  11. if(ar[i] > ar[i+1])
  12. {
  13. t=ar[i]; ar[i]=ar[i+1]; ar[i+1]=t;
  14. swap_costs=swap_costs+5;
  15. }
  16. }
  17. }
  18. int main()
  19. {
  20. int n, compare_costs, swap_costs;
  21. int ar[10];
  22. int *bs = new int[n];
  23. int *ss = new int[n];
  24. int *qs = new int[n];
  25. srand(time(0));
  26. for(int i=0; i<n-1; i++)
  27. *(bs+i) = *(ss+i) = *(qs+i) = 1+rand()%100;
  28.  
  29. for(int i=0; i<n-1; i++)
  30. cout<< *(bs+i)<< " "; //prints random number before sort
  31.  
  32. BubbleSort(bs,n,compare_costs,swap_costs);
  33. cout<<endl;
  34.  
  35. for(int i=0; i<n-1; i++)
  36. cout<< *(bs+i)<< " "; //"SUPPOSE TO" print number after sort
  37.  
  38. }

Lines 5, 6, 8 - swaps is initialized to -1, then it's incremented to 0 regardless of whether there are any swaps, at which time the loop will exit.

lines 20, 32, 3, 9, 14. compare_costs and swap_costs are declared in line 20 but never initialized, passed by reference in line 32, then used in your function. You need to initialize them somewhere or get rid of them. Don't assume they'll be initialized to 0.
Last edited by VernonDozier; Aug 30th, 2008 at 4:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 6
Reputation: entei is an unknown quantity at this point 
Solved Threads: 1
entei entei is offline Offline
Newbie Poster

Re: Generate 10 random numbers (1-100) using bubblesort

 
0
  #12
Aug 30th, 2008
you code doesnt make sense so i have to ask, "Are you new to programming?", "And is this the full source code"
I'm only asking because theres some serious flaws in you code. first you have variables that arent initialised, you have memory leaks were you've allocated new memory on the heap and not destoryed them, and you cand seed the system time for you random number withouth including the correct header like <ctime>, i suggest you start again and try to use arrays instead of pointers in your algorithm
Last edited by entei; Aug 30th, 2008 at 7:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 9
Reputation: LiquidScorpio81 is an unknown quantity at this point 
Solved Threads: 0
LiquidScorpio81 LiquidScorpio81 is offline Offline
Newbie Poster

Re: Generate 10 random numbers (1-100) using bubblesort

 
0
  #13
Aug 30th, 2008
Yes I am a beginner, just started taking C++ 2 weeks ago. I got the program to work but can't sort it.

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void BubbleSort(int ar[10],int n, int compare_costs, int swap_costs)
  5. {
  6. int t; int swaps=-1;
  7. compare_costs=0; swap_costs=0;
  8. while(swaps)
  9. {
  10. swaps++;
  11. for(int i=0; i<10-1; i++,compare_costs++)
  12. if(ar[i] > ar[i+1])
  13. {
  14. t=ar[i]; ar[i]=ar[i+1]; ar[i+1]=t;
  15. swap_costs=swap_costs+5;
  16. }
  17. }
  18. }
  19. int main()
  20. {
  21. int n, compare_costs, swap_costs;
  22. int *bs = new int[n];
  23. int *ss = new int[n];
  24. int *qs = new int[n];
  25. //srand(time(0));
  26.  
  27. for(int i=0; i<n; i++)
  28. *(bs+i) = *(ss+i) = *(qs+i)= rand()%100;
  29. for(int i=0; i<10; i++)
  30. cout<< *(bs+i)<< " ";
  31. BubbleSort(bs,n,compare_costs,swap_costs);
  32. cout<<endl;
  33. for(int i=0; i<10; i++)
  34. cout<< *(bs+i)<< " ";
  35. // cout << "\nCosts:" << swap_costs;
  36. // cout << "\nCompare:"<< compare_costs;
  37. // int cost = swap_costs + compare_costs;
  38. // cout << "\nCosts:" << cost << endl;
  39. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Generate 10 random numbers (1-100) using bubblesort

 
0
  #14
Aug 30th, 2008
Originally Posted by LiquidScorpio81 View Post
Yes I am a beginner, just started taking C++ 2 weeks ago. I got the program to work but can't sort it.

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void BubbleSort(int ar[10],int n, int compare_costs, int swap_costs)
  5. {
  6. int t; int swaps=-1;
  7. compare_costs=0; swap_costs=0;
  8. while(swaps)
  9. {
  10. swaps++;
  11. for(int i=0; i<10-1; i++,compare_costs++)
  12. if(ar[i] > ar[i+1])
  13. {
  14. t=ar[i]; ar[i]=ar[i+1]; ar[i+1]=t;
  15. swap_costs=swap_costs+5;
  16. }
  17. }
  18. }
  19. int main()
  20. {
  21. int n, compare_costs, swap_costs;
  22. int *bs = new int[n];
  23. int *ss = new int[n];
  24. int *qs = new int[n];
  25. //srand(time(0));
  26.  
  27. for(int i=0; i<n; i++)
  28. *(bs+i) = *(ss+i) = *(qs+i)= rand()%100;
  29. for(int i=0; i<10; i++)
  30. cout<< *(bs+i)<< " ";
  31. BubbleSort(bs,n,compare_costs,swap_costs);
  32. cout<<endl;
  33. for(int i=0; i<10; i++)
  34. cout<< *(bs+i)<< " ";
  35. // cout << "\nCosts:" << swap_costs;
  36. // cout << "\nCompare:"<< compare_costs;
  37. // int cost = swap_costs + compare_costs;
  38. // cout << "\nCosts:" << cost << endl;
  39. }
Read my last post and the previous poster's post. Don't take out your random seed line. Add:

  1. #include <ctime>

at the top. C++, unlike C, has boolean variables. Make swaps a boolean variable. In Bubble Sort, your numbers are sorted when you are able to go through all of the elements with no swaps. Keep going through them till that is true. So your while loop condition is good, but change swaps to a boolean variable and don't increment it, but rather set it to true or false.

Any variable you use must be initialized somewhere. So if n represents the number of elements and the number of elements is 10, assign n to equal ten. Tackle one thing at a time for now and don't worry about swap_costs and compare_costs. Get the sorting right first. You can keep those variables in there the way you have them now, but they won't be accurate the way you have them. You actually had it right before. Pass them by reference like you had it originally.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC