C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 17th, 2012, 06:12 PM
jvan jvan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 jvan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 59 sec
Reputation 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
Quote:
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;
}

Reply With Quote
  #2  
Old November 17th, 2012, 08:07 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,360 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 55 m 30 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old November 17th, 2012, 08:43 PM
jvan jvan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 jvan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 59 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #4  
Old November 17th, 2012, 10:55 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,360 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 55 m 30 sec
Reputation Power: 383
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
$

Reply With Quote
  #5  
Old November 17th, 2012, 11:29 PM
jvan jvan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 jvan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 59 sec
Reputation 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

Reply With Quote
  #6  
Old November 17th, 2012, 11:37 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,360 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 55 m 30 sec
Reputation Power: 383
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;
}

Reply With Quote
  #7  
Old November 17th, 2012, 11:56 PM
jvan jvan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 jvan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 59 sec
Reputation Power: 0
BUT STILL CANNOT COMPILE

Code:
http://i46.tinypic.com/33mmonm.png

Reply With Quote
  #8  
Old November 18th, 2012, 12:33 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 54 m 25 sec
Reputation Power: 1774
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need help in c programing .

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap