In: Computer Science
Instead of using two different variables, however, we will
define a structure with two members; one representing the feet and
the other one representing the inches. We 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 the main. Then, call your first
function (the 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 the main. When done, call a
second function (the checking 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 (the
printing 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.
Use C code to solve this problem.
Code:
#include <stdio.h>
struct Height{
int feet;
int inches;
};
struct Height initializeStructure(){
struct Height h;
printf("Enter feet: ");
scanf("%d", &h.feet);
printf("Enter inches: ");
scanf("%d", &h.inches);
return h;
}
void verifyHeight(struct Height h){
int inches = h.feet * 12 + h.inches;
if(inches <= (5*12 + 8) || inches >= (7*12 + 7)){
printf("Height entered is invalid");
exit(0);
}
}
void printHeight(struct Height h){
printf("\nHeight:\tFeet = %d Inches = %d\n", h.feet, h.inches);
}
int main(){
struct Height h1;
h1 = initializeStructure();
verifyHeight(h1);
printHeight(h1);
return 0;
}
Output: