| | |
Generate 10 random numbers (1-100) using bubblesort
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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: Need Help with this code
- Next Thread: What c++ book to get beyond beginner
Views: 2298 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






