Sorting Help

How could I arrange the results of fats and proteins in ascending and descending?

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
  #include <iostream> 
#include <iomanip> 


int main()
{
    std::cout << "\nGRAMS OF FAT" << std::endl;
  //Arrays and variables
  std::string Days[5] = {"Day 1: ", "Day 2: ", "Day 3: ", "Day 4: ", "Day 5: "};
   int         GFats[5],
               total;
    
    //USER ENTERS NUMBERS TO DAYS           
    for (int i = 0; i < 5; ++i)
  {
    int Fats;
    std::cout << "Enter number of grams of fat consumed on " << Days[i];
    std::cin >> Fats;
    GFats[i] = Fats;
  }
    
    std::cout << std::endl << "Total Fats:" << GFats[0]+GFats[1]+GFats[2]+GFats[3]+GFats[4] << std::endl;
    
    std::cout << "\nPROTEINS" << std::endl;
    //second array
    std::string Days_2[5] = {"Day 1: ", "Day 2: ", "Day 3: ", "Day 4: ", "Day 5: "};
   int         GProteins[5],
               total_2;
      //USER ENTERS NUMBERS TO DAYS           
    for (int i = 0; i < 5; ++i)
  {
    int Proteins;
    std::cout << "Enter number of proteins consumed on " << Days_2[i];
    std::cin >> Proteins;
    GProteins[i] = Proteins;
  }
  
  std::cout << std::endl << "Total proteins:" << GProteins[0]+GProteins[1]+GProteins[2]+GProteins[3]+GProteins[4] << std::endl;   

    std::cout << "\nRESULTS" << std::endl;
  std::cout << "Total proteins:" << GProteins[0]+GProteins[1]+GProteins[2]+GProteins[3]+GProteins[4] << std::endl;   
  std::cout << "Total Fats:" << GFats[0]+GFats[1]+GFats[2]+GFats[3]+GFats[4] << std::endl;
}
I'm going to assume you mean smallest number to largest number for this; This seems like an assignment, so I'll let you implement it yourself, but a very easy way to sort, in this case, bubble sorting, can be used:

1
2
3
4
5
6
7
8
9
10
11
12
for(int j = 0, temp = 0; j < (arraySize); j++)
{
    for(int i = 0; i < (arraySize-1); i++)
    {
        if(array[i] > array[i+1])
        {
            temp = array[i+1];
            array[i+1] = array[i];
            array[i] = temp;
        }
    }
}


The idea is that you can run through an array of values, and "swap" the smaller ones with larger ones a number of times until they are sorted appropriately.

The above is called a bubble sort, very simple, not always the most efficient, but for smaller programs like this it could be implemented and used. You should also check out sorting methods like selection sort, merge sort, etc etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
// ...
#include <algorithm>

int main()
{
    int GFats[5];
    // ...
    int GProteins[5];

    // ...
    std::sort(GFats, GFats + 5);
    std::sort(GProteins, GProteins + 5);
}

https://en.cppreference.com/w/cpp/algorithm/sort
Is that what you want?
Why do you have used for each item a different array? If you sort one of this arrays, you need to re-arrange the order of the others. A better way would be, grouping all items of a record together, maybe into a struct.
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
#include <iostream>
#include <string>
#include <algorithm> // for std::sort

struct Day
{
     std::string day_name;
     int fat;
     int proteine;
};

int main()
{
    Day record_v[5];
    for(auto & r : record_v)
    {
        std::cin >> r.day_name >> r.fat >> r.proteine;
    }
    
    std::cout << "Proteins of a day in ascending order:";
    std::sort( std::begin(record_v),
               std::end(record_v),
               [](Day& a, Day& b)->bool{return a.proteine < b.proteine;}
    );
    for( auto & r : record_v)
    {
        std::cout << "Day " << r.day_name
                  << ", fat: " << r.fat
                  << ", proteine: " << r.proteine
                  << std::endl;
    }
}
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
#include <iostream> 
#include <iomanip> 

int main()
{
    std::cout << "\nGRAMS OF FAT" << std::endl;
  //Arrays and variables
  std::string Days[5] = {"Day 1: ", "Day 2: ", "Day 3: ", "Day 4: ", "Day 5: "};
   int         GFats[5],
               total;
    
    //USER ENTERS NUMBERS TO DAYS           
    for (int i = 0; i < 5; ++i)
  {
    int Fats;
    std::cout << "Enter number of grams of fat consumed on " << Days[i];
    std::cin >> Fats;
    GFats[i] = Fats;
  }
    
    
    
    for (int i = 0; i!=5; i++)
	{
	   std::cout  << std::endl <<Days[i] << GFats[i];
	}
    
    std::cout << std::endl << "Total Fats:" << GFats[0]+GFats[1]+GFats[2]+GFats[3]+GFats[4] << std::endl;
    
    std::cout << "\nPROTEINS" << std::endl;
    //second array
    std::string Days_2[5] = {"Day 1: ", "Day 2: ", "Day 3: ", "Day 4: ", "Day 5: "};
   int         GProteins[5],
               total_2;
      //USER ENTERS NUMBERS TO DAYS           
    for (int i = 0; i < 5; ++i)
  {
    int Proteins;
    std::cout << "Enter number of proteins consumed on " << Days_2[i];
    std::cin >> Proteins;
    GProteins[i] = Proteins;
  }
  
  for (int i = 0; i!=5; i++)
	{
	  std::cout << std::endl << Days_2[i] << GProteins[i];
	}
	
  std::cout << std::endl << "Total proteins:" << GProteins[0]+GProteins[1]+GProteins[2]+GProteins[3]+GProteins[4] << std::endl;   

    std::cout << "\nRESULTS" << std::endl;
  std::cout << "Total proteins:" << GProteins[0]+GProteins[1]+GProteins[2]+GProteins[3]+GProteins[4] << std::endl;   
  std::cout << "Total Fats:" << GFats[0]+GFats[1]+GFats[2]+GFats[3]+GFats[4] << std::endl;
  

}


I added this to have each day with the number that the user inputs
1
2
3
4
for (int i = 0; i!=5; i++)
	{
	  std::cout << std::endl << Days_2[i] << GProteins[i];
	}



GRAMS OF FAT                                                                                                                                                                         
Enter number of grams of fat consumed on Day 1: 5                                                                                                                                    
Enter number of grams of fat consumed on Day 2: 5                                                                                                                                    
Enter number of grams of fat consumed on Day 3: 5                                                                                                                                    
Enter number of grams of fat consumed on Day 4: 5                                                                                                                                    
Enter number of grams of fat consumed on Day 5: 5                                                                                                                                    
                                                                                                                                                                                     
Day 1: 5                                                                                                                                                                             
Day 2: 5                                                                                                                                                                             
Day 3: 5                                                                                                                                                                             
Day 4: 5                                                                                                                                                                             
Day 5: 5                                                                                                                                                                             
Total Fats:25         


So according to Tduck I could use a bubble sort to sort the values of the days right?
and I have to use arrays, I would have preferred using other method but :(
Topic archived. No new replies allowed.