Can some one help me with this..... C programming noob here.

This is a discussion on Can some one help me with this..... C programming noob here. within the C Programming forums, part of the General Programming Boards category; 1. Define a structure called entry that has at least three elds: a pointer to char (which will be used ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    32

    Can some one help me with this..... C programming noob here.

    1. Define a structure called entry that has at least three elds: a pointer to char (which will be used as a string), an integer counter, and a pointer to another entry structure. Write a function called new entry that accepts a string as an argument, then allocates a new entry structure and initializes it with a copy of the string passed to it. For this you will need to allocate space both for the structure and for the copy of the string that it is to point to (using two separate calls to malloc). Initialize the other elds too, as appropriate, and return a pointer to the new entry structure as the function value. Note: the function new entry should place no limit on the size of the string it can hold, since it is to allocate as much space as it needs to hold the string passed to it.

    2. Write a function called add to dictionary that accepts a single string argument (i.e. pointer to char), and does one of two things with that string. If the string has already been added to the \dictionary", then it will simply increment the counter associated with that word. Otherwise, it will add the string to the dictionary. Ordering is unimportant here, so you can add the a new entry structure at the beginning or at the end of the existing list. Either way, you will need to maintain a global variable that points to the rst entry of the dictionary.

    3. Write a function called make dictionary that accepts a single string argument, which it assumes is the name of a text file to open. It should open the le using fopen and read one character at a time using getc until it returns EOF (end of file). The function make dictionary should look for sequences of alphabetic characters delimited by spaces, punctuation marks, newlines,



    This is what i have for part 1 and 2....need help with part 3


    Code:
    #include <stdio.h> //calling library
    #include <math.h> //calling library
    #include <stdlib.h>
    #include <string.h>
    
    struct entry 
    {
    	char *word;
    	int counter;
    	struct entry *next_entry;
    } *dictionary, *first; 
    
    entry* new_entry(char*s)
    { 
    	struct entry *x = NULL;
    	x = (entry*) malloc(sizeof(entry)); 
    	x->word=(char*)malloc(strlen(s)); 
    	x->counter=0;
    	strcpy(x->word,s);
    	x->next_entry = NULL;
    	return x;
    }
    
    void add_to_dictionary( char *word )
    {
      struct entry *temp;
      struct entry *newEntry;
      
      // Look for a match for "word"
      for(temp = first; temp != NULL; temp = temp->next_entry)
      {
        if(strcmp(temp->word, word) == 0)
        {
          // Increment the counter
          temp->counter++;
    
          // No reason to go on
          return;
        }
      }
    
      // If we got here, we did not find a match so add one
      newEntry = new_entry(word);
      
      // this is the first entry
      if(first == NULL)
      {
        first = newEntry;
      }
      else
      {
        // Find the last entry
        temp = first;
        while(temp->next_entry != NULL) 
        {
          temp = temp->next_entry;
        }
        temp->next_entry = newEntry;
      }  
    }
    
    void make dictionary( char *fname )
    { //what should i put here?}
    
    int main()
    {
    
    }



    So i managed to to get help and finish part 1 and 2, however i am not sure how to start part 3.... can some one help me out with this?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First of all you want to test these things one at a time... first get your user input working, then add the string to the strcture and get that working then build your linked list and test that. Don't try to write the whole program then compile it later... many sorrows lie down that road as you are very likely to be hit with a torrent of errors and problems all at once.

    Second since this isn't your own code, be aware that professors do search the internet for similar code samples and you will lose credit for the assignment if they find it...

    Ok... so get to work on part 3, write the code best you can and if you run into troble you can post it up here and ask specific questions.

    Homework Policy

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    Quote Originally Posted by CommonTater View Post
    First of all you want to test these things one at a time... first get your user input working, then add the string to the strcture and get that working then build your linked list and test that. Don't try to write the whole program then compile it later... many sorrows lie down that road as you are very likely to be hit with a torrent of errors and problems all at once.

    Second since this isn't your own code, be aware that professors do search the internet for similar code samples and you will lose credit for the assignment if they find it...

    Ok... so get to work on part 3, write the code best you can and if you run into troble you can post it up here and ask specific questions.

    Homework Policy


    How do i test it one at a time? i know i can debug it, but how would i know if it is okay and would work?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok take an obvious example... at some point you're going to have to get input from the keyboard or a file... So, write that code and simply have it print to screen so you can see that it's getting the data correctly. Once you have that working, you need to set up the dictonary which looks like it's asking for a linked list... so get it so you can create the first struct test it, make sure it's loading correctly... then add the code to add a new struct and check that you now have that working... and so on.

    Working that way is a whole lot simpler than trying to debug an entire program at once. At worst you will know a problem exists in the last bit of code you entered... so you probably only have to look at 10 or 15 lines to find the problem.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    Quote Originally Posted by CommonTater View Post
    Ok take an obvious example... at some point you're going to have to get input from the keyboard or a file... So, write that code and simply have it print to screen so you can see that it's getting the data correctly. Once you have that working, you need to set up the dictonary which looks like it's asking for a linked list... so get it so you can create the first struct test it, make sure it's loading correctly... then add the code to add a new struct and check that you now have that working... and so on.

    Working that way is a whole lot simpler than trying to debug an entire program at once. At worst you will know a problem exists in the last bit of code you entered... so you probably only have to look at 10 or 15 lines to find the problem.
    so you mean write a printf and scanf statement the code i have now to test it? can you give me an example?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Nope, no examples.... It's apparent you aren't grasping the concept, so you should just carry on as you were...

    Make your best try at your #3 ... if you get stuck, post your code and maybe we can help you...
    But I'm not going to write it for you...

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    because you said have it print to screen, i didn't quite get what you meant by this...

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    Quote Originally Posted by CommonTater View Post
    Nope, no examples.... It's apparent you aren't grasping the concept, so you should just carry on as you were...

    Make your best try at your #3 ... if you get stuck, post your code and maybe we can help you...
    But I'm not going to write it for you...

    Your alt + f4 keeps redirecting me to another website.... how to I change this back ...

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm talking about a programming process where you build your code in small sections, testing each section as you go rather than writing the the whole thing at once and having to deal with hundreds of simultaneous errors.

    In a small think like you're doing it makes very little difference... but you don't ever want to write a 50,000 line project, spanning multiple source and header files, without testing as you go... it would drive you crazy.

    The idea was to help you form good programming habits early, so they stay with you...
    For now, just forget I said anything and get your homework turned in on time.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kkmoslehpour View Post
    Your alt + f4 keeps redirecting me to another website.... how to I change this back ...
    LOL... now THAT is funny...

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    How do i fix the redirection url = = not funny if you cant go on facebook lol
    Last edited by kkmoslehpour; 04-27-2011 at 11:58 AM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kkmoslehpour View Post
    How do i fix the redirection url = = not funny if you cant go on facebook lol
    Oh boy... go to facebook and make a new shortcut for it.

    (And trust me... no facebook = no loss)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-02-2010, 01:41 PM
  2. Help on my C programming (noob)
    By gnozahs in forum C Programming
    Replies: 5
    Last Post: 09-17-2009, 07:48 AM
  3. Noob to programming need help with a project
    By Wheelsonbus in forum C Programming
    Replies: 6
    Last Post: 02-25-2009, 02:46 AM
  4. noob need help with programming question!
    By kraze_101 in forum C Programming
    Replies: 12
    Last Post: 11-02-2005, 05:23 PM
  5. im a programming noob, having trouble with arrays
    By scoot in forum C Programming
    Replies: 5
    Last Post: 05-07-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21