Thread: Infinite Loop/ General Help

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    39

    Infinite Loop/ General Help

    What I'm trying to do is create a schedule generator for a fantasy football league. The key things that need to happen are:
    -- that all 10 teams have to play every other team in the league once.
    -- every team will also play 4 teams twice (since it is a 13 week schedule)

    So they system I've come up with (probably not the best) is two find a set of numbers whose factors are unique no matter which two numbers you use. I used 21-30. There factors then become a matchup number. I'm having trouble when making sure that every team has only four match-ups with other teams.

    Not sure if I'm being clear enough.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #define MAX_LENGTH 30
    
    
    typedef struct Team_struct{
        int num;
        int count;
        char name[MAX_LENGTH];
        struct Team_struct *next;
    }teams;
    
    
        char *titles[10] = {"Team1", "Team2", "Team3", "Team4", "Team5", "Team6", "Team7", "Team8", "Team9", "Team10"};
    
        int arrCount[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int used[100];
        int usedTwice[50];
        int teamNums[10]= {21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
        int utIndex = 0;
        int usedIndex = 0;
        teams *head = NULL;
    
    void week(int weekNum);
    void calculateMatchups(int array[], int week);
    void updateTeamCount(int array[], int i);
    int getRandom(teams *ptr);
    teams * insertNode(void);
    teams * createNode(int data);
    int checkCount(int num1, int num2);
    int a = 0;
    
    
    int main(void)
    {
        srand(time(NULL));
        int weekNum = 1;
        int i;
        used[0]  = 0;
        char* text = "ffoutput.xls";
        FILE * output = fopen(text, "w");
        
        head = insertNode();
        
        for(weekNum = 1; weekNum <= 13; weekNum++)
            week(weekNum);
    
        for(i = 0; i < 50; i++)
        {
            printf("Used[%d]: %d UsedTwice[%d]: %d\n", i, used[i], i, usedTwice[i]);
        }
    
    
        while(head != NULL)
        {
            printf("Team Number: %d\t Team Name: %s\t Team Count: %d\n", head->num, head->name, head->count);
            head = head->next;
        }
    
        
        //    printf("team1: %d team 2: %d team3: %d team 4: %d\n", team1count, team2count, team3count, team4count);
            
        printf("arrCount:\n");
        for(i = 0; i < 10; i++)
            printf("[%d]", arrCount[i]);
    
        return 0;
    }
    
    int checkCount(int num1, int num2)
    {
    
    
        teams * temp = head;
        teams * prev = head;
        int ans = 0;
        int i,j,k,l,m, n;
        
        for(i = 0; i < 10; i++)
        {
            if(num1 == teamNums[i])
            {
                j = i;
                i =    10;
                
                k = 1;
            }
        }
        if(k == 1)
        {
            if(arrCount[j] == 4)
                ans = 1;
        }
        for(i = 0; i < 10; i++)
        {
            if(num2 == teamNums[i])
            {
                j = i;
                i =    10;
                
                k = 1;
            }
        }
        if(k == 1)
        {
            if(arrCount[j] == 4)
                ans = 1;
        }
        return ans;
        
    /*
        while(temp != NULL || ans == -1)
        {
            if(temp->num == num1 || temp->num == num2 && temp->count == 4)
            {
                ans = -1;
                return ans;
            }
            else
            {
                temp = temp->next;
                ans = 0;
            }
        }
        return ans;
    */
    }
    
    
    void week(int weekNum)
    {
        int weeklyUsed[10];
        int random;
        int i;
        int j = 0;
        int res = 1;
        int times = 0;
    //    teams * ptr = NULL;
        for(i = 0; i < 10; i+=2)
        {
                do{
    
                    weeklyUsed[i] = getRandom(head);
                    weeklyUsed[i + 1] = getRandom(head);
                    //printf("%d %d", weeklyUsed[i], weeklyUsed[i+1]);
                for(j = 0; j <= i; j++)
                {
                    if(i == j)
                    {
                        if(weeklyUsed[j] == weeklyUsed[i + 1])
                        {
                            res = 0;
                            j = i;
                        }
                        else
                        {
                            res = 1;
                        }
                    }
                    if(i != j)
                    {
                        if(weeklyUsed[j] == weeklyUsed[i] || weeklyUsed[j] == weeklyUsed[i + 1])
                        {
                            res = 0;
                            j = i;
                        }
                        else
                        {
                            res = 1;
                        }
                    }    
                }
            }while(weeklyUsed[i] == weeklyUsed[i + 1] || res == 0);
        //    updateTeamCount(weeklyUsed[i], weeklyUsed[i + 1]);
        }
        calculateMatchups(weeklyUsed, weekNum);
    
    }
    
    void calculateMatchups(int array[], int weekNum)
    {
        int i;
        int j = 0;
        int k;
        int matchup;
        int res = 1;
        int val = 1;
        int usedCount = 0;
        int utCount = 0;
        int success[10];
        int successCount = 0;
    
        for(i = 0; i < 10; i++)
            printf("%d ", array[i]);
            printf("\n");
    
        for(i = 0; i < 10; i +=2)
        {
            val = 1;
            matchup = array[i] * array[i+1];
            res = 1;
    
            printf("Teams: %d %d Matchup #: %d\n", array[i], array[i + 1], array[i] * array[i+1]);
            
        
            if(usedIndex == 0)
            {
                used[0] = matchup;
                usedIndex++;
                res = 0;
            }
            else
            {
                for(j = 0; j < usedIndex; j++)
                {
                    if(used[j] == matchup)
                    {
                        j = usedIndex;
                        val = 0;
                    /*    if(utIndex == 0)
                        {
                            usedTwice[0] = matchup;
                            utIndex++;
                        } */
                        for(k = 0; k < utIndex; k++)
                        {
                            if(usedTwice[k] == matchup)
                            {
                                k = utIndex;
                            //    printf("%d THREE TIMES\n", matchup);
                                utIndex -= utCount;
                                usedIndex -= usedCount;
                                i = 11;
                            //    printf("ran\n");
                                week(weekNum);
                                
                                res = 0;
                                return;
                            }
                        }
                            
                            if(res != 0)
                            {
                            
                                if(checkCount(array[i], array[i + 1]) == 1)
                                {
                                    week(weekNum);
                                    i = 11;
                                }
                                
                                usedTwice[utIndex] = matchup;
                                utIndex++;
                                utCount++;
                                success[successCount] = array[i];
                                successCount++;
                                success[successCount] = array[i + 1];
                                successCount++;
                                
                            }
                    }
    
                }
                        if(val != 0)
                        {
                            used[usedIndex] = matchup;
                            usedIndex++;
                            usedCount++;
                        }
                        //if(successCount != 10)
                            //updateTeamCount(success);
    
            }
    
        }
                if(successCount != 0)
                            updateTeamCount(success, successCount);
                        
    }
    
    
    void updateTeamCount(int array[], int i)
    {
    
        int j, k;
        char* text = "ffoutput.xls";
        FILE * output = fopen(text, "w");
        
        for(j = 0; j < i; j++)
        {
            for(k = 0; k < 10; k++)
            {
                if(array[j] == teamNums[k])
                    arrCount[k]++;
            
            }
        }
        /*
        printf("%d\n", a);
        a++;
        
        teams * temp = head;
        if(i <  10)
        {
            while(temp != NULL)
            {
                for(k = 0; k < i; k++)
                {
                    if(temp->num == array[k])
                    {
                            temp->count++;
                    }
                }
                    temp = temp->next;
                
            }
        }
        */
    }
    
    
    int getRandom(teams * ptr)
    {
        teams * temp = ptr;
        teams * temp2 = ptr;
        int count = 0;
        int random;
        int i;
        while(temp != NULL)
        {
            count++;
            temp = temp->next;
        }
        random = rand() % count;
        for(i = 0; i < random; i++)
            temp2 = temp2->next;
    
        return temp2->num;
    }
    
    teams * insertNode(void)
    {
        int i;
        teams * newTeam = NULL;
        teams* temp2 = NULL;
        teams * temp = NULL;
        
        for(i = 0; i < 10; i++)
        {
        
        //    temp2 = newTeam;
            temp = createNode(i);
            /*
            if(newTeam == NULL)
                newTeam = temp;
            else
            {
                while(temp2->next != NULL)
                {
                    temp2 = temp2->next;
                }
                temp2->next = temp;
                temp2->next = NULL;
            }
            newTeam = temp2;
            */
        
            temp->next = newTeam;
    
            newTeam = temp;
        
        }
        
        return newTeam;
    }
    
    teams * createNode(int data)
    {
        teams * temp = malloc(sizeof(teams));
        temp->num = teamNums[data];
        strcpy(temp->name, titles[data]);
        temp->count = 0;
        temp->next = NULL;
        
        return temp;
    }
    The bolded section gives me an infinite loop only when I call week(weekNum). Not sure how to fix this or if the program just never generates enough unique results. Please help in any way you can. There's a lot of commented out code and printf's from me trying to figure out problems and experimenting with new ways.

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    39
    The commented out portion in updateCount and checkCount are how I had it with the struct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  2. Infinite loop!!!
    By Bizmark in forum C Programming
    Replies: 3
    Last Post: 02-21-2009, 12:09 PM
  3. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  4. Infinite loop
    By osal in forum Networking/Device Communication
    Replies: 1
    Last Post: 06-08-2004, 04:18 PM
  5. infinite loop
    By bruce in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2002, 04:35 PM