| | |
Generate 10 random numbers (1-100) using bubblesort
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 9
Reputation:
Solved Threads: 0
#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)<< " ";
BubbleSort(bs,n,compare_costs,swap_costs);
cout<<endl;
for(int i=0; i<n-1; i++)
cout<< *(bs+i)<< " ";
}
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)<< " ";
BubbleSort(bs,n,compare_costs,swap_costs);
cout<<endl;
for(int i=0; i<n-1; i++)
cout<< *(bs+i)<< " ";
}
Last edited by LiquidScorpio81; Aug 30th, 2008 at 2:45 pm.
> Last edited by LiquidScorpio81 : 43 Minutes Ago at 18:45.
It's a shame you didn't add the code tags.
"Hey, I know, lets **** off all the people who help around here by continually posting unindented crap."
Doesn't work kiddo. Either we ignore you, or just report the post and wait for a mod to show up in a few hours. Then, if we're still interested, we might just get around to looking at your missive.
Either way, what could have been answered straight away is now delayed until your post gets fixed.
It's a shame you didn't add the code tags.
"Hey, I know, lets **** off all the people who help around here by continually posting unindented crap."
Doesn't work kiddo. Either we ignore you, or just report the post and wait for a mod to show up in a few hours. Then, if we're still interested, we might just get around to looking at your missive.
Either way, what could have been answered straight away is now delayed until your post gets fixed.
•
•
Join Date: Aug 2008
Posts: 9
Reputation:
Solved Threads: 0
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 }
Last edited by LiquidScorpio81; Aug 30th, 2008 at 4:15 pm.
•
•
Join Date: Aug 2008
Posts: 9
Reputation:
Solved Threads: 0
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<10-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<10; i++) cout<< *(bs+i)<< " "; //prints random number before sort BubbleSort(bs,n,compare_costs,swap_costs); cout<<endl; for(int i=0; i<10; i++) cout<< *(bs+i)<< " "; //"SUPPOSE TO" print number after sort }
Last edited by LiquidScorpio81; Aug 30th, 2008 at 4:44 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: C++ Template success, methods in seperate .cpp file
- Next Thread: What c++ book to get beyond beginner
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






