In: Computer Science
You are required to use only C for this problem.
To begin, instead of using two different variables for this problem, you are going to 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, one to check the validity of its values, and the last one to print them out.
First, define your structure. Next, declare a structure of this type inside main. Then, call your first function (the initialization function). This function will be of a type structure, it will receive zero parameters, and it will return a structure. Inside the function, you will ask the user for the height of the student. To clarify, you will use this function to initialize the structure you have in main.
After that, call a second function (the checking function). In 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” of the student height), your program will display an error message and your program will then exit. Oh and do make sure to also check for negative values. That'll be important.
With all that done, 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. Good luck!
#include <stdio.h>
//Structure declaration for the valid height
struct Valid_height {
int feet;
int inch;
};
//Struct Function prototype for initialization
struct Valid_height getStudentDetail();
//Struct Function prototype for Validation
void validate_student_detail(struct Valid_height
student_height);
//Struct Function prototype for Printing
void print_student_detail(struct Valid_height student_details);
int main() {
struct Valid_height Student1;
Student1 = getStudentDetail();
validate_student_detail(Student1);
print_student_detail(Student1);
return 0;
}
//Struct Function definition for initialization
struct Valid_height getStudentDetail()
{
struct Valid_height s1;
printf("Enter the student height details in feet and
inches\n");
printf("Enter Student height in feet: ");
scanf("%i", &s1.feet);
printf("Enter Student height in inches: ");
scanf("%i", &s1.inch);
return s1;
}
//Struct Function definition for validation
void validate_student_detail(struct Valid_height
student_height)
{
printf("In the validate student function\n");
//Handle the feet to inches conversion for valid height
float valid_inches;
float total_feet;
int quotient;
valid_inches = student_height.inch % 12;
quotient = student_height.inch / 12;
total_feet = (valid_inches/12);
student_height.feet = quotient + student_height.feet;
total_feet = student_height.feet + total_feet;
printf("The Student height in feet: %f\n", total_feet );
if (total_feet < 5.6667 || total_feet >= 7.58333)
{
printf("Student height not in expected range between 5’ 8” and 7’
7” \n");
exit(0);
}
}
//Struct Function definition for printing
void print_student_detail(struct Valid_height
student_details)
{
//Handle the feet to inches conversion for valid height
float valid_inches;
int quotient;
valid_inches = student_details.inch % 12;
quotient = student_details.inch / 12;
student_details.inch = valid_inches;
student_details.feet = quotient + student_details.feet;
printf("The Student height in feet: %i\n", student_details.feet
);
printf("The Student height in inches: %i\n", student_details.inch
);
}