HELP! Data structures

Hello, so i need quick help with putting stuff in order. i have a text file:
6
v k 250
m k 280
v p 240
m p 290
v s 63
m s 45
i need to to make this into descending order by numbers so it would look like this :
m p 290
m k 280
v k 250
v p 240
v s 63
m s 45
Here's what i came up with, but program crashes
1
2
3
4
5
6
7
8
9
10
  for(int b=0; b<n; b++)
        for(int j=0; j+1<n;j++)
    {
        if(A[j].kaina>A[b].kaina) // kaina == price
        {
             A[j].kaina = B[b].kaina;
             A[j].lytis = B[b].lytis; // lytis == sex
             A[j].tipas = B[b].tipas;; // tipas == type of the clothes 
        }
    }
Last edited on
You haven't shown enough conntent. For example how did you define your structures and how did you define your arrays?

Also what exactly are you trying to accomplish in that if statement.


IT looks like you're trying a bubble sort. At lines 6-8, you probably need to swap A[j] and A[b] rather than copying A[b] to A[j]. Also the loop at line 2 should probably be for (int j=0; j < b; ++j)

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
struct drabuziai //clothes
{
    char lytis;
    char tipas;
    int kaina;
};
void veiksmai(drabuziai A[],drabuziai B[], int n);
int main()
{
    drabuziai A[256];
    drabuziai B[256];
    int n;
    duomenys(A,n); //text file stuff
    veiksmai(A,B,n); //actions
    return 0;
}
void veiksmai(drabuziai A[],drabuziai B[], int n)
{
    int kostiumai=0, paltai=0,striukes=0,k=0,p=0,s=0;
    int vyr=0,mot=0;

    for(int b=0; b<n; b++)
        for(int j=0; j<n;j++)
    {
        if(A[j].kaina>A[b].kaina)
        {
             A[j].kaina = B[b].kaina;
             A[j].lytis = B[b].lytis;
             A[j].tipas = B[b].tipas;
        }
    }

this should do for code, and for
what exactly are you trying to accomplish in that if statement.

if the price is higher, then it should be at the top of the list, it's just an exercise to put this list in decsending order.

If it's easyer you can show how to sort list like this in other example. the point is that it should decsend by number
Last edited on
Well assigning values to A from B seems wrong to start with. It looks like B is empty, unless there are some other "hidden" functions.

To sort your array of structures you can swap the entire structure at once, you don't need to swap all the individual elements of the structure, as stated by dhayden above.

you can swap the entire structure at once

And how do i do that?
1
2
3
4
        if(A[j].kaina>A[b].kaina)
        {
             std::swap(A[j], A[b]);
        }
Topic archived. No new replies allowed.