943,822 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3468
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Aug 30th, 2008
0

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

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Aug 30th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
entei is offline Offline
6 posts
since Aug 2008
Aug 30th, 2008
0

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

Yes I am a beginner, just started taking C++ 2 weeks ago. I got the program to work but can't sort it.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LiquidScorpio81 is offline Offline
10 posts
since Aug 2008
Aug 30th, 2008
0

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

Yes I am a beginner, just started taking C++ 2 weeks ago. I got the program to work but can't sort it.

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need Help with this code
Next Thread in C++ Forum Timeline: What c++ book to get beyond beginner





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC