In: Computer Science
Struct PERSON Assignment
Outcomes:
Program Specifications:
DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON.
Create a struct using the typedef command for a DATE.
Create a struct for a PERSON with the following fields.
Create three variables of type PERSON. Create a function that populates the data for each person (all 3 of them). Create a function that outputs all data about each of the people in a nice formatted manner.
Data Validation:
All dates entered must be validated. Make sure you account for the number of days in each month, the fact that there are exactly 12 months and every four years there is a leap year.
The name for each PERSON will be stored in sentence case.
The gender will either be M, F, or O.
The annual income will between 0 dollars and 1 million dollars.
Submission Requirements:
Requirements will be same as the first assignment which will be the same for all future assignments except that you do NOT NEED a design tool for this assignment.
DO NOT:
CODE:
OUTPUT:
Raw_code:
//includind stdio.h for input and ouput functions
#include<stdio.h>
//including string functions
#include<string.h>
//i implemented new functions for the code readability
//leap year function to check whether the given year in the date of
birth is leap year or not
int leapyear(int);
//check_date function prototype to check the whether the given date
is valid or not
int check_date(int ,int ,int);
//valid_gender function prototype to check whether the given gender
is valid or not
int valid_gender(char c);
//sentence_case function prototype to check whether the given name
in sentence case or not
int sentence_Case(char *);
//typedef is used to give new name for the existing name in c
program
//DATE structure with 3 members for day ,month and year
typedef struct
{
//integer variable for date
int dd;
//integer variable for month
int mm;
//integer variable for year
int yyyy;
}DATE;//structure name DATE
//PERSON structure 4 members for name,date of birth that is another
structure , gender and annual income
typedef struct
{
//character array for name
char name[30];
//structure variable of date of birth this is best
advantage of typedef we do not need to always specify the
//struct structname structure_member just we need
structure_name structure_member
DATE dob;
char gender[1];
double annualIncome;
}PERSON;//structure name PERSON
//function to get the details of the person
PERSON setData_of_Person()
{
//PERSON structure member
PERSON ob;
int valid;
//enter the name of the person
printf("\nEnter the Name:");
scanf("%s",ob.name);
//if the name is not in sentence case this loop
executed
while(sentence_Case(ob.name)!=1)
{
printf("\nInvalid name!\nEnter the
name again [ex:Tony ,Robert]:");
scanf("%s",ob.name);
}
//enter the date of birth of the person
printf("\nEnter the date of birth (dd mm
yyyy):");
scanf("%d %d
%d",&ob.dob.dd,&ob.dob.mm,&ob.dob.yyyy);
valid=check_date(ob.dob.dd,ob.dob.mm,ob.dob.yyyy);
//if the date is not valid this loop is executed
while(valid!=1){
printf("\nInvalid date:\nEnter the
date of birth again:");
scanf("%d %d
%d",&ob.dob.dd,&ob.dob.mm,&ob.dob.yyyy);
valid=check_date(ob.dob.dd,ob.dob.mm,ob.dob.yyyy);
}
//enter the gender of the person
printf("\nEnter the gender (M or F or O):");
scanf("%s",ob.gender);
//if the gender is not valid this loop is
executed
while(valid_gender(ob.gender[0]))
{
printf("\nInvalid gender \nEnter
the gender again:");
scanf("%s",ob.gender);
}
//enter the annnual income of the person
printf("\nEnter the annualIncome:");
scanf("%lf",&ob.annualIncome);
//if the annual income is not valid this loop is
executed
while(ob.annualIncome<0||ob.annualIncome>100000)
{
printf("\nEnter valid
annualIncome:");
scanf("%lf",&ob.annualIncome);
}
return ob;
}
//function to display the function
void getData_of_Person(PERSON ob)
{
printf("\nName: %s",ob.name);
printf("\nDate of birth:
%d-%d-%d",ob.dob.dd,ob.dob.mm,ob.dob.yyyy);
printf("\nGender:%s",ob.gender);
printf("\nAnnual
Income:$%.2lf",ob.annualIncome);
printf("\n------------------------------------------");
}
//main function
int main()
{
int number_of_people,i;
//taking the value of number of people
printf("Enter the number of people:");
scanf("%d",&number_of_people);
//array of structures
PERSON ob[number_of_people];
//taking the information about the people
for(i=0;i<number_of_people;i++)
{
ob[i]=setData_of_Person();
}
printf("\nThe Details are:");
//displaying the people information
for(i=0;i<number_of_people;i++)
{
getData_of_Person(ob[i]);
}
return 0;
}
//fucntion definition to check the given name is in sentence_case
or not
int sentence_Case(char *name)
{
int i=1;
//checking the first letter of the name is capital or
not
if(name[0]>=65&&name[0]<=90)
{
//checking remaining letters are
small case
while(name[i]>=97&&name[i]<=122)
i++;
//at the end the stringlenth is
equal to the i variable we conculde that given name is valid
//then return 1
if(i==strlen(name))
return 1;
//else return 0
else
return 0;
}
//if the first letter of the name is not capital it is
not in sentence case then return 0
else
return 0;
}
//function defintion to check the given the gender is valid or
not
int valid_gender(char c)
{
//if the gender M or F or O return 0
if(c=='m'||c=='M'||c=='F'||c=='f'||c=='O'||c=='o')
return 0;
//else return 1;
else
return 1;
}
//function defintion to check the date of birth is valid or
not
int check_date(int d,int m,int y)
{
//if the days are less than or equal to 31 ,months are
less than or equal to 12 and year is greater than 0
//this if condition is executed
if(d<=31&&m<=12&&y>0)
{
//if the days
are valid according to their months
if(d<=31&&(m==1||(m==3)||m==5||m==7||m==8||m==10||m==12))
{
return 1;
}
else
if(d<=30&&(m==4||m==6||m==9||m==11))
{
return 1;
}
//if the day is
29 , month is 2 and the year is leap year it is valid date
else
if(d==29&&m==2&&leapyear(y))//function calling to
check the year is valid or not
{
return 1;
}
else return
0;
}
else
return 0;
}
//function defintion to check the year is leap year or not
int leapyear(int year)
{
//if the year is divisible by 4 only it is a leap
year
//if the year is divisible by 4 ,100 and 400 it is a
leap year
//in all other conditions it is not a leap year
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
{
return 1;
}
else
{
return 0;
}
}
else
{
return 1;
}
}
else
{
return 0;
}
}
**********For any queries comment me in the comment
box**************