In: Computer Science
(MUST BE DONE IN C (NOT C++))
Instead of using two different variables, define a structure with two members; one representing the feet and the other one representing the inches. You will also use three functions; one to initialize a structure, another one to check the validity of its values and one last one to print them out.
First, go ahead and define your structure. Next, declare a structure of this type inside main. Then, call your first function (this is a initialization function). This function will be of type structure, it will receive zero parameters and it will return a structure. Inside the function, ask the user for the height of the student. In other words, you will use this function to initialize the structure you have in main.
When done, call a second function (this is checking the function). To this function, you will send your structure. The function will not return anything and it will validate the values inputted by the user. If any of the values is not in the right range (between 5’ 8” and 7’ 7”), display an error message and exit the program. Make sure to also check for negative values.
Lastly, if the program didn’t exit, call the last function (this is printing the function). This function will receive a structure, it will not return anything and it will display the values of the two members of the structure.
Code is:
#include <stdio.h>
#include <stdlib.h>
struct st_size
{
int feet;
int inches;
};
struct st_size init_struct();
void check_struct(struct st_size in_struct);
void print_struct(struct st_size in_struct);
int main()
{
struct st_size struct1;
struct1 = init_struct();
check_struct(struct1);
print_struct(struct1);
return 0;
}
//function to assign structure
struct st_size init_struct()
{
struct st_size struct2;
printf("Please enter height of the student in feet and inches \n");
scanf("%d", &struct2.feet);
scanf("%d", &struct2.inches);
return struct2;
}
//function to check structure
void check_struct(struct st_size in_struct)
{
if (((in_struct.feet <= 5 ) && (in_struct.inches < 8 )) || ((in_struct.feet >= 7) && (in_struct.inches>7))||((in_struct.feet<0) || (in_struct.inches<0 )))
{
printf("Invalid height");
exit(0);
}
}
//function to print structure
void print_struct(struct st_size in_struct)
{
printf("Height entered is %d feet and %d inches",in_struct.feet,in_struct.inches);
}
Output: