The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Need help in c programing .
Discuss Need help in c programing . in the C Programming forum on Dev Shed. Need help in c programing . C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 17th, 2012, 06:12 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
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;
}
|

November 17th, 2012, 08:07 PM
|
 |
Contributing User
|
|
|
|
|
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!
|

November 17th, 2012, 08:43 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
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 
|

November 17th, 2012, 10:55 PM
|
 |
Contributing User
|
|
|
|
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
$
|

November 17th, 2012, 11:29 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
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 
|

November 17th, 2012, 11:37 PM
|
 |
Contributing User
|
|
|
|
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;
}
|

November 17th, 2012, 11:56 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 25 m 59 sec
Reputation Power: 0
|
|
BUT STILL CANNOT COMPILE
Code:
http://i46.tinypic.com/33mmonm.png
|

November 18th, 2012, 12:33 AM
|
 |
Contributed User
|
|
|
|
|
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)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|