#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Need help in c programing .


    Hi Guys i need help with this question . i already try to do many time but, i cant figure it out wat is the problem

    this is the question
    Write a complete C program to accomplish each of the following. Assume that all the manipulations occur in main (therefore, no addresses of pointer variables are needed), and assume the following definitions:

    struct gradeNode
    {
    char lastName[20];
    float grade;
    struct gradeNode *nextPtr;
    }

    typedef struct gradeNode GRADENODE;
    typedef GRADENODE *GRADENODEPTR;

    • Create a pointer to the start of the linked-list called startPtr. The list is empty initially.
    • Create a new node of type GRADENODE that is pointed to by a pointer newPtr of type GRADENODEPTR. Assign the string “Johan” to member lastname and the value 91.5 to member grade (use strcpy). Provide any necessary declarations and statements.
    • Assume that the list pointed to by startPtr currently consists of 2 nodes – one containing “Johan” and on containing “Samsiah” with member grade 78. The nodes are in alphabetical order. Provide the statements necessary to insert the nodes containing the following data:

    “Aarthi” 85.0
    “Tan Chong” 73.5
    “Priscilla” 66.5

    Use pointers previousPtr, currentPtr, and newPtr to perform the insertions. Print out what previousPtr and currentPtr point to before each insertion. Assume that newPtr always points to the new node, and that the new node has already been assigned the data.
    • Using a loop, printout the data in each node of the list. Use pointer currentPtr to move along the list.
    • Using another loop, delete all the nodes in the list and free the memory associated with each node. Use currentPtr and pointer tempPtr to walk along the list and free memory, respectively.

    Make sure that your program is well-documented and proper variable names are used at all times.
    this is my answer but i cant compile it and run

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        struct gradeNode
        {
            char lastName[20];
            float grade;
            struct gradeNode *nextPtr;
        };
    
        typedef struct gradeNode GRADENODE;
        typedef GRADENODE *GRADENODEPTR;
    
    
        GRADENODEPTR startPtr=NULL;
        GRADENODEPTR newPtr=NULL;
        GRADENODEPTR previousPtr=NULL;
        GRADENODEPTR currentPtr=NULL;
        GRADENODEPTR tempPtr=NULL;
    
    
        /*Node 1*/
        newPtr = malloc(sizeof(GRADENODEPTR*));
        strcpy(newPtr->lastName,"Johan");
        newPtr->grade = 91.5;
        newPtr->nextPtr = NULL;
    
    
        startPtr = newPtr;
    
    
    
        /*Node 2*/
        newPtr = malloc(sizeof(GRADENODE));
        strcpy(newPtr->lastName,"Samsiah");
        newPtr->grade = 78;
        startPtr->nextPtr = newPtr;
    
    
    
    
        /*Node 3*/
        newPtr = malloc(sizeof(GRADENODE));
        strcpy(newPtr->lastName,"Aarthi");
        newPtr->grade = 85.0;
    
    
        previousPtr=NULL;
        currentPtr=startPtr;
    
    
        newPtr->nextPtr = currentPtr;
        startPtr = newPtr;
    
    
    
    
        /*Node 4*/
        newPtr = malloc(sizeof(GRADENODE));
        strcpy(newPtr->lastName,"Tan Chong");
        newPtr->grade = 73.5;
    
    
        previousPtr = (startPtr->nextPtr)->nextPtr;
        currentPtr=NULL;
    
    
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
    
    
    
    
    
    
        /*Node 5*/
        newPtr = malloc(sizeof(GRADENODE));
        strcpy(newPtr->lastName,"Priscilla");
        newPtr->grade = 66.5;
    
    
        previousPtr = startPtr->nextPtr;
        currentPtr = (startPtr->nextPtr)->nextPtr;
    
    
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
    
    
    
    
        /*Printing the nodes*/
        currentPtr = startPtr;
        while(currentPtr!=NULL){
            printf("Lastname = %s\nGrade = %.1f\n\n",currentPtr->lastName,currentPtr->grade);
            currentPtr = currentPtr->nextPtr;
        }
    
    
        /*Deleting the nodes*/
        currentPtr = startPtr;
        while(currentPtr != NULL){
            tempPtr=currentPtr;
            currentPtr=currentPtr->nextPtr;
            free(tempPtr);
        }
        
    
        
        return 0;
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481

    Line incorrectly written


    /*Node 1*/
    newPtr = malloc(sizeof(GRADENODEPTR*));


    should be

    /*Node 1*/
    newPtr = malloc(sizeof(GRADENODE));
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    /*Node 1*/
    newPtr = malloc(sizeof(GRADENODEPTR*));


    should be

    /*Node 1*/
    newPtr = malloc(sizeof(GRADENODE));

    I already change but this cannot compile :confused: :confused: :confused:
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Code:
    $ gcc -Wall c.c -o c
    $
    $ ./c
    Lastname = Aarthi
    Grade = 85.0
    
    Lastname = Johan
    Grade = 91.5
    
    Lastname = Priscilla
    Grade = 66.5
    
    Lastname = Samsiah
    Grade = 78.0
    
    Lastname = Tan Chong
    Grade = 73.5
    $
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    umhh sorry to say .. im bit confuse in which part i need to change . coz im still new in this programing language . can u pls help me with more clear detail.... thx in advance :tntworth: :tntworth:
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        struct gradeNode
        {
            char lastName[20];
            float grade;
            struct gradeNode *nextPtr;
        };
    
        typedef struct gradeNode GRADENODE;
        typedef GRADENODE *GRADENODEPTR;
    
    
        GRADENODEPTR startPtr=NULL;
        GRADENODEPTR newPtr=NULL;
        GRADENODEPTR previousPtr=NULL;
        GRADENODEPTR currentPtr=NULL;
        GRADENODEPTR tempPtr=NULL;
    
    
        /*Node 1*/
        /***************** newPtr = malloc(sizeof(GRADENODEPTR*)); ******original***********/
        newPtr = malloc(sizeof(GRADENODE));
        strcpy(newPtr->lastName,"Johan");
        newPtr->grade = 91.5;
        newPtr->nextPtr = NULL;
    
    
        startPtr = newPtr;
    
    
    
        /*Node 2*/
        newPtr = malloc(sizeof(GRADENODE));
        strcpy(newPtr->lastName,"Samsiah");
        newPtr->grade = 78;
        startPtr->nextPtr = newPtr;
    
    
    
    
        /*Node 3*/
        newPtr = malloc(sizeof(GRADENODE));
        strcpy(newPtr->lastName,"Aarthi");
        newPtr->grade = 85.0;
    
    
        previousPtr=NULL;
        currentPtr=startPtr;
    
    
        newPtr->nextPtr = currentPtr;
        startPtr = newPtr;
    
    
    
    
        /*Node 4*/
        newPtr = malloc(sizeof(GRADENODE));
        strcpy(newPtr->lastName,"Tan Chong");
        newPtr->grade = 73.5;
    
    
        previousPtr = (startPtr->nextPtr)->nextPtr;
        currentPtr=NULL;
    
    
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
    
    
    
    
    
    
        /*Node 5*/
        newPtr = malloc(sizeof(GRADENODE));
        strcpy(newPtr->lastName,"Priscilla");
        newPtr->grade = 66.5;
    
    
        previousPtr = startPtr->nextPtr;
        currentPtr = (startPtr->nextPtr)->nextPtr;
    
    
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
    
    
    
    
        /*Printing the nodes*/
        currentPtr = startPtr;
        while(currentPtr!=NULL){
            printf("Lastname = %s\nGrade = %.1f\n\n",currentPtr->lastName,currentPtr->grade);
            currentPtr = currentPtr->nextPtr;
        }
    
    
        /*Deleting the nodes*/
        currentPtr = startPtr;
        while(currentPtr != NULL){
            tempPtr=currentPtr;
            currentPtr=currentPtr->nextPtr;
            free(tempPtr);
        }
        
    
        
        return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    BUT STILL CANNOT COMPILE :confused: :confused:

    Code:
    http://i46.tinypic.com/33mmonm.png
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    The answer is to NOT compile your C program with a C++ compiler.

    Rename your source file to be something like prog.c (and not prog.cpp).
    Then add that source file to the project in your IDE (not forgetting to remove the old prog.cpp)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo