In: Computer Science
Write a C program that creates a structure and displays its content. • Create a struct that will be used to hold a student's name, age, and year in school (Freshman, Sophomore, Junior, or Senior) • Within function main, dynamically allocate space to hold the structure and assign a pointer to point to the memory space allocated • Read in (from the keyboard) the student's name, age, and year in school • Create a separate function with the prototype: void display (struct student *) that can be used to display the contents of a single structure • Call this function twice - once for the original contents of the structure and again when the structure has been modified (Display year in school as indicated above, not 1, 2, 3, 4) • Increase the student's age by one and upgrade their year in school one level (unless they are already a Senior) • Free up the memory space before exiting
Answer:)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct student {
char name[64];
int age;
char yearin[64]
};
void display (struct student *ptr,int n){
printf("%s\t%d\n", (ptr+n)->name,
(ptr+n)->age,(ptr+n)->yearin);
}
int main() {
struct student *ptr;
int i, numberofrecords,n;
printf("Enter number of records for students: ");
scanf("%d", &numberofrecords);
ptr = (struct student*) malloc (numberofrecords * sizeof(struct
student));
printf("Enter name, age and year in school of the students
respectively:\n");
for(i = 0; i < numberofrecords; ++i)
{
scanf("%s%d%s", &(ptr+i)->name, &(ptr+i)->age,
&(ptr+i)->yearin);
}
printf("enter the student number(roll number) whose details you
want see ");
scanf("%d",&n);
display (ptr,n);
printf("change the content of records");
for(i = 0; i < numberofrecords; ++i)
{
printf("Enter name, age and year in school of the student
respectively:\n");
scanf("%s%d%s", &(ptr+i)->name, &(ptr+i)->age,
&(ptr+i)->yearin);
}
printf("enter the student number(roll number) whose details you
want see after change ");
scanf("%d",&n);
display (ptr,n);
for(i = 0; i < numberofrecords; ++i){
(ptr+i)->age=(ptr+i)->age+1;
if((ptr+i)->yearin=='Freshman')
printf("upgrade to Sophomore");
scanf("%s",&(ptr+i)->yearin);
if( (ptr+i)->yearin=='Sophomore')
printf("upgrade to junior");
scanf("%s",&(ptr+i)->yearin);
if((ptr+i)->yearin=='Junior')
printf("upgrade to junior");
scanf("%s",&(ptr+i)->yearin);
}
free(ptr);
/* Enter your code here. Read input from STDIN. Print output to
STDOUT */
return 0;
}