Question

In: Computer Science

Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...

Struct PERSON Assignment

Outcomes:

  • Demonstrate the ability to create structs using typedef
  • Demonstrate the ability to create an array of structs

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.

  • name [this will be a string]
  • birthdate [this will be a DATE]
  • gender [this will be a char]
  • annualIncome [this will be either float or double, your choice]

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:

  • Use global variables, in this or any program ever.
  • Use goto statement(s), in this or any program ever.

Solutions

Expert Solution

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**************


Related Solutions

Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs 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. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs 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. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to...
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to create and use menus Demonstrate the ability to create and use an array of structs Demonstrate the ability to generate and use random numbers Program Specifications: Assume that gambling Greg often goes to the Dog Racing Track. Greg loves to bet on the puppies. In each race Greg will place a wager and pick a dog. The dog information will be stored in a...
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to...
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to create and use menus Demonstrate the ability to create and use an array of structs Demonstrate the ability to generate and use random numbers Program Specifications: Assume that gambling Greg often goes to the Dog Racing Track. Greg loves to bet on the puppies. In each race Greg will place a wager and pick a dog. The dog information will be stored in a...
Playing with strings Assignment Outcomes: Demonstrate the ability to create strings. Demonstrate the ability to manipulate...
Playing with strings Assignment Outcomes: Demonstrate the ability to create strings. Demonstrate the ability to manipulate strings. Demonstrate the ability to write well written code. Program Specifications: DESIGN and IMPLEMENT a short program that will: Allow the user to enter a string with up to 100 letters. Display the user-entered string: Forward Backward Vertical As a triangle made from the letters of the string Display the number of letters in the string. Once everything above is displayed, the program will...
Create a typedef fruitType using the struct fruitType_struct to store the following data about a fruit:...
Create a typedef fruitType using the struct fruitType_struct to store the following data about a fruit: name (string, up to 50 characters long) color (string, up to 10 characters long) fat (integer) sugar (integer) carbohydrate (integer) Write a void function called printFruit that takes a fruitType parameter and prints the data (as shown in the example below). Declare a variable of type fruitType to store the following data: name: banana color: yellow fat: 1 sugar: 15 carbohydrate: 22 then use...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode*...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode* left;     struct bintreenode* right; } btreenode; // Used for a node in the queue. typedef struct node {     btreenode* nodePtr;     struct node* next; } node; // Used to represent the queue efficiently. typedef struct queue {     node* front;     node* back; } queue; Implement the following functions: void bfs(btreenode* root) // Prints a breadth first search traversal of the binary search tree rooted at root....
#C language array must store at least 50 inputs Game Scores Assignment Outcomes: Demonstrate the ability...
#C language array must store at least 50 inputs Game Scores Assignment Outcomes: Demonstrate the ability to create a menu driven program. Demonstrate the ability to create and use a 2D array on the stack. Demonstrate the use of functions. Demonstrate good programming style. Program Specifications: Your program will read in "game scores" from the user. Each score consists of a score for YOUR team and a score for the OTHER team. You don't need names or dates for the...
In this assignment, you need to demonstrate your ability in using input, output, data types, and...
In this assignment, you need to demonstrate your ability in using input, output, data types, and if statement in C++ program. Assume that you need write a C++ program for a cash register. There are only four items in the store: Cereal, $3.99 Milk, $3.99 Egg, $0.25 Water, $ 1.50 Once a customer purchases items, you will ask her/his how many of them are bought. The quantity can be in the range of 0-10 (including 0 and 10). Then, calculate...
Using Union technique, define two structs employee_record and student_record. The employee_record struct will contain the name...
Using Union technique, define two structs employee_record and student_record. The employee_record struct will contain the name (size 50) and the salary (double) of an employee. The student_record struct will have the name (size 30), Id number (int) of a student, and the grade (int). Include these two structs in a union called 'person'. Using file redirection called input.txt, at first, ask the user for the employee's information and print them from the main() and the size of the union. Then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT