In: Computer Science
Exercise 1: Write a C program that does the following tasks:
a. Declare a structure called StudRec with four components:
an int containing the StuId,
a string containing the StudName,
a string containing the MajorName,
b. Define typedef List to be a synonym for the type struct StudRec.
c. Declare a global variable array StudST[] of List.
d. Declare a global variable Ptr to be a pointer to List.
e. Write a C function (return pointer to List) that does the following. It accepts the StudST array and an N integer denoting the actual size of the array, read and fill N student details using structure, Dynamic Memory Allocation from the keyboard and return a pointer of List.
f. Declare a structure called Major with two components:
a string containing the MajorName,
an int NumSt containing the number of students in the Major,
g. Declare a global variable array MajorST[] of structure Major.
h. Write a C function that does the following. It accepts the MajorST array with 4 integer denoting the actual size of the array and update the array with 4 MajorName read from the keyboard and assign 0 to NumSt.
i. Write a C function that does the following. It accepts, as arguments, the StudST array (e) with N integer denoting the actual size of the array and the MajorST array (h). Using StudST, the function calculates the number of students in each major and updates the MajorST array by these numbers for each MajorName.
j. Compile and Run the previous statements in a main C file.
Program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
Declare a structure called StudRec with four components:
- an int containing the StuId,
- a string containing the StudName,
- a string containing the MajorName,
Define typedef List to be a synonym for the type struct
StudRec.
*/
typedef struct StudRec{
int Stud;
char StudName[100];
char MajorName[100];
}List;
// Declare a global variable array StudST[] of
List.
List StudST[1000];
//Declare a global variable Ptr to be a pointer to
List.
List *Ptr;
/*
function (return pointer to List) that does the following. It
accepts the StudST array and an
N integer denoting the actual size of the array, read and fill N
student details using structure, Dynamic
Memory Allocation from the keyboard and return a pointer of
List.
*/
List* readStudents(List StudST[], int N){
Ptr = (List *)malloc(N*sizeof(List));
int i;
for(i=0; i<N; i++){
printf("Enter the ID of Student:
");
scanf("%d", &(Ptr+i)->Stud);
printf("Enter the name of Student:
");
scanf("%s", (Ptr+i)->StudName);
printf("Enter the major of Student:
");
scanf("%s", (Ptr+i)->MajorName);
}
return Ptr;
}
/*
Declare a structure called Major with two components:
- a string containing the MajorName,
- an int NumSt containing the number of students in the
Major,
*/
struct Major{
char MajorName[100];
int NumSt;
};
// Declare a global variable array MajorST[] of
structure Major.
struct Major MajorST[1000];
/*
function that does the following. It accepts the MajorST array with
4 integer denoting the
actual size of the array and update the array with 4 MajorName read
from the keyboard and assign 0 to
NumSt.
*/
void readMajors(struct Major MajorST[4]){
int i=0;
for(i=0; i<4; i++){
printf("Enter the name of the Major
%d", i+1);
scanf("%s",
MajorST[i].MajorName);
MajorST[i].NumSt = 0;
}
}
/*
function that does the following. It accepts, as arguments, the
StudST array (e) with N integer
denoting the actual size of the array and the MajorST array (h).
Using StudST, the function calculates
the number of students in each major and updates the MajorST array
by these numbers for each
MajorName.
*/
void updateNumMajors(List StudST[], int N, struct Major
MajorST[4]){
int i=0;
for(i=0;i<N;i++){
if(strcmp(StudST[i].MajorName,
MajorST[0].MajorName) == 0){
MajorST[0].NumSt++;
}
else if(strcmp(StudST[i].MajorName,
MajorST[1].MajorName) == 0){
MajorST[1].NumSt++;
}
else if(strcmp(StudST[i].MajorName,
MajorST[2].MajorName) == 0){
MajorST[2].NumSt++;
}
else if(strcmp(StudST[i].MajorName,
MajorST[3].MajorName) == 0){
MajorST[3].NumSt++;
}
else{
printf("Something went wrong");
}
}
}
// printing hello world just to check everything
compiles fine
int main(){
printf("Hello World");
return 0;
}


==========================================================================================
Hope this helps!
Please let me know if any changes needed.
Thank you!
I hope you're safe during the pandemic.