Shorting 2 corresponding arrays? Please help


Hey guys im trying sort my Calorie array and day array corresponding to each other but i have no idea how to start i cant grasp the concept for some reason.

i want it to sort by calories in descending order.

so instead of this...

Day: Calories:

1 200
2 500
3 400
4 600
5 100

I want this.....

5 100
1 200
3 400
2 500
4 600

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <cstdlib>
#include <iostream>

using namespace std;

//Prototypes
int getDays(int);
void getCal(int*,int);
float calcSum(float,int*,int);
float calcAvg(float,int);
void sortRay(int*,int*,int);


int main(int argc, char *argv[])
{
    int days;
    int *calorie;
    int *day;
    float sum = 0;
    float average = 0;
    
    days = getDays(days);
    day = new int[days];
    calorie = new int[days];
          
    getCal(calorie,days);
   
    sum = calcSum(sum,calorie,days);
    
    average = calcAvg(sum,days);
    
    cout << days << endl;
    cout << sum << endl;
    cout << showpoint << average << endl;
    for (int i = 0; i < days; i++)
        {
        cout << calorie[i] << endl;
        }
        
    system("PAUSE");
    return EXIT_SUCCESS;
}



//******************************************************************************
int getDays(int days)
{
    cout << "Please type how many days to track for the month 1-31" << endl;
    cin >> days;
    while (days <= 0 || days > 31)
          {
                cout << "please provide a valide amount" << endl;
                cin >> days;
          }
    return days;
}
//******************************************************************************

void getCal(int* calorie,int days)
{
     for (int i = 0; i < days; i++)
        {
          cout << " How many calories where taken? " << endl;
          cin >> calorie[i];   
        } 
     
}    
//******************************************************************************

float calcSum(float sum, int* calorie,int days)
{
 int average;
  for (int i = 0; i < days; i++)
         {
              sum+=calorie[i];
         }  
  
  return sum;
    
}
//******************************************************************************

float calcAvg(float sum,int days)
{
      float average = 0;
      average = (float)sum / (float)days;
      return average;
}
//******************************************************************************
    
void sortRay(int* day, int* calorie,int days)
{

}
First I would suggest that you hold both the day number and calories for that day in the same object:

1
2
3
4
5
6
struct Data
{
   Data() : day(0), calories(0) {}
   int day;
   int calories;
};


Then hold all the data in a std::map, using the day as the index, this will then sort your data on the day index:

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
#include <map>

using namespace std;

struct Data
{
   Data() : day(0), calories(0) {}
   int day;
   int calories;
};

int main()
{
   typedef map<int, Data> DATA_TYPE;
   DATA_TYPE mydata;

   // declare instance:
   Data data;
	
   // TO DO - add some data
   // code ...

   // insert into map, which will automatically sort on index:
   mydata.insert(DATA_TYPE::value_type(data.day, data));

   // rest of code etc...

   return 0;
}
Last edited on
Topic archived. No new replies allowed.