In: Computer Science
MUST BE DONE IN C (NOT C++))
Here, we will create a structure that resembles a university’s profile (you can pick any university name, just so long as the program runs properly). The structure must contain 5 members:
- One member for number of undergraduate students
- One member for number of graduate students
- One member for number of classrooms
- One member for the name of the university (an array)
- One member for the term (fall, summer or spring; also an array)
Once you define a structure with these members (don’t forget to give your arrays a default length), go ahead and declare one structure of this type in main. Then, also inside main, initialize the members that aren’t arrays. After this, you will ask the user for the estimated lengths of the other two members. For example, “How many letters do we expect for the name of the university? ”. When you receive these two values (one for each array), make sure to check that they do not surpass the default length you gave each array. If any of these two surpass the default length(s), print out a message and exit the program (all of this is done in main as well).
Next, if the program didn’t ended, the next step is to initialize the arrays with the help of a function. You will initialize one array at a time (keep in mind that these arrays are members of a structure, so you will have to manipulate them a little different than before). This function will not return anything and it will accept an array. Inside the function, you will scan for a set of characters given by the user. Indicate the user to end with a "dot" (be sure you understand what a dot is in programming).
You will have to call this function twice, a first time for the University’s name, and a second time for the term. Once you call this function two times, your structure will be fully initialized. All is left, is to print out all the members. Go ahead and print the non-array members. Then, use a function to print out the array members (you will also have to call this function twice; once for the University’s name and once for the term). This function should not return anything and it should accept an array.
#include<stdio.h>
#include<stdlib.h>//for exit()
struct Edu
{
int ug;
int g;
int class;
char uni[100];
char term[100];
};//declaration of structure
void initialize(char arr[], n)//method to initialize
arrays
{
arr= new int [n];
printf("Enter the name followed by a dot to mark the
end: ");
scanf("%s", arr);
}
void display(int ug, int g, int class, char uni[], char
term[])//method to display the structure values
{
printf("No. of Undergraduates: %d", ug);
printf("No. of Graduates: %d", g);
printf("No. of Classes: %d", class);
printf("Name of university: %s", uni);
printf("Name of term: %s", term);
}
int main()
{
int n;
Edu A;
A.ug=0;
A.g=0;
A.class=0;
printf("Enter the no.of undergraduates");
scanf("%d", &A.ug);
printf("Enter the no.of graduates");
scanf("%d", &A.g);
printf("Enter the no.of classes");
scanf("%d", &A.class);
printf("How many letters do we expect for the name of
the university?");
scanf("%d", &n);
if(n<=100)//if the given the size is leserthan or
equal to the default size i.e. 100
initialize(A.uni, n);
else
exit(0);
printf("How many letters do we expect for the name
of the term?");
scanf("%d", &n);
if(n<=100)
initialize(A.term, n);
else
exit(0);
display(A.ug, A.g, A.class, A.uni, A.term);
return 0;
}//end of main()