943,709 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3467
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 30th, 2008
0

Generate 10 random numbers (1-100) using bubblesort

Expand Post »
#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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LiquidScorpio81 is offline Offline
10 posts
since Aug 2008
Aug 30th, 2008
0

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

Well, and your question is... ???
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 30th, 2008
0

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

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 ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LiquidScorpio81 is offline Offline
10 posts
since Aug 2008
Aug 30th, 2008
0

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

> 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 piss 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 30th, 2008
0

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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LiquidScorpio81 is offline Offline
10 posts
since Aug 2008
Aug 30th, 2008
0

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

how do i indent ? i press tab and it just tabs over to the reply button
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LiquidScorpio81 is offline Offline
10 posts
since Aug 2008
Aug 30th, 2008
0

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

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
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Aug 30th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by mitrmkar ...
Indent using the space bar.
I see.
Last edited by LiquidScorpio81; Aug 30th, 2008 at 4:20 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LiquidScorpio81 is offline Offline
10 posts
since Aug 2008
Aug 30th, 2008
0

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

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.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Aug 30th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by mitrmkar ...
If you don't use code tags, the indentation simply gets lost.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LiquidScorpio81 is offline Offline
10 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need Help with this code
Next Thread in C++ Forum Timeline: What c++ book to get beyond beginner





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC