DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Generate 10 random numbers (1-100) using bubblesort (http://www.daniweb.com/forums/thread143125.html)

LiquidScorpio81 Aug 30th, 2008 2:41 pm
Generate 10 random numbers (1-100) using bubblesort
 
#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)<< " ";

}

ArkM Aug 30th, 2008 3:22 pm
Re: Generate 10 random numbers (1-100) using bubblesort
 
Well, and your question is... ???

LiquidScorpio81 Aug 30th, 2008 3:27 pm
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 ?

Salem Aug 30th, 2008 3:33 pm
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.

LiquidScorpio81 Aug 30th, 2008 3:49 pm
Re: Generate 10 random numbers (1-100) using bubblesort
 
#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

}

LiquidScorpio81 Aug 30th, 2008 3:53 pm
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

mitrmkar Aug 30th, 2008 4:07 pm
Re: Generate 10 random numbers (1-100) using bubblesort
 
Quote:

Originally Posted by LiquidScorpio81 (Post 681300)
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]

LiquidScorpio81 Aug 30th, 2008 4:12 pm
Re: Generate 10 random numbers (1-100) using bubblesort
 
Quote:

Originally Posted by mitrmkar (Post 681302)
Indent using the space bar.

I see.

mitrmkar Aug 30th, 2008 4:15 pm
Re: Generate 10 random numbers (1-100) using bubblesort
 
Quote:

Originally Posted by LiquidScorpio81 (Post 681306)
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.

LiquidScorpio81 Aug 30th, 2008 4:21 pm
Re: Generate 10 random numbers (1-100) using bubblesort
 
Quote:

Originally Posted by mitrmkar (Post 681309)
If you don't use code tags, the indentation simply gets lost.

#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

}


All times are GMT -4. The time now is 4:34 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC