Well, and your question is... ???
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
> 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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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 usingcode tags is
[code]
your code here ...
[/code]
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
It appears indented when in the text editor, once it's edited it appears non-indented.
If you don't usecode tags, the indentation simply gets lost.
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
#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.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
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.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711