370 likes | 462 Views
Arrays Pass by referance Struts the same way as the basic types in C - char, int, float passed as arguments returned as results. Structs as Function Arguments and Results. typedef struct Person { int age; char gnd; float weight; } PERSON ;. int adjustAge( int oldAge ){
E N D
Arrays • Pass by referance • Struts • the same way as the basic types in C - char, int, float • passed as arguments • returned as results Structs as Function Arguments and Results
typedef struct Person{ int age; char gnd; float weight; • } PERSON;
int adjustAge( int oldAge ){ • if ( oldAge < 39 ) return( ++oldAge ); • else return( oldAge ); • /*return (oldAge<39) ? ++oldage : oldage;*/ • } void main(){ • PERSON Jim; • : • Jim.age = adjustAge( Jim.age ); • : } the use of the members of a struct.
typedef struct Book{ • int edition; • int pages; • double weight; • char type; • int WC; /*weight classification 1 heavy, 0 is not */ • } BOOK; Write a function tack an argument double to test the weight of a book and return 1 if the weight greater than 2 and 0 otherwise. The value to be assign to WC member.
int weightC(double w){ if(w > 2) return 1; else return 0; } void main(){ BOOK myBok; : myBoK.wc = weightC(myBok.weight); : } int weightC(double w){ return (w > 2)? 1 : 0; }
Consider the student struct: add a new member name it GL /*Grades Later*/ Write a function tack a double to test the average of the student and return a later match the average to be assign to the new member Test the function ASS
PERSON get_details() { PERSON temp; printf("Please enter Age, Gender and Weight (Kg): "); scanf("%d %c%f", &temp.age, &temp.gnd, &temp.weight ); return temp; } void main(){ PERSON Jim, Mary, Sid; : Jim = get_details(); : } The whole of a struct is returned by a function
Write a function allow the user to input the details of a BOOK and return it back • typedef struct Book{ • int edition; • int pages; • double weight; • char type; • } BOOK;
BOOK GetBook(){ BOOK tmp; printf(“\nEnter the BOOK information:”); printf(“ [edition, pages, weight, and type]”); scanf(“%d%d%f%c”, &tmp.edition, &tmp.pages. &tmp.weight. &tmp.type); retrun tmp; } void main(){ BOOK myBook; : myBook = GetBook(); : }
Write a function allow the user to input the details of a STUDENTand return it back Test the function ASS
void show_details( PERSONwho ){ • printf( "Person's age was: %d\n", who.age ); • printf( " Gender was: %c\n", who.gnd ); • /* Kg -> lb */ • who.weight = who.weight * 2.2; • printf( " weight (lb) was: %5.1f\n", who.weight ); • } void main(){ PERSON Jim, Mary, Sid; : Jim = get_details(); : show_details(Jim); } Passing a struct as a function argument
void PrintBook( const BOOK b){ • printf(“The BOOK information:\n”); • printf(“\n\tedition: %d\n\tpages: %d”, b.edition, b.pages); • printf(“\n\tweight: %f \n\ttype: %c”, b.weight, b.type); • } • void main(){ • BOOK myBook = {2, 120, 2.5, ‘A’}; • : • PrintBook(myBook); • : • }
Write a function tack a STUDENT and print the content out Test the function ASS
to store the same package of information for a number of different entities • typedef struct Person{ int age; char gnd; float weight; • } PERSON; Arrays of structs
PERSON employee[12]; • We have an array of 12 elements • each of which is a structure with 3 members. • We can access any member of any element, employee[9].age = 45; • Also we can treat each element as a whole struct • show_details(employee[5]); • employee[3] = get_details(); Arrays of structs
Consider the following struct BOOK, Create an array of three BOOK s • typedef struct Book{ • int edition; • double weight; • char type; • } BOOK;
BOOK academic[3]; BOOK academic[3] = { {1,2.5,’a’}, {2,3.5,’n’}, {2,1.5,’a’} }; Read in the information of the second
academic[1] = GetBook(); Printout the information of the first
PrintBook(academic[0]); Printout the content of the array
int i; for(i=0; i<3; i++) PrintBook(academic[i]);
1. Create an array of four STUDNTs with initial values • 2. • Create another array of five STUDNTs • Read in the information of the whole student • Printout the content • Assign the second the information of the second of the array created at 1 • Printout the information of the second ASS
typedef struct{ • int age; • char gnd; • float weight; • char name[20]; • } HUMAN; HUMAN ahm = { 45, ‘M’, 95.1, “Ahmad Ali”}; HUMAN ahm = { 45, ‘M’, 95.1, {‘A’, ‘h’, ‘m’, ‘a’, ‘d’, ‘ ‘, ‘A’, ‘l’, ‘i’, ‘\0’}}; Structs Containing Arrays
char initial = ahm.name[0]; • putchar(initial); • Printout the 3rd char of ahm’s name
char third = ahm.name[2]; printf(“%c”, third); /*putchar(third)*/ allow the user to input the 6th char of ahm’s name
scanf(“%c”, &ahm.name[5]); print the name char by char
int i; for(i=0; i<20; i++) putchar(ahm.name[i]); i=0; while( ahm.name[i] != ‘\0’) putchar(ahm.name[i++]); Print out the name at once
printf(“%s\n”, ahm.name); puts(ahm.name); allow the user to input the ahm’s name
scanf(“%s”, ahm.name); gets(ahm.name); Which is better?
typedef struct Book{ • int edition; • double weight; • char type; • } BOOK; • Rewrite the BOOK with a new member title
typedef struct Book{ • int edition; • double weight; • char type; • char title[30]; • } BOOK; • Create a variable of book with initial values
BOOK myBook = {1,2.5,’a’, “C programming” } change the title to “c how to program”
gets(myBook.title); scanf(“%s”, myBook.title);
Create a Time struct with classification member (“PM”, “AM”) Write a function to print out the content of the Time Create a STUDENT struct with a name member up to 30 char Write two functions to read the information of a student and the second to print out the information. ASS
HUMAN client[2] = { 35, ‘M’, 95.1, “Ahmad Ali”, 25, ‘F’, 50.9, "Salma Zakarea” }; printf( "%s\n", client[1].name ); printf( "%c\n", client[1].name[0] );
HUMAN client[2] = { 35, ‘M’, 95.1, “Ahmad Ali”, 25, ‘F’, 50.9, "Salma Zakarea” }; HUMAN temp; Assign the values of first client to temp • typedef struct{ • int age; • char gnd; • float weight; • char name[20]; • } HUMAN;
temp = client[0]; temp.age = client[0].age; temp.gnd = client[0].gnd; Temp.weight = client[0].weight; strcpy(temp.name, client[0].name);