Generate 10 random numbers (1-100) using bubblesort

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 9
Reputation: LiquidScorpio81 is an unknown quantity at this point 
Solved Threads: 0
LiquidScorpio81 LiquidScorpio81 is offline Offline
Newbie Poster

Generate 10 random numbers (1-100) using bubblesort

 
0
  #1
Aug 30th, 2008
#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)<< " ";

}
Last edited by LiquidScorpio81; Aug 30th, 2008 at 2:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

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

 
0
  #2
Aug 30th, 2008
Well, and your question is... ???
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 9
Reputation: LiquidScorpio81 is an unknown quantity at this point 
Solved Threads: 0
LiquidScorpio81 LiquidScorpio81 is offline Offline
Newbie Poster

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

 
0
  #3
Aug 30th, 2008
When I run the program it appears:
83 86 77 15 93 35 86 92 49 21
83 77 15 86 35 86 92 49 21 62

It is clearly not sorted and 93 appears on the first output but not the second, and 62 is in the second output but not the first. Why is it doing this ?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #4
Aug 30th, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 9
Reputation: LiquidScorpio81 is an unknown quantity at this point 
Solved Threads: 0
LiquidScorpio81 LiquidScorpio81 is offline Offline
Newbie Poster

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

 
0
  #5
Aug 30th, 2008
  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. }
Last edited by LiquidScorpio81; Aug 30th, 2008 at 4:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 9
Reputation: LiquidScorpio81 is an unknown quantity at this point 
Solved Threads: 0
LiquidScorpio81 LiquidScorpio81 is offline Offline
Newbie Poster

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

 
0
  #6
Aug 30th, 2008
how do i indent ? i press tab and it just tabs over to the reply button
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

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

 
0
  #7
Aug 30th, 2008
Originally Posted by LiquidScorpio81 View Post
how do i indent ? i press tab and it just tabs over to the reply button
Indent using the space bar.

And the simplest form of using code tags is

[code]
your code here ...
[/code]
Last edited by mitrmkar; Aug 30th, 2008 at 4:11 pm. Reason: code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 9
Reputation: LiquidScorpio81 is an unknown quantity at this point 
Solved Threads: 0
LiquidScorpio81 LiquidScorpio81 is offline Offline
Newbie Poster

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

 
0
  #8
Aug 30th, 2008
Originally Posted by mitrmkar View Post
Indent using the space bar.
I see.
Last edited by LiquidScorpio81; Aug 30th, 2008 at 4:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

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

 
0
  #9
Aug 30th, 2008
Originally Posted by LiquidScorpio81 View Post
It appears indented when in the text editor, once it's edited it appears non-indented.
If you don't use code tags, the indentation simply gets lost.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 9
Reputation: LiquidScorpio81 is an unknown quantity at this point 
Solved Threads: 0
LiquidScorpio81 LiquidScorpio81 is offline Offline
Newbie Poster

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

 
0
  #10
Aug 30th, 2008
Originally Posted by mitrmkar View Post
If you don't use code tags, the indentation simply gets lost.
  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<10-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<10; 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<10; i++)
  36. cout<< *(bs+i)<< " "; //"SUPPOSE TO" print number after sort
  37.  
  38. }
Last edited by LiquidScorpio81; Aug 30th, 2008 at 4:44 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC