thanks a lot..
i got it..
take care...bye ....
Moderator: Board moderators
#include <iostream>
#include <algorithm>
#include <algo.h>
using namespace std;
int main ()
{
int a[1002],n,i,count=0;
while(cin >> n)
{
if(n<=0)
break;
count=0;
for (i=0; i<n; i++)
cin >>a[i];
while (!is_sorted(a,a+n))
{
for (i=0; i<n; i++)
{
if (a[i]>a[i+1])
{
swap(a[i], a[i+1]);
count++;
}
}
}
cout<<"Minimum exchange operations : "<<count<<endl;
}
return 0;
}
for (i=0; i<n; i++)
cin >>a[i];
for (i=0; i<n; i++)
{
if (a[i]>a[i+1]) .....//problem here
look here you r comparing a[n-1]>a[n] when i=n-1(final pass).but what is the index of last element in a[]??is it n or n-1???
so write i<n-1 rather than n.
you have used #include <algo.h> i dont know what is this
you have also used several built in function like is_sorted,swap.
i think u shouldn't use built in function for such a simple problem if you want to learn programing.
you should code this bubble sort manually.
removed.
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
//freopen("input.in", "r", stdin);
int n,x[1003];
while(cin>>n)
{
for(int i=0;i<n;i++)
{cin>>x[i];}
int count=0;
///////////ARRANGING ARRAY (SORTING) --->HERE ASCENDING FOR EXAMPLE//////////////
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
if (x[i]>x[j]) /////Swapping
{swap(x[i],x[j]);
count++;
}
}
//////////////////////END OF SORTING///////////////////////////////////////
cout<<"Minimum exchange operations : "<<count<<endl;
}
return 0;
}
Users browsing this forum: No registered users and 0 guests