Question

Q 1.

Complete the function createStudent below: #include <stdio.h> #include <stdlib.h> struct student { char name[50]; int age; stFor example: Test Input Result Zhangwei Student Zhangwei is 20 years old. 20 scanf(%s %d, myName, &myAge); studptr createSt

Q 2.Complete the function appendStudent below: struct student * appendStudent(struct student * end, struct student * newStudptr)

if it works, I'll thumb up for u.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q1. code:

#include<stdio.h>
#include<stdlib.h>
struct student {
char name[50];
int age;
struct student *next;
};
struct student * createStudent(char studentName[], int studentAge)
{
struct student *temp = (struct student *)malloc(sizeof(struct student)); //creating empty node to store the student details.
strcpy(temp->name, studentName); //copying student name into structure name variable.
temp->age = studentAge; //copying student age into structure age variable.
temp->next = NULL; //storing reference next variable to null.
return temp; //returning created node to main function.
}
int main()
{
struct student *studptr;
int myAge;
char myName[50];
scanf("%s %d", myName, &myAge);
studptr = createStudent(myName, myAge);
printf("Student %s is %d years old.\n", studptr->name, studptr->age);
free(studptr);
return 0;
}

Image of code:

#include<stdio.h> #include<stdlib.h> Estruct student { char name (50); int age; struct student *next; -}; struct student * cr

output:

Zhangwei 20 Student Zhangwei is 20 years old. Process returned (@xo) execution time : 28.027 s Press any key to continue.

Q2 code:

#include<stdio.h>
#include<stdlib.h>
struct student {
char name[50];
int age;
struct student *next;
};
struct student * createStudent(char studentName[], int studentAge)
{
struct student *temp = (struct student *)malloc(sizeof(struct student)); //creating empty node to store the student details.
strcpy(temp->name, studentName); //copying student name into structure name variable.
temp->age = studentAge; //copying student age into structure age variable.
temp->next = NULL; //storing reference next variable to null.
return temp; //returning created node to main function.
}
struct student * appendStudent(struct student * end, struct student * newStrudptr)
{
end->next = newStrudptr; //giving the link to end student node to new student node.
end = end->next; //changing the end variable to last node i.e last student node.
return end; //returning last node.
}
int main()
{
struct student *start, *newStudptr, *end;
char name1[50], name2[50], name3[50];
int age1, age2, age3;
scanf("%s %s %s", name1, name2, name3);
scanf("%d %d %d", &age1, &age2, &age3);

start = createStudent(name1, age1);
end = start;
newStudptr = createStudent(name2, age2);
end = appendStudent(end, newStudptr);
newStudptr = createStudent(name3, age3);
end = appendStudent(end, newStudptr);
printf("Student %s is %d years old.\n", start->name, start->age);
printf("Student %s is %d years old.\n", start->next->name, start->next->age);
printf("Student %s is %d years old.\n", start->next->next->name, start->next->next->age);
return 0;
}

Image of code:

1 2 3 4 5 #include<stdio.h> #include<stdlib.h> struct student { char name [50]; int age; struct student *next; 6 struct stude30 31 32 33 34 35 36 37 38 39 40 start = createStudent (namel, agel); end = start; newStudptr = createStudent (name2, age2);

output:

Zhangwie John Jerry 15 40 25 Student Zhangwie is 15 years old. Student John is 40 years old. Student Jerry is 25 years old. e

Note: my friend if you have any questions or queries comment below. I am happy to answer your all questions. I will sort out your queries. Thank you my friend.

Add a comment
Know the answer?
Add Answer to:
Q 1. Q 2. if it works, I'll thumb up for u. Complete the function createStudent...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

  • Hello, I am having trouble with a problem in my C language class. I am trying...

    Hello, I am having trouble with a problem in my C language class. I am trying to make a program with the following requirements: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct {     char name[MAX_SIZE1];     int scores[MAX_SIZE2]; } 3. The program accepts from the command line an integer n (<= 30) as the number of students in the class. You may assume that the...

  • Write a function to insert a name at the end of a linked list? (C) I...

    Write a function to insert a name at the end of a linked list? (C) I have a linked list: struct node { char person[100]; struct node *next; }; int main() { struct node *new_node; new_node=malloc(sizeof(struct node)); printf("Please enter a name:\n"); scanf("%s", ); } How do I get the name from the user and write a function to add it to the end of the linked list? I'm not supposed to use the insert() library function, which is why I'm...

  • Write a statement that calls a function named IncreaseItemQty, passing the variable addStock. Assign notebookInfo with...

    Write a statement that calls a function named IncreaseItemQty, passing the variable addStock. Assign notebookInfo with the value returned by IncreaseItemQty. #include <stdio.h> #include <string.h> typedef struct ProductInfo_struct {    char itemName[30];    int itemQty; } ProductInfo; ProductInfo IncreaseItemQty (ProductInfo productToStock, int increaseValue) {    productToStock.itemQty = productToStock.itemQty + increaseValue;    return productToStock; } int main(void) {    ProductInfo notebookInfo;    int addStock = 10;    scanf("%s", notebookInfo.itemName);    scanf("%d", &notebookInfo.itemQty);    /* Your solution goes here */    printf("Name:...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • Given the following program: #include <stdio.h> struct student { int id; char name[20]; char grade; };...

    Given the following program: #include <stdio.h> struct student { int id; char name[20]; char grade; }; void func(struct student stud); int main() { struct student astud; astud.id=9401; strcpy(astud.name, "Joe"); astud.grade = 'A'; func(astud); return 0; } Abdelghani Bellaachia, CSCI 1121 Page: 16 void func(struct student astud) { printf(" Id is: %d \n", astud.id); printf(" Name is: %s \n", astud.name); printf(" Grade is: %c \n", astud.grade); } Modify this program to include the address of a student as a separate structure....

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...

    Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){    char str[1000], ch;    int i, frequency = 0;    clock_t t; struct timespec ts, ts2;       printf("Enter a string: ");    gets(str);    int len = strlen(str);    printf("Enter a character...

  • Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...

    Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session. This is my program so far, when I run the program I keep getting...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT