Linked List/Recursion/Word Scrambling in C. Help Please

No errors or warnings on compiling but the program isn't doing a t

Page 1 of 1

7 Replies - 2427 Views - Last Post: 29 September 2010 - 04:07 PM Rate Topic: -----

#1 TorontoKnuckles  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 24-January 10

Linked List/Recursion/Word Scrambling in C. Help Please

Posted 28 September 2010 - 04:35 PM

Ok so my task is to make a program where the word carbon is scramble in every arrangement possible and printed to the screen with no duplicates. So I thought theoretically this is how my program would work: I'd make a linked list, make a function to scramble the word carbon, have another function search the list for a duplicate and if there is no duplicate to add the word to the linked list. Also if there is a duplicate found it'll call the makeList function again to rescramble the word and try again. Therefore a rearrangement of the word carbon won't be added to the list unless it's an original arrangement.

I'm getting no errors or warnings but it's not working at all I have no idea where the issue is either :( :huh: Anyways here's the codes and I've added comments so you know what I'm doing or at least TRYING to do with each function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//Structure for the linked list, the data which would be the string word and node next.
struct node { 
    char *word;
    struct node *next;
};

typedef struct node datatype; //Making struct node under type "datatype"

char *rearrangeWord(char *str) //Function to scramble the word carbon
{
    int i, a, b;
    for(i=0;i<6;i++){
        a = rand() % 6;
        b = rand() % 6;
        if(a==B)/>{
            i--;
            continue;
        }
        char temp = str[a];
        str[a] = str[b];
        str[b] = temp;
    }
    return str;
}

void printWords(datatype *root) //Function to print words, traverse list & print to screen
{
    datatype *temp;
    temp = root;

    while (temp != NULL){
        printf("%s\n", temp->word);
        temp = temp->next;
        }
}

void freeList(datatype *root) // Function to free list, traverse list and free memory
{
    datatype *temp;
    while(root != NULL){
        temp = root;
        root = temp->next;
        free(temp);
        }
}

int searchList(datatype *root, char *carb)//Function to search list for duplicates.
{
    int FAIL, SUCCESS;
    datatype *temp;
    
    FAIL = 1;
    SUCCESS = 3;
    
    while (root != NULL){
        temp = root;
        root = temp->next;
        
        if(strcmp(temp->word, carb) == 0){ //Returns 1 if there's a duplicate
            return FAIL;
            }
        }
    
    if (root == NULL){
        return SUCCESS; //If root has reached NULL with no duplicates then it worked
        }               //and returns 3 to show that the search was successful 
}
        

datatype *makeList(datatype *root, char *carb) //Function to make the list and uses
{                                              //the search list & word scramble functions
    datatype *temp;
    char *phrase;
    
    temp = malloc(sizeof(struct node));
    temp->next = root;
    
    phrase = malloc(sizeof(char)*7);
    temp->word = malloc(sizeof(char)*7);
    strcpy(phrase, carb); //Carbon is passed into this function, and then into
                          // the scramble function
    
    rearrangeWord(phrase);
    
    if (searchList(root, phrase) == 3){ //So if search returns 3, then it SUPPOSED
                                        //to add that word to the list
        strcpy(temp->word, phrase);
        return root;
        }
    
    else if (searchList(root, phrase) != 3){ //Recursion to start function over
                                             //if there was a duplicate
        makeList(root, phrase);
        }
}       


int main (void)
{
    datatype *root;
    char *str;
    
    root = NULL;
    
    str = malloc(sizeof(char)*7);
    strcpy(str, "carbon");

    int i;
    for (i=0;i<720;i++){ //720 b/c there's supposed to be 6! iterations of carbon
        makeList(root, str);
    }

    printWords(root);
    
    freeList(root);
    
    free(str);
    
    return 0;
}



Is This A Good Question/Topic? 1
  • +

Replies To: Linked List/Recursion/Word Scrambling in C. Help Please

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Linked List/Recursion/Word Scrambling in C. Help Please

Posted 28 September 2010 - 05:34 PM

1 - Where are you seeding your random number generator?
http://www.dreaminco...random-numbers/

2 - "It's not working" is not a bug report.
Please give us some more details of your problem.
( a ) Does your code compile? < We can infer it does. >
( b ) Any errors or warnings? If there are then share them with us. < You say no. But see below.>
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details and, ideally, examples.
( e ) What have you already tried to fix it?

I do get warnings thrown
dic.c: In function ‘searchList’:
dic.c:71: warning: control reaches end of non-void function
dic.c: In function ‘makeList’:
dic.c:99: warning: control reaches end of non-void function


These are being presented because of the slightly strange choices you have made about how your code is laid out.
If you have a loop that spins while "root is not NULL" there is no need to test for "root is NULL" after that.
The only way your code can progress is if "root is NULL" so you already know the answer to your question.

Also have a look at your indentation and brace ("{","}") placements.
Are things laid out as they should be?
Was This Post Helpful? 0
  • +
  • -

#3 TorontoKnuckles  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 24-January 10

Re: Linked List/Recursion/Word Scrambling in C. Help Please

Posted 28 September 2010 - 06:25 PM

I tried to compile in a different program and I got one warning "a1.c(70): warning #2235: Not all control paths return a value." This is in Pelles C. But anyways when I compile it I get no output it just goes straight to press any key to continue. I removed the other unnecessary if statements in the searchList function and the makeList function and I still get the same result. And as far as I know I don't have anything wrong with the braces but then again I've seen people use them differently so I could be wrong.

Did I initialize the list wrong? I don't even know if the list was made or not but I assume it wasn't because nothing is being printed to the screen.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Linked List/Recursion/Word Scrambling in C. Help Please

Posted 28 September 2010 - 06:43 PM

View PostTorontoKnuckles, on 29 September 2010 - 10:25 AM, said:

I removed the other unnecessary if statements in the searchList function and the makeList function and I still get the same result.

Please post your changed code so we are all working on the same code.


View PostTorontoKnuckles, on 29 September 2010 - 10:25 AM, said:

And as far as I know I don't have anything wrong with the braces


Compare this
        if(a==B)/>{
            i--;
            continue;
        }


With this
        if(strcmp(temp->word, carb) == 0){ //Returns 1 if there's a duplicate
            return FAIL;
            }
        }


Which of those two braces at the end of the second block of code closes the scope of the if()?
Why is it different to the placement in the first block of code from your own program?
Choose one indentation style and follow it consistently.



View PostTorontoKnuckles, on 29 September 2010 - 10:25 AM, said:

I don't even know if the list was made or not but I assume it wasn't because nothing is being printed to the screen.

As well as the atrocious alignment of the if else() that needs to be looked at let's put some code in your program that tells you what is happening.
    if (searchList(root, phrase) == 3){ //So if search returns 3, then it SUPPOSED
                                        //to add that word to the list
        printf("%s\n", "adding a word");
        strcpy(temp->word, phrase);
        return root;
        }


Now when you run the code every time you add a word to the list a message will display on the screen.
Does it ever display?

This post has been edited by janotte: 28 September 2010 - 06:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 TorontoKnuckles  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 24-January 10

Re: Linked List/Recursion/Word Scrambling in C. Help Please

Posted 28 September 2010 - 07:11 PM

    
    while (root != NULL){
        temp = root;
        root = temp->next;

        if(strcmp(temp->word, carb) == 0){
            return FAIL;
            }
        }


That second brace was to close the while loop because the if statement was nested in the while loop. But yes you're totally right for pointing out my inconsistency I didn't realize. Thanks.

This is the code I got now but without those comments because I put in those comments specifically for the post so you could understand what I was doing and my thought process.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
    char *word;
    struct node *next;
};

typedef struct node datatype;

char *rearrangeWord(char *str)
{
    int i, a, b;
    for(i=0;i<6;i++){
        a = rand() % 6;
        b = rand() % 6;
        if(a==B)/>{
            i--;
            continue;
        }
        char temp = str[a];
        str[a] = str[b];
        str[b] = temp;
    }
    return str;
}

void printWords(datatype *root)
{
    datatype *temp;
    temp = root;

    while (temp != NULL){
        printf("%s\n", temp->word);
        temp = temp->next;
    }
}

void freeList(datatype *root)
{
    datatype *temp;
    while(root != NULL){
        temp = root;
        root = temp->next;
        free(temp);
    }
}

int searchList(datatype *root, char *carb)
{
    int FAIL, SUCCESS;
    datatype *temp;

    FAIL = 1;
    SUCCESS = 3;

    while (root != NULL){
        temp = root;
        root = temp->next;

        if(strcmp(temp->word, carb) == 0){
            return FAIL;
        }
    }
        return SUCCESS;
}


datatype *makeList(datatype *root, char *carb)
{
    datatype *temp;
    char *phrase;

    temp = malloc(sizeof(struct node));
    temp->next = root;

    phrase = malloc(sizeof(char)*7);
    temp->word = malloc(sizeof(char)*7);
    strcpy(phrase, carb);

    rearrangeWord(phrase);

    if (searchList(root, phrase) == 3){
        
	printf("%s\n", "adding a word");

	strcpy(temp->word, phrase);
        return root;
    }

        makeList(root, phrase);
}


int main (void)
{
    datatype *root;
    char *str;

    root = NULL;

    str = malloc(sizeof(char)*7);
    strcpy(str, "carbon");

    int i;
    for (i=0;i<720;i++){
        makeList(root, str);
    }

    printWords(root);

    freeList(root);

    free(str);

    return 0;
}


Ok so now after that "adding a word" test it does output that adding a word string but still no rearrangements of the word carbon. So does this mean I made the list wrong?
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Linked List/Recursion/Word Scrambling in C. Help Please

Posted 28 September 2010 - 07:51 PM

Probably so let's try and get the program to tell us a bit more about its inner workings.
Let's expand our little debugging output line to:
int tempAdd = temp;
printf("adding a word - phrase = %s temp = %d\n", phrase, tempAdd);


Now we should get told what word is being added and the memory address of where you are putting it.
The address doesn't matter in itself but it had better be different for each save if we aren't just overwriting the same location each time.
Does that seem to be working?

Do you see a way to keep going through your code inserting little outputs like this to give you a window into what is happening at places you want to see into the machine's innards?
Obviously you throw these away when their usefulness is over.

Now it is better to use a debugging tool to achieve this but this approach is simple and powerful.
Was This Post Helpful? 0
  • +
  • -

#7 TorontoKnuckles  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 24-January 10

Re: Linked List/Recursion/Word Scrambling in C. Help Please

Posted 29 September 2010 - 11:04 AM

Okay so I put that test in and it appears to be working because it's showing the different rearrangements of the word carbon with different memory addresses every line. So theoretically that would lead me to believe something is wrong with my printList function. Maybe I'll just print the word from there and scratch the print line function in general? But I'd rather figure out my mistakes and correct them.. Anyways I tried taking out the "temp" from the printWords function, and just using the root and never worked I'm running out of ideas..
void printWords(datatype *root)
{
    while (root != NULL){
        printf("%s\n", root->word);
        root = root->next;
    }
}


but its really the exact same thing as doing it with the datatype *temp; in there..
void printWords(datatype *root)
{
    datatype *temp;
    temp = root;

    while (temp != NULL){
        printf("%s\n", temp->word);
        temp = temp->next;
    }
}


I'm lost for ideas. Isn't that the correct way to traverse the list?
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Linked List/Recursion/Word Scrambling in C. Help Please

Posted 29 September 2010 - 04:07 PM

Have you tried using the idea of little debug outputs to see your code working?
If you didn't, why not?
If you did, what did you learn that you can share with us?

If you are confident that you correctly written a traversal then what else could be wrong?
The code is completely dependent upon you passing in a good value for "root" is it not?
Have you checked you are passing in a good value for "root"?
Do you have an idea of how to test what you are passing in as "root"?

I am not giving you hints as to what the answer is. I am not analysing your code. I am simply giving you idea of things that you can do to analyse your own code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1