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...
Please, write this code in c++. Using iostream and cstring library. Write a function that will...
Please, write this code in c++. Using iostream and cstring library. Write a function that will delete all words in the given text that meets more that one time. Also note than WORD is sequence of letters sepereated by whitespace. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000 symbols with whitespaces. Each word is not longer than 30 symbols. Output: Formatted text. example: input: Can you can the can...
Part 2 Write a C code to create a user defined function called RecArea that will...
Part 2 Write a C code to create a user defined function called RecArea that will calculate the area of a rectangle. Call the function to print the area of the rectangle on the screen. Note: The user will enter the length and width of a rectangle (decimal values, float) and you Part 3 (Continue on part 2) Create another user defined function called TriArea that will calculate the area of a triangle. Print the area of the rectangle on...
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...
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....
Please, write code in c++. Using iostream and cstring library Write a function that will find...
Please, write code in c++. Using iostream and cstring library Write a function that will find and return most recent word in the given text. The prototype of the function have to be the following void mostRecent(char *text,char *word) In char *word your function have to return the most recent word that occurce in the text. Your program have to be not case-sensitive(ignore case - "Can" and "CAN" are the same words) Also note than WORD is sequence of letters...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT