| | |
Generate 10 random numbers (1-100) using bubblesort
![]() |
•
•
Join Date: Aug 2008
Posts: 10
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 1: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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
--
If your code lacks code tags, you will be IGNORED
•
•
Join Date: Aug 2008
Posts: 10
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 3:15 pm.
•
•
Join Date: Aug 2008
Posts: 10
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 3:44 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Need Help with this code
- Next Thread: What c++ book to get beyond beginner
Views: 2536 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment basic beginner binary browser c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count data delete desktop display dll dynamic encryption error file files form fstream function functions game givemetehcodez graph gui homework i/o iamthwee input int integer lazy library linker list loop loops map math matrix member memory newbie news number object objects opengl output parameter pointer pointers problem program programming project random read recursion recursive reference sort sorting spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual win32 window windows winsock






