130 likes | 234 Views
Group 1 EEL presentation. Arnaldo Garcia PID 2414790 7/19/2010. The Program. #include < stdio.h > struct donor{ float amount; char fname [30]; char lname [30]; }; int main(void) { struct donor rec ; printf ("Enter the donor's first and last names,<br>");
E N D
Group 1 EEL presentation Arnaldo Garcia PID 2414790 7/19/2010
The Program • #include <stdio.h> • struct donor{ • float amount; • char fname[30]; • char lname[30]; • }; • int main(void) • { • struct donor rec; • printf("Enter the donor's first and last names,\n"); • printf("separated by a space: "); • scanf("%s %s", rec.fname, rec.lname); • printf("\nEnter the donation amount: "); • scanf("%f", &rec.amount); • printf("\nDonor %s %s gave $%.2f.", rec.fname, rec.lname, • rec.amount); • return 0; • }
Purpose of the program The programs asks the user to input the first and last name of a donor, and the donation amount, and them it formats and prints out all of this information
Structures • The secondary purpose of the program is to provide a simple example that illustrates how structures work. • Structures are useful for storing many different types of variables under a single name
Structures in this program structdonor{ float amount; char fname[30]; char lname[30]; }; • This program contains the structure donor with the member variables amount, last name and first name. • With this structure initialise many more variables of type donors can be made, such as • struct donor rec; • struct donor donor1; • struct donor donor2; • All of the structure variables are capable of having their own separate value for the member variables amount, fname and lname. • To acces or assing values to this member variables the format must be as follows, structure_variable_name.member_varible_name • For example rec. fnameand rec.amount.
Program breakdown • #include <stdio.h> • This is the standard input and output header of the program. • It contains many functions like printf and scanf.
Program breakdown • struct donor{ • float amount; • char fname[30]; • char lname[30]; • }; • This is the Structure definition, program must have the keyword struct followed by the structure name in this case donor • The three variables Inside the braces are the member variables, one float for amount and two character arrays with a maximum of 30 characters for the first and last name
Program breakdown • int main(void) { • This line calls the main function, it commences the execution of the program. • The void inside of the main means it does not receive any information.
Program breakdown • struct donor rec; • this line innitializes the structure, by creating a variable of type struct donor named rec • printf("Enter the donor's first and last names,\n"); • printf("separated by a space: "); • This two lines simply call on the printffuntion to print what is written withing quotation marks • the \n does not print instead it makes a new line, hence all new text from that point forward appears in the line below .
Program breakdown • scanf("%s %s", rec.fname, rec.lname); • The scanf receives input from the keyboard, • the %s represents a conversion specifier, it tells the function what data type it should expect to receive, in this case it receives a pointer to an array of type char ending with the null character • The first string enter will be stored in the rec.fname character array member variable, and the second, following a space, will be stored in the rec.lname variable.
Program breakdown • printf("\nEnter the donation amount: "); • Prints a new line with the words inside the quotation marks • scanf("%f", &rec.amount); • Uses the conversion specifier %f which means it is asking for a value of type float and it stores it at the the address of the third structure member variable, rec.amount.
Program breakdown • printf("\nDonor %s %s gave $%.2f.", rec.fname, rec.lname, rec.amount); • Prints “Donor” plus two character arrays and them it prints “gave $” and them it prints a floating point value with two decimal places of precision after the decimal point. • The two character arrays are stored at rec.fname and rec.lname and the float value is stored at rec.amount.
Program breakdown • return 0;} • Return 0 is used to end the program and to demonstrate that it runs properly. • THE END