-2

I am quite new to programming, and I have tried restlessly to figure out how to do this, but Alas I have yet to find anything. In my program, I have a struct. It is a sport sim, so the struct looks a bit like this

struct Team{

string name;
int reputation;

}

I have over one-hundred teams declared in an array

Team team[120];

I need something to arrange these teams by their reputation, and I need the program to be able to figure out who the top four teams (based on reputation) are to put them in the playoffs.

I am still new to programming, so any help is appreciated!

Troy Cox
  • 5
  • 1
  • 2
    Sorting is about comparing, and in C++ that's done by implementing a comparator function, either to compare two teams, or where team has `operator<` to compare to another team. – tadman Feb 11 '21 at 02:06
  • Assuming tis is an assignment and you cannot simply overload `operator<` (see [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706) for some help on doing that) and use [`std::sort`](https://en.cppreference.com/w/cpp/algorithm/sort) to let the C++ Standard Library do the grunt work for you, Wikipedia has several pages on notable sorting algorithms, [here's the page for Quicksort](https://en.wikipedia.org/wiki/Quicksort), that contain pseudocodes you can adapt to your purposes. If you get stuck, bring code and you'll get better answers. – user4581301 Feb 11 '21 at 02:27
  • 1
    Step 1: Implement a comparator function of some kind. Step 2: Pick a [sorting algorithm](https://en.wikipedia.org/wiki/Sorting_algorithm) to implement if `std::sort` is not an option. Step 3: Write *tests* for your code with something like [catch2](https://github.com/catchorg/Catch2) so you can be sure your sorting works correctly under a variety of use cases, like an empty list, a list with 1000 items, a list with all items the same, etc. – tadman Feb 11 '21 at 02:30
  • Step 4: step through the program with the debugger that came with your development tools and keep an eye out for the unexpected, like the wrong value being stored or the program taking the wrong path, to help you find errors because there will almost certainly be errors. It's a joyous day when you write something complicated like a sorting algorithm and it works perfectly the first time. – user4581301 Feb 11 '21 at 02:34

1 Answers1

-1

in your structs you have the member

int reputation;

assuming this is your "way to arrange them" you could declare a nested loop that iterate throug your array to find which team have the highest reputation

for the outer loop to iterate through each team. and the inner loop to iterate through each team reputation"to find the highest reputation".

Edit : Code example

for(int i = 0 ; i < team.size();i++){ // outer loop
    for(int j = i ; j < team.size();j++){ // inner loop
         if(team[i].reputation > team[j].reputation) // forgot it was a struct
             //switch them
    }
}
Boubla
  • 94
  • 1
  • 3
  • Could you give a code example? – Troy Cox Feb 11 '21 at 02:24
  • 2
    Is this like a really imprecise description of Bubble Sort? – tadman Feb 11 '21 at 02:29
  • @tadman yes it is. – Boubla Feb 11 '21 at 02:33
  • 2
    careful with the range of the loops. A bubble sort usually stops when it can make a pass though the container and make no swaps. Worst case it swaps every element with every other element, but usually it can kick out much sooner than that. [Here's how wikipedia implements it](https://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation) – user4581301 Feb 11 '21 at 02:39
  • You're using the quirky new C++ initializer style here, which can be confusing for some, and also Bubble Sort is *the worst* example to start with. There are other sorts that are only incrementally more complicated, but vastly faster. – tadman Feb 11 '21 at 02:45
  • 1
    @tadman edited ! yes you are right, it was the first one that popped out of my head at that time and i thought that it was good enought since he said he was new to programming. – Boubla Feb 11 '21 at 02:47
  • 1
    The `int i = 0` method has been in textbooks for decades, it's very familiar. The `int i { 0 }` method is still pretty new, and I'm not sure why it's necessary, it just takes up more space and involves more typing. To each their own, of course, but a little explanation can help. – tadman Feb 11 '21 at 02:48