#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 > ar[i+1])
{
t=ar; ar=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)<< " ";


}

Well, and your question is... ???

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 ?

> 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.

#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

}

how do i indent ? i press tab and it just tabs over to the reply button

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]

Indent using the space bar.

I see.

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.

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

}
#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

}

Lines 5, 6, 8 - swaps is initialized to -1, then it's incremented to 0 regardless of whether there are any swaps, at which time the loop will exit.

lines 20, 32, 3, 9, 14. compare_costs and swap_costs are declared in line 20 but never initialized, passed by reference in line 32, then used in your function. You need to initialize them somewhere or get rid of them. Don't assume they'll be initialized to 0.

you code doesnt make sense so i have to ask, "Are you new to programming?", "And is this the full source code"
I'm only asking because theres some serious flaws in you code. first you have variables that arent initialised, you have memory leaks were you've allocated new memory on the heap and not destoryed them, and you cand seed the system time for you random number withouth including the correct header like <ctime>, i suggest you start again and try to use arrays instead of pointers in your algorithm

Yes I am a beginner, just started taking C++ 2 weeks ago. I got the program to work but can't sort it.

#include<iostream>
using namespace std;

void BubbleSort(int ar[10],int n, int compare_costs, int swap_costs)
{
        int t; int swaps=-1;
        compare_costs=0; swap_costs=0;
        while(swaps)
        {
                swaps++;
                for(int i=0; i<10-1; i++,compare_costs++)
                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 *bs =  new int[n];
        int *ss =  new int[n];
        int *qs =  new int[n];
        //srand(time(0));

for(int i=0; i<n; i++)
        *(bs+i) = *(ss+i) = *(qs+i)= rand()%100;
for(int i=0; i<10; i++)
cout<< *(bs+i)<< " ";
        BubbleSort(bs,n,compare_costs,swap_costs);
        cout<<endl;
for(int i=0; i<10; i++)
cout<< *(bs+i)<< " ";
//      cout << "\nCosts:" << swap_costs;
//      cout << "\nCompare:"<< compare_costs;
//      int cost = swap_costs + compare_costs;
//      cout << "\nCosts:" << cost << endl;
}

Yes I am a beginner, just started taking C++ 2 weeks ago. I got the program to work but can't sort it.

#include<iostream>
using namespace std;

void BubbleSort(int ar[10],int n, int compare_costs, int swap_costs)
{
        int t; int swaps=-1;
        compare_costs=0; swap_costs=0;
        while(swaps)
        {
                swaps++;
                for(int i=0; i<10-1; i++,compare_costs++)
                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 *bs =  new int[n];
        int *ss =  new int[n];
        int *qs =  new int[n];
        //srand(time(0));

for(int i=0; i<n; i++)
        *(bs+i) = *(ss+i) = *(qs+i)= rand()%100;
for(int i=0; i<10; i++)
cout<< *(bs+i)<< " ";
        BubbleSort(bs,n,compare_costs,swap_costs);
        cout<<endl;
for(int i=0; i<10; i++)
cout<< *(bs+i)<< " ";
//      cout << "\nCosts:" << swap_costs;
//      cout << "\nCompare:"<< compare_costs;
//      int cost = swap_costs + compare_costs;
//      cout << "\nCosts:" << cost << endl;
}

Read my last post and the previous poster's post. Don't take out your random seed line. Add:

#include <ctime>

at the top. C++, unlike C, has boolean variables. Make swaps a boolean variable. In Bubble Sort, your numbers are sorted when you are able to go through all of the elements with no swaps. Keep going through them till that is true. So your while loop condition is good, but change swaps to a boolean variable and don't increment it, but rather set it to true or false.

Any variable you use must be initialized somewhere. So if n represents the number of elements and the number of elements is 10, assign n to equal ten. Tackle one thing at a time for now and don't worry about swap_costs and compare_costs. Get the sorting right first. You can keep those variables in there the way you have them now, but they won't be accurate the way you have them. You actually had it right before. Pass them by reference like you had it originally.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.