Sorting a struct
I am trying to sort an array declared as a struct but it doesn't work. What am i doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("date.in");
ofstream g("date.out");
int n;
struct film
{
float a, b;
int numar;
}v[100], aux;
int main()
{
f>>n;
for(int i=1;i<=n;i++)
{
f>>v[i].a>>v[i].b;
}
int ok=1;
do{
for(int i=1;i<=n;i++)
{
if(v[i].a>v[i+1].a)
{
aux=v[i];
v[i]=v[i+1];
v[i+1]=aux;
ok=0;
}
}
}while(ok==0);
for(int i=1;i<=n;i++)
{
cout<<v[i].a<<" "<<v[i].b<<endl;
}
return 0;
}
|
once ok is set to 0 it never becomes equal to 1. put int ok=1;
inside the do-while loop.
Topic archived. No new replies allowed.