Question

In: Computer Science

Written in C code: Using a structure called person, write a C function called compare_age that...

Written in C code:

Using a structure called person, write a C function called compare_age that takes two person structure pointers as input and returns 1 if the first person is older, -1 if the second person is older, or 0 if they have exactly the same birthday. Hint: Compare the years first, then if equal compare months, if they are equal compare days.

The structure called person should contain the following; has fields for name (50 char array), dateOfBirth (structure called date that has three fields day, month, year, all of which are ints.), address (200 char array), phoneNum (20 char array), and ID (int).

You should print out the comparison result of whether person 1 or 2 is older or if they have the same age.

Solutions

Expert Solution

#include<stdio.h>
struct date
{
int day;
int month;
int year;
};
struct person
{
char name[50];
struct date dateOfBirth;
char address[200];
char phoneNum[20];
int ID;
};
int compare_age(struct person *person1,struct person *person2)
{
if(person1->dateOfBirth.year==person2->dateOfBirth.year)
{
if(person1->dateOfBirth.month==person2->dateOfBirth.month)
{
if(person1->dateOfBirth.day==person2->dateOfBirth.day)
return 0;
else if(person1->dateOfBirth.day<person2->dateOfBirth.day)
return 1;
else
return -1;
}
else if(person1->dateOfBirth.month<person2->dateOfBirth.month)
return 1;
else
return -1;
}
else if(person1->dateOfBirth.year<person2->dateOfBirth.year)
return 1;
else
return -1;
}
void main()
{
struct person person1,person2;
printf("person1 details\n");
printf("Enter name: ");
scanf("%s",person1.name);
printf("Date of Birth details: \n");
printf("Enter day: ");
scanf("%d",&person1.dateOfBirth.day);
printf("Enter month: ");
scanf("%d",&person1.dateOfBirth.month);
printf("Enter year: ");
scanf("%d",&person1.dateOfBirth.year);
printf("Enter address: ");
scanf("%s",person1.address);
printf("Enter phone number: ");
scanf("%s",person1.phoneNum);
printf("Enter ID: ");
scanf("%d",&person1.ID);
printf("person2 details\n");
printf("Enter name: ");
scanf("%s",person2.name);
printf("Date of Birth details: \n");
printf("Enter day: ");
scanf("%d",&person2.dateOfBirth.day);
printf("Enter month: ");
scanf("%d",&person2.dateOfBirth.month);
printf("Enter year: ");
scanf("%d",&person2.dateOfBirth.year);
printf("Enter address: ");
scanf("%s",person2.address);
printf("Enter phone number: ");
scanf("%s",person2.phoneNum);
printf("Enter ID: ");
scanf("%d",&person2.ID);
if(compare_age(&person1,&person2)>0)
printf("person1 is older\n");
else if(compare_age(&person1,&person2)<0)
printf("person2 is older\n");
else
printf("person1 and person2 have same age\n");
}

Output


Related Solutions

Write C code that contains a function called triangle_generator that outputs a triangle wave at a...
Write C code that contains a function called triangle_generator that outputs a triangle wave at a specified frequency (in hertz) using a specified sample rate (in hertz), and for a specified time duration (in seconds). These parameters are float type. The output you generate are floating point numbers between -1 and 1, one number per output line. The math trig functions in C use radians while all the specifications here are in hertz (cycles per second). One hertz is 2*Pi...
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
Write C code to create a structure called time_of_day, which stores the current time in hours,...
Write C code to create a structure called time_of_day, which stores the current time in hours, minutes, and seconds. All the fields should be integers except for seconds, which should be a floating point value. Write a C function called check_time, which takes a pointer to a time_of_day structure as input, and return 1 if the time is valid (0 to 23 hours, 0 to 59 minutes, 0 to 59.999999 seconds) and 0 otherwise. Assume that times are stored in...
Using pseudocode or C++ code, write a function to compute and return the product of two...
Using pseudocode or C++ code, write a function to compute and return the product of two sums. The first is the sum of the elements of the first half of an array A. The second is the sum of the elements of the second half of A. The function receives A and N ≥ 2, the size of A, as parameters. (in c++)
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who does not like any limitations in the life. And when you said to him that it is totally impossible to work with integer numbers bigger than 4 294 967 296 in C++ he blamed you in time-wasting during the university study.So to prove that you hadn't waste 2 months of your life studying C++ in university you have to solve this issue. Your task...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT