In: Computer Science
Here, we will create a structure that resembles a university’s profile. 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.
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.
Use C code to answer this question.
#include <stdio.h> // for scanf() and prinf() functions
#include <stdlib.h> // for exit() function
#define NAME_SIZE 40
#define TERM_SIZE 8
struct UniversityProfile {
int noOfUndergraduate;
int noOfGraduate;
int noOfClassrooms;
char nameOfUniversity[NAME_SIZE];
char term[TERM_SIZE];
};
void arrayInitializer(char []); // Function to initialize the array members.
void printArrayMembers(char []); // Funtion to print the array members.
int main() {
struct UniversityProfile profile1 = {.noOfUndergraduate = 2500, .noOfGraduate = 1200, .noOfClassrooms = 800};
int nameSize;
int termSize;
// Asking the user for the estimated lengths of the two arrays.
printf("How many letters do you expect for the name of the university?\n");
scanf("%d", &nameSize);
printf("How many letters do you expect for the term?\n");
scanf("%d", &termSize);
// Checking if the size provided by the user do not surpass the default length
if ((nameSize >= 40) || (termSize >= 8)) // Taking equality in the condition so, to have space for the '\0' character.
{
printf("ERROR: Your provided sizes is more than the nameSize or termSize");
exit(1);
}
// Calling the array initializer function to intialize the array members.
arrayInitializer(profile1.nameOfUniversity);
arrayInitializer(profile1.term);
// Printing all structure members.
printf("Number of Undergraduates: %d\n", profile1.noOfUndergraduate);
printf("Number of Graduates: %d\n", profile1.noOfGraduate);
printf("Number of Classrooms: %d\n", profile1.noOfClassrooms);
printf("Name of University: ");
printArrayMembers(profile1.nameOfUniversity);
printf("Term: ");
printArrayMembers(profile1.term);
return 0;
}
void arrayInitializer(char arr[]) {
printf("Enter the string you want to set(Press . to end): \n");
// This will make sure that character newline and whitespaces are taken until '.' followed but newline is encountered
scanf("%[^.]%*c", arr);
// You can use getche() in a loop by including conio.h if you are using MS DOS compilers.
// In GCC compilers conio.h is not defined.
}
void printArrayMembers(char arr[]) {
printf("%s\n", arr);
}
OUTPUT: