Question

In: Computer Science

Write a C program that creates a structure and displays its content. • Create a struct...

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

Solutions

Expert Solution

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;
}


Related Solutions

write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
Write a c program that creates a struct to be able to read a .img file...
Write a c program that creates a struct to be able to read a .img file and a .pat file.
Create a C++ program that creates instances of a Struct using an Array (you can choose...
Create a C++ program that creates instances of a Struct using an Array (you can choose the number of instances to create). You can also use either user input or an initialization list to initialize 3 peoples names. Make sure that the values stored in each member are printed to the screen.
Create a program that: Creates a sales receipt, displays the receipt entries and totals, and saves...
Create a program that: Creates a sales receipt, displays the receipt entries and totals, and saves the receipt entries to a file Prompt the user to enter the Item Name Item Quantity Item Price Display the item name, the quantity, and item price, and the extended price (Item Quantity multiplied by Item Price) after the entry is made Save the item name, quantity, item price, and extended price to a file When you create the file, prompt the user for...
Create a Python program that: Creates a sales receipt, displays the receipt entries and totals, and...
Create a Python program that: Creates a sales receipt, displays the receipt entries and totals, and saves the receipt entries to a file Prompt the user to enter the Item Name Item Quantity Item Price Display the item name, the quantity, and item price, and the extended price (Item Quantity multiplied by Item Price) after the entry is made Save the item name, quantity, item price, and extended price to a file When you create the file, prompt the user...
Create a python program that: Creates a sales receipt, displays the receipt entries and totals, and...
Create a python program that: Creates a sales receipt, displays the receipt entries and totals, and saves the receipt entries to a file Prompt the user to enter the Item Name Item Quantity Item Price Display the item name, the quantity, and item price, and the extended price (Item Quantity multiplied by Item Price) after the entry is made Save the item name, quantity, item price, and extended price to a file When you create the file, prompt the user...
C++ Create a program that checks whether a number is a prime number and displays its...
C++ Create a program that checks whether a number is a prime number and displays its factors if it is not a prime number. Console Prime Number Checker Please enter an integer between 1 and 5000: 5 5 is a prime number. Try again? (y/n): y Please enter an integer between 1 and 5000: 6 6 is NOT a prime number. It has 4 factors: 1 2 3 6 Try again? (y/n): y Please enter an integer between 1 and...
Write a C++ program that displays the current time
Write a C++ program that displays the current time
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a...
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an inpatient or an outpatient. If the patient was an inpatient, the following data should be entered: The number of days spent in the hospital The daily rate Hospital medication charges Charges for hospital services (lab tests, etc.) The program should ask for the following data if the patient was...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT