How can I minimize this code?

How can I minimize this code. My prof has told me to fix this but I dont know how to.
The swap in the sorts can and should use object assignment so that you can do the swap in 3 steps.


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
//sort
void sortage(Juventus team[], int n)
{

     bool swapped = true;
     int j = 0, temp, temp5, temp6;
     string temp2, temp3, temp4;

     while (swapped) {
          swapped = false;
          j++;

          for (int i = 0; i < n - j; i++) {
               if(team[i].personal.age>team[i+1].personal.age) {
               
               temp=team[i].personal.age;               
               temp2=team[i].personal.first;
               temp3=team[i].personal.last;
               temp4=team[i].player.position;
               temp5=team[i].player.goals;
               temp6=team[i].player.assists;
               
               team[i].personal.age = team[i+1].personal.age;                            
               team[i].personal.first = team[i+1].personal.first;
               team[i].personal.last = team[i+1].personal.last;
               team[i].player.position = team[i+1].player.position;
               team[i].player.goals = team[i+1].player.goals;
               team[i].player.assists = team[i+1].player.assists;
               
               team[i + 1].personal.age=temp;
               team[i+1].personal.first = temp2;
               team[i+1].personal.last = temp3;
               team[i+1].player.position = temp4;
               team[i+1].player.goals = temp5;
               team[i+1].player.assists = temp6;
               
               swapped = true;
               }
          }
     }
     return;
}


I was also told this
ERROR: printone is using a global data structure. It should receive the single object to print as a parameter, rather than receiving the subscript as a parameter. You must declare the array of object inside main. Class definitions are global, but not actual variables.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//search function
void findlastname(Juventus team[], int n, string lastname)
{
     
     for(int count=0;count<n;count++){
          if(team[count].personal.last==lastname)
               printone(count);
          
     }
          return;
}
//printone
void printone(int count){

    
     cout<<"First name: "<<team[count].personal.first<<endl;
     cout<<"Last name: "<<team[count].personal.last<<endl;
     cout<<"Age: "<<team[count].personal.age<<endl;
     cout<<"Player Position: "<<team[count].player.position<<endl;
     cout<<"Goals scored this season: "<<team[count].player.goals<<endl;
     cout<<"Assists this season: "<<team[count].player.assists<<endl<<endl;
     
     return;
}
use object assignment
1
2
3
temp = team[i];               
team[i] = team[i + 1];                            
team[i + 1] = temp;


To get rid of the global variables you instead pass them in as arguments.
what do you mean by passing them as argument?
I understand that printone is supposed to take an object of type Juventus (i.e. void printone(Juventus j)) instead of relying on a global array of these and taking an int named count as a parameter. That means that line 7 of your second code snippet would look more like:
printone(team[count]);

What Peter meant by passing something as an argument is to call a function with that something in a proper place between the parentheses. All your functions seem like they can already take an array of Juventuses...

Example:
sortage(team, 20);

EDIT: Ninjas.

-Albatross
Last edited on
ok i get the [printone(); function. And i have now edited the sort function, but now I am getting error codes

cannot convert `Juventus' to `int' in assignment

note: candidates are: Juventus& Juventus::operator=(const Juventus&)

no match for 'operator=' in '*((((Juventus*)(((unsigned int)i) * 24u)) + team) + 24u) = temp'
Um... could you post the updated code, please?

-Albatross
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//sort
void sortage(Juventus team[], int n)
{

     bool swapped = true;
     int j = 0, temp;

     while (swapped) {
          swapped = false;
          j++;

          for (int i = 0; i < n - j; i++) {
               if(team[i].personal.age>team[i+1].personal.age) {
               
               temp = team[i];               
               team[i] = team[i + 1];                            
               team[i + 1] = temp;
               
               swapped = true;
               }
          }
     }
     return;
}
As suspected, but I just wanted to be sure. :)

temp needs to be of type Juventus.

-Albatross
How would I declare that? Did you mean "team" needs to be of type Juventus?
Juventus temp;
I actually got that. But now im getting errors in the printone() function
¿So?
cannot convert `Juventus' to `Juventus*' for argument `1' to `void printone(Juventus*)'
First paragraph http://cplusplus.com/forum/general/57985/#msg312307
¿what are you doing differently?
This is what I have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//search function
void findlastname(Juventus team[], int n, string lastname)
{
     
     for(int count=0;count<n;count++){
          if(lastname==team[count].personal.last)
               printone(&team);
          
     }
          return;
}
//printone
void printone(Juventus &team){

    
     cout<<"First name: "<<team.personal.first<<endl;
     cout<<"Last name: "<<team.personal.last<<endl;
     cout<<"Age: "<<team.personal.age<<endl;
     cout<<"Player Position: "<<team.player.position<<endl;
     cout<<"Goals scored this season: "<<team.player.goals<<endl;
     cout<<"Assists this season: "<<team.player.assists<<endl<<endl;
     
     return;
}


But i am now getting an error message invalid initialization of non-const reference of type 'Juventus&' from a temporary of type 'Juventus**'
Stop guessing. Think.
A wild printone(&team); appears. ¿what do you expect that line to do?

Albatross already told you the prototype and the call to that function.
If you just overloaded the assignment operator for your objects you could do a simple 3 line swap. If this is for school, why haven't you done that anyway? Show us you struct/class.
Last edited on
Topic archived. No new replies allowed.