| | |
Generate 10 random numbers (1-100) using bubblesort
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; void BubbleSort(int ar[10], int n, int & compare_costs, int & swap_costs) { int t; int swaps=-1; while(swaps) { swaps++; compare_costs++; for(int i=0; i<n-1; i++) if(ar[i] > ar[i+1]) { t=ar[i]; ar[i]=ar[i+1]; ar[i+1]=t; swap_costs=swap_costs+5; } } } int main() { int n, compare_costs, swap_costs; int ar[10]; int *bs = new int[n]; int *ss = new int[n]; int *qs = new int[n]; srand(time(0)); for(int i=0; i<n-1; i++) *(bs+i) = *(ss+i) = *(qs+i) = 1+rand()%100; for(int i=0; i<n-1; i++) cout<< *(bs+i)<< " "; //prints random number before sort BubbleSort(bs,n,compare_costs,swap_costs); cout<<endl; for(int i=0; i<n-1; i++) cout<< *(bs+i)<< " "; //"SUPPOSE TO" print number after sort }
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.
•
•
Join Date: Aug 2008
Posts: 6
Reputation:
Solved Threads: 1
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
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.
•
•
Join Date: Aug 2008
Posts: 9
Reputation:
Solved Threads: 0
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)
#include<iostream> using namespace std; void BubbleSort(int ar[10],int n, int compare_costs, int swap_costs) { int t; int swaps=-1; compare_costs=0; swap_costs=0; while(swaps) { swaps++; for(int i=0; i<10-1; i++,compare_costs++) if(ar[i] > ar[i+1]) { t=ar[i]; ar[i]=ar[i+1]; ar[i+1]=t; swap_costs=swap_costs+5; } } } int main() { int n, compare_costs, swap_costs; int *bs = new int[n]; int *ss = new int[n]; int *qs = new int[n]; //srand(time(0)); for(int i=0; i<n; i++) *(bs+i) = *(ss+i) = *(qs+i)= rand()%100; for(int i=0; i<10; i++) cout<< *(bs+i)<< " "; BubbleSort(bs,n,compare_costs,swap_costs); cout<<endl; for(int i=0; i<10; i++) cout<< *(bs+i)<< " "; // cout << "\nCosts:" << swap_costs; // cout << "\nCompare:"<< compare_costs; // int cost = swap_costs + compare_costs; // cout << "\nCosts:" << cost << endl; }
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
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)
#include<iostream> using namespace std; void BubbleSort(int ar[10],int n, int compare_costs, int swap_costs) { int t; int swaps=-1; compare_costs=0; swap_costs=0; while(swaps) { swaps++; for(int i=0; i<10-1; i++,compare_costs++) if(ar[i] > ar[i+1]) { t=ar[i]; ar[i]=ar[i+1]; ar[i+1]=t; swap_costs=swap_costs+5; } } } int main() { int n, compare_costs, swap_costs; int *bs = new int[n]; int *ss = new int[n]; int *qs = new int[n]; //srand(time(0)); for(int i=0; i<n; i++) *(bs+i) = *(ss+i) = *(qs+i)= rand()%100; for(int i=0; i<10; i++) cout<< *(bs+i)<< " "; BubbleSort(bs,n,compare_costs,swap_costs); cout<<endl; for(int i=0; i<10; i++) cout<< *(bs+i)<< " "; // cout << "\nCosts:" << swap_costs; // cout << "\nCompare:"<< compare_costs; // int cost = swap_costs + compare_costs; // cout << "\nCosts:" << cost << endl; }
C++ Syntax (Toggle Plain Text)
#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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Need Help with this code
- Next Thread: What c++ book to get beyond beginner
Views: 2299 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






