# Help with my Permutation Program (several parts, sorry =[)

• 02-06-2010
Soulzityr
Help with my Permutation Program (several parts, sorry =[)
I need to write a permutation program which takes in a set x number of males and females where they rate each other and then the program will organize them into a pattern of best possible score;

the program works like this

Matt, Dave, Alan; Heather, Sara, Joan

Matt- 3,5,6
Dave- 5,6,8
Alan- 4,2,5

Heather-6,7,8
Sara-4,6,5
Joan-4,2,3

The program will calculate each possible matching to get best possible score; this is achieved by taking the lower rating between the two people from each couple and adding them together to achieve a total score; highest total score is the match, ties are broken by subtracting differences

For example, Matt&Heather- 3, Dave&Sara- 4, Alan&Joan-4; Total score: 11; permute; Matt&Heather - 3, Dave&Joan- 3, Alan&Sara-6; Total Score: 12; permute...

---

My current problem lies in calculating the TotalMax score at the moment. What I'm attempting to do is make a method that calculates TotalMax. This code is going to get the total of a particular set. My first question is about whether or not the (int) casting is correct for p and q[i].ratings. I think what I'm doing is casting an int on the pointer? I believe it's incorrect but as my main is still suffering issues I can't really test this out yet.

The struct Matcher has char name[19] (person's name) and int* ratings (their ratings of the opposite sex) to represent each person; in my main i have created struct Matcher males[] and struct Matcher females[]

compareRate returns -1 if p[i].ratings[i] is smaller than q's, 0 if even, 1 if bigger

Code:

```//this is to get total score for one set of matches; it will return the //score of the points added together; k=size of array p and q which are equal int getMatchTotal(struct Matcher p[], struct Matcher q[], int k) {     int i,total;     for(i=0;i<k;i++) {       if(compareRate(p[i],q[i],i)==-1)           total+=(int)p[i].ratings;       else           total+=(int)q[i].ratings;     }     return total; }```
---

My second problem is the permutating part. I have to permute an n amount of people, which is all read from a file, with names and ratings, etc. I'm trying to write a method that needs to permute both the names of the people in the second list(im using females) and the ratings stored in each person at the same time, but I'm having trouble putting this code together. Is a hint possible?

---

I also have a third problem in the main

Code:

```      assign each male their ratings       int j;       for(dud=0;dud<num_people;dud++) {         for(j=0;j<num_people;j++) {             fscanf(read,"%d",&males[dud].ratings[j]);             printf("%d ",males[dud].ratings[j]);           }           printf("\n");       }```
i dont know why but this crashes my program...cant figure it out, help please? :)
• 02-06-2010
Soulzityr
can anyone please give me a little advice? =/ ive been writing this for about half a week and am currently stuck as it is...i dont know how to proceed, as I cant tell if my method is right and why my main is crashing because the double for loop is just supposed to store ints into the arrays inside the structs...any pointers? sorry i am a beginner...
• 02-06-2010
NeonBlack
Code:

```    for(i=0;i<k;i++) {       if(compareRate(p[i],q[i],i)==-1)           total+=(int)p[i].ratings;       else           total+=(int)q[i].ratings;     }```
You say that Matcher.ratings is an int*, which you shouldn't be adding to total, which is an int. I'm guessing you got a warning and added the cast, which you shouldn't have done. I'm not entirely sure what you're trying to do here, but you want to be working with elements of the ratings array.
• 02-06-2010
Soulzityr
hmm, yes i got a typecast error, but that is wrong? what i want to do is ratings will store many ints, but i need to add those points in order to total and then return total;

i will try to edit it. as for the main, do you have any idea why it crashes?

---

Okay, I made the code for the method like this

Code:

```//this is to get total score for one set of matches; it will return the //score of the points added together; k=size of array p and q which are equal int getMatchTotal(struct Matcher p[], struct Matcher q[], int k) {     int i,total;     for(i=0;i<k;i++) {       if(compareRate(p[i],q[i],i)==-1)           total+=p[i].ratings[i];       else           total+=q[i].ratings[i];     }     return total; }```
My intention is that i will permeate the ratings for each male at the same time as i permeate the female list, that way this method will take the correct value every time
---

would it be beneficial to post the whole code? I havent because I'm not trying to throw my code out and tell you guys to do it, haha sorry If I'm coming up this way. This assignment is due tomorrow at midnight and currently I'm just stuck on making a permeate method and storing the number values in the ints.
• 02-06-2010
Quote:

Originally Posted by Soulzityr
I need to write a permutation program which takes in a set x number of males and females where they rate each other and then the program will organize them into a pattern of best possible score;

the program works like this

Matt, Dave, Alan; Heather, Sara, Joan

Matt- 3,5,6
Dave- 5,6,8
Alan- 4,2,5

Heather-6,7,8
Sara-4,6,5
Joan-4,2,3

The program will calculate each possible matching to get best possible score; this is achieved by taking the lower rating between the two people from each couple and adding them together to achieve a total score; highest total score is the match, ties are broken by subtracting differences

For example, Matt&Heather- 3, Dave&Sara- 4, Alan&Joan-4; Total score: 11; permute; Matt&Heather - 3, Dave&Joan- 3, Alan&Sara-6; Total Score: 12; permute...

---

My current problem lies in calculating the TotalMax score at the moment. What I'm attempting to do is make a method that calculates TotalMax. This code is going to get the total of a particular set. My first question is about whether or not the (int) casting is correct for p and q[i].ratings. I think what I'm doing is casting an int on the pointer? I believe it's incorrect but as my main is still suffering issues I can't really test this out yet.

The struct Matcher has char name[19] (person's name) and int* ratings (their ratings of the opposite sex) to represent each person; in my main i have created struct Matcher males[] and struct Matcher females[]

compareRate returns -1 if p[i].ratings[i] is smaller than q's, 0 if even, 1 if bigger

Code:

```//this is to get total score for one set of matches; it will return the //score of the points added together; k=size of array p and q which are equal int getMatchTotal(struct Matcher p[], struct Matcher q[], int k) {     int i,total;     for(i=0;i<k;i++) {       if(compareRate(p[i],q[i],i)==-1)           total+=(int)p[i].ratings;       else           total+=(int)q[i].ratings;     }     return total; }```
---

My second problem is the permutating part. I have to permute an n amount of people, which is all read from a file, with names and ratings, etc. I'm trying to write a method that needs to permute both the names of the people in the second list(im using females) and the ratings stored in each person at the same time, but I'm having trouble putting this code together. Is a hint possible?

---

I also have a third problem in the main

Code:

```      assign each male their ratings       int j;       for(dud=0;dud<num_people;dud++) {         for(j=0;j<num_people;j++) {             fscanf(read,"%d",&males[dud].ratings[j]);             printf("%d ",males[dud].ratings[j]);           }           printf("\n");       }```
i dont know why but this crashes my program...cant figure it out, help please? :)

First, in your example, you took the lowest ranking of each individual in that pair, and assigned it to them, but not for Alan & Joan, which each had a low score of 2, but you gave them a 4. ??

This doesn't sound like a permutation problem at all. It sounds like you need to sort and select and do some simple arithmetic on your data, according to some specific logic.

A set with all permutations would give you every possible grouping. A set with all combinations would give you every possible grouping, irrespective of order.

Please back up and post another example or two, because so far, I can't make out what you're trying to do in this program.

As for the program, I believe it's fair to say that after 3 problems in the program, that it's time to post some example of it (as small as possible is helpful), that shows the problem.

I would like to confirm first however, that the program is going to do something that is what you can actually use.
• 02-06-2010
Soulzityr
Oh, I screwed up with the example, it should be a 2 yes you are right

--

and mhmm i do think there is an easier way ot do it; however, my teacher says its required of us to use a permutation method in our program; the thing being permeated will be the total combinations of couples possible, i guess; heheh.

---

Okay, here is the example, and later the program

Let’s go through a simple example with 3 men and 3 women:

Let the men’s names be Adam, Bob and Carl, and the women’s names be Diana, Ellen and Fran.

The following chart shows how much the men like the women:

----- Diana Ellen Fran
Bob -- 6 --7 -- 5
Carl -- 5--9 -- 6

This chart shows how much the women like the men:

Diana--7 -- 6 -- 8
Ellen --6 -- 5 -- 9
Fran -- 4 -- 7 -- 3

Now, let’s look at the six possible matchings and their scores

12-18-15-16-19-14

In this situation, the best matching is the fifth one placing Adam with Fran, Bob with Diana, and Carl with Ellen, for a “likeability” quotient of 19.

Had there been a tie, we would have broken it by looking at the differences in the matching. For matching #5, the difference between Adam and Fran is 3, Bob and Diana is 0, and Carl and Ellen is 0, for a total difference of 3+0+0 = 3.

---

this is my program

Code:

``` //Jason Shih #include <stdio.h> #include <stdlib.h> #include <string.h> struct Matcher {   char name[19];   int* ratings;     //stores a list of names in order of int* ratings   //with their corresponding rated values   char lists[10][19]; }; void permuteScores(struct Matcher males[], struct Matcher females[]); void recursivePermute(struct Matcher males[], struct Matcher females[], int k); int getMatchTotal(struct Matcher p[], struct Matcher q[],int k); int compareRate(struct Matcher p, struct Matcher q, int k); void permuteScores(struct Matcher males[], struct Matcher females[]) {   } //this is to get total score for one set of matches; it will return the //score of the points added together; k=size of array p and q which are equal int getMatchTotal(struct Matcher p[], struct Matcher q[], int k) {     int i,total;     for(i=0;i<k;i++) {       if(compareRate(p[i],q[i],i)==-1)           total+=p[i].ratings[i];       else           total+=q[i].ratings[i];     }     return total; } //returns -1 if p has lower rating, returns 0 if its equal //and 1 if q has lower rating; parameter k to set where in array they are located int compareRate(struct Matcher p, struct Matcher q, int k) {     if(p.ratings[k]>q.ratings[k])       return 1;     else if(p.ratings[k]<q.ratings[k])       return -1;     return 0; } int main() {     FILE *read;     read=fopen("matching.txt","r");     printf("1\n");     int n;     fscanf(read,"%d",&n);     int k,num_people,dud;     printf("2\n");     for(k=0;k<n;k++) {       printf("beginning of loop: %d\n",k);       fscanf(read,"%d",&num_people);       printf("num of people: %d\n",num_people);       struct Matcher males[num_people];       struct Matcher females[num_people];       printf("\n\n");             //attempt to read and print a single name of males       for(dud=0;dud<num_people;dud++) {           fscanf(read,"%s",&males[dud].name);           males[dud].ratings[num_people];           printf("%s\n",&males[dud].name);       }       //-------------------------------------------------------------------       printf("\n\nmales done\n\n");             //assign and print females       for(dud=0;dud<num_people;dud++) {           fscanf(read,"%s",&females[dud].name);           printf("%s\n",&females[dud].name);       }       printf("\n\nfemales done\n\n");       /*       int j;       for(dud=0;dud<num_people;dud++) {         for(j=0;j<num_people;j++) {             fscanf(read,"%d",&males[dud].ratings[j]);             printf("%d ",&males[dud].ratings[j]);           }           printf("\n");       }       */     }     printf("\n\n\n");     system("PAUSE");     return 0; }```
• 02-06-2010
Soulzityr
i just dont get why my main crashes...=[
• 02-06-2010
NeonBlack
Because you haven't allocated any memory for "ratings" in your struct.
• 02-07-2010
NeonBlack
I also want to mention that in my opinion, you've written too much (non-working) code.

What I suggest you do is start completely over. Write code in small chunks (a few lines at a time), making sure that it is correct at each step.
First make sure that you can read input correctly before doing anything else.
• 02-07-2010
Soulzityr
hmm well idk, my main works really well up into the point, and thank you for pointing out that i didnt initialize size. sorry, i guess im really bad at this...but yeah i really hope i dont have to rewrite. that took me 2-3 days to get to of light working on it throughout the day and i only have one more day to get this assignment

i mean theoretically all i have left to do, since my main will now correctly read info, is get the permute method working right? heh
• 02-07-2010
Soulzityr
Quote:

Originally Posted by NeonBlack
Because you haven't allocated any memory for "ratings" in your struct.

I initialized the array both dynamically and statically but the program still crashes =/ did i do it wrong? and i thought i did initialize it, in the male one at least

Code:

```      for(j=0;j<num_people;j++) {           males[dud].ratings= (int*) malloc (sizeof(males[dud].ratings));           females[dud].ratings[10];                              }```
• 02-07-2010
NeonBlack
I know that you put a lot of work into this code, but I think you will gain a lot by rewriting it.
1. Rewriting code is one of the best ways to learn from your past mistakes.
2. I honestly think that there are so many problems with what you've written that it would be less work to rewrite it than to fix everything that's wrong.
• 02-07-2010
Soulzityr
lolol that sounds painful, but alright; i will take ur advice and rewrite it......hopefully it will...work better.

is there any advice on the many things i did wrong? like just a heads up so i dont make the same mistakes??
• 02-07-2010
NeonBlack
Good luck. Remember to build up your program slowly and don't write more than a few lines of code without compiling and testing to verify that they actually work.
• 02-07-2010
Soulzityr
lol well I'm glad I took your advice. My code now is much cleaner and my main correctly reads everything. Thanks for your help so far :) Currently though, I am still confused by the logic of permutations, lol, and writing a recursive method that not only switches up the order of the females correctly but also captures the right int values

Code:

```//Jason Shih #include <stdio.h> #include <stdlib.h> #include <string.h> int main() {   FILE *read;   read=fopen("matching.txt","r");   int n,group,k,j;   fscanf(read,"%d",&n);   printf("%d\n",n);   //for(j=0;j<n;j++) {       fscanf(read,"%d",&group);       char males[group][19], females[group][19];       int mrates[group][group], frates[group][group];       for(k=0;k<group;k++) {         fscanf(read,"%s",&males[k]);                          printf("%s\n",males[k]);       }       for(k=0;k<group;k++) {         fscanf(read,"%s",&females[k]);         printf("%s\n",females[k]);                        }       int a,b;       for(a=0;a<group;a++) {         for(b=0;b<group;b++) {         fscanf(read,"%d",&mrates[a][b]);         printf("%d-",mrates[a][b]);            }         printf("\n");       }       for(a=0;a<group;a++) {         for(b=0;b<group;b++) {             fscanf(read,"%d",&frates[a][b]);             printf("%d-",frates[a][b]);                          }         printf("\n");       }         //}     system("PAUSE");   return 0; }```