Problem with using and understanding arrays in structures and calling into Functions.

I do not know how to go about sorting a variable that was declared within a structure when that variable was not made into an array to begin with... if that makes sense?

So in order to sort a variable within a struct, do I need to make it into an array? If so, what do I need to put within [] if nothing is being stored to begin with?

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
   struct Info      ***Info c in main function;
{
    int ID;         
    string name;
    int scores;
};

 void sorting(ifstream &inFile, Info &c)
 {
     int i;
     int j;
     int first;
     int temp;

       for (int i= n-1; i >= 0; i--)
      { 
        first = 0;                 
        for (int j=0; j<=i; j++)  
        {
            if (c[j].ID < c[first].ID)
            first = j;
        }
        temp = c[first].ID;   
        c[first].ID = c[i].ID;
        c[i].ID = temp;

        cout << setw(20) << fixed << setprecision(2) << temp << endl;
    }
Last edited on
So in order to sort a variable within a struct...

Every possible collection of a single element is always sorted completely.

So you need to answer -- what are you sorting the value of that variable relative to? You need to group everything that you wish to sort together somehow -- using an array or any other data structure you can think of.

If I had to make a guess, I would suggest that you are sorting instances of Info -- individual bits of information. Those instances need to be grouped together, and then you can access each instances' ID independently.

Note:
Arrays store values, not variables. This is because each element of an array is a variable -- and variables can only store values.
Each instance of Info has its' own member variables with corresponding values. If you create two Info objects, each one has a different ID variable, for instance (even if the values in each were the same).
Last edited on
Would I need a nested structure to be able to access the ID independently? Would that make sense?
Last edited on
Topic archived. No new replies allowed.