Sie sind auf Seite 1von 4

# GIRNE AMERICAN UNIVERSITY

## January 10, 2016

1- Define structure "Date" with the following fields and declare two immediate instances of it,
"dtTemp" and "dt1", initialize "dt1" with "1-3-1975":
a) day (integer)
b) month (integer)
c) year (integer)
struct Date{
int day;
int month;
int year;
}dtTemp, dt1={5,5,1975};
2- Define the second structure "Person" with the following fields and declare three immediate
instances of it, "per1", "per2", and "per3" and an immediate pointer to it "perPtr"; initialize
"per3" with "Jim" as name, "Carrey" as surname, dt1 as day-of-birth(DOB), and 'M' as gender.
a)
b)
c)
d)

## name (string with 20 characters)

surname (string with 30 characters)
DOB (the structure "Date")
gender (character ('M' for Male and 'F' for Female))

struct Person{
char name[20];
char surname[30];
struct Date DOB;
char gender; // 'M':Male, 'F':Female
}per1, per2, per3={"Jim","Carrey",dt1,'M'}, *perPtr;
3- Declare an array of "Person" with 4 elements and name it "celebrities".
struct Person celebrities[4];
4- Complete the following function. First parameter is pointer to "Date" structure, 2nd, 3rd, and 4th
are day, month and year respectively which should be assigned to the structure pointed by the
pointer. The function assigns d to the day field, m to the month field and y to the year field of the
structure pointed by date.
void setDate(struct Date *date, int d, int m, int y)
{
date->day = d;
date->month = m;
date->year = y;
}
5- Write a void function which gets a Person instance and prints its information on the screen like
the following:
Jim Carrey
5-5-1974
M
void printPerson(struct Person p)
{
printf("%s %s\t\t%d-%d-%d\t%c\n",
By Sen. Inst. ShAhin Mehdipour Ataee

## GIRNE AMERICAN UNIVERSITY

ENG203 FINAL PREPARATION PRACTICE I

## p.name, p.surname, p.DOB.day,

p.DOB.month, p.DOB.year, p.gender);
}
6- Complete the following function which declares an instance of "Person", reads its information
from the user and then returns it:
struct Person inputPerson()
{
struct Person p;
printf("Enter name: "); scanf("%s",p.name);
printf("Enter surname: "); scanf("%s",p.surname);
printf("Enter date-of-birth(dd-mm-yyyy): ");
scanf("%d-%d-%d",&(p.DOB.day),&(p.DOB.month),&(p.DOB.year));
getchar(); //Eats the last \n entered by the user
printf("Enter gender(M or F): "); scanf("%c", &p.gender);
return p;
}
7- By considering the above program parts and the following function:
struct Person newPerson2(char n[20], char sn[30], struct Date dob, char
gen)
{
struct Person tempP;
strcpy(tempP.name,n); strcpy(tempP.surname,sn);
tempP.DOB = dob; tempP.gender = gen;
return tempP;
}
What is the output of the following code?
setDate(&dtTemp,7,7,1977);
per2 = newPerson2("Sibel","Can", dtTemp,'F');
printPerson(per2);
8- What is the difference between the following function (newPerson1) and the one defined in
question 7 (newPerson2):
struct Person newPerson1(char n[20], char sn[30], struct Date *dob, char
gen)
{
struct Person tempP;
strcpy(tempP.name,n); strcpy(tempP.surname,sn);
tempP.DOB = *dob; tempP.gender = gen;
return tempP;
}
In this function Date is passed-by-reference.
9- What is the output of the following program?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

## GIRNE AMERICAN UNIVERSITY

ENG203 FINAL PREPARATION PRACTICE I

## void setDate(struct Date *date, int y, int m, int d);

void printPerson(struct Person p);
struct Person newPerson1(char n[20], char sn[30], struct Date *dob, char gen);
struct Person newPerson2(char n[20], char sn[30], struct Date dob, char gen);
struct Person inputPerson();
struct Date{
int day;
int month;
int year;
}dtTemp, dt1={5,5,1975};
struct Person{
char name[20];
char surname[30];
struct Date DOB;
char gender; // 'M':Male, 'F':Female
}per1, per2, *perPtr, per3={"Jim","Carrey",dt1,'M'};
struct Person people[4];
int main()
{
struct Person per4;
setDate(&dtTemp,2,2,1982);
per1 = newPerson1("Katy","Perry", &dtTemp,'F');
printPerson(per1);
setDate(&dtTemp,7,7,1977);
per2 = newPerson2("Sibel","Can", dtTemp,'F');
printPerson(per2);
printPerson(per3);
perPtr = &per4;
setDate(&dtTemp,18,7,1918);
strcpy(perPtr->name,"Denzel");
strcpy(perPtr->surname,"Washington");
perPtr->DOB = dtTemp; perPtr->gender = 'M';
printPerson(per4);
struct Person per5 = inputPerson();
printPerson(per5);
system("pause");
return 0;
}
void setDate(struct Date *date, int d, int m, int y)
{
date->year = y; date->month = m; date->day = d;
}
void printPerson(struct Person p)
{
printf("%s %s\t\t%d-%d-%d\t%c\n",
p.name, p.surname, p.DOB.day,
p.DOB.month, p.DOB.year, p.gender);

## GIRNE AMERICAN UNIVERSITY

ENG203 FINAL PREPARATION PRACTICE I

## January 10, 2016

}
struct Person newPerson1(char n[20], char sn[30], struct Date *dob, char gen)
{
struct Person tempP;
strcpy(tempP.name,n); strcpy(tempP.surname,sn);
tempP.DOB = *dob; tempP.gender = gen;
return tempP;
}
struct Person newPerson2(char n[20], char sn[30], struct Date dob, char gen)
{
struct Person tempP;
strcpy(tempP.name,n); strcpy(tempP.surname,sn);
tempP.DOB = dob; tempP.gender = gen;
return tempP;
}
struct Person inputPerson()
{
struct Person p;
printf("Enter name: "); scanf("%s",p.name);
printf("Enter surname: "); scanf("%s",p.surname);
printf("Enter date-of-birth(dd-mm-yyyy): ");
scanf("%d-%d-%d",&(p.DOB.day),&(p.DOB.month),&(p.DOB.year));
getchar(); //Eats the last \n entered by the user
printf("Enter gender(M or F): "); scanf("%c", &p.gender);
return p;
}