1 / 13

Group 1 EEL presentation

Group 1 EEL presentation. Arnaldo Garcia PID 2414790 7/19/2010. The Program. #include &lt; stdio.h &gt; struct donor{ float amount; char fname [30]; char lname [30]; }; int main(void) { struct donor rec ; printf (&quot;Enter the donor's first and last names,<br>&quot;);

duc
Download Presentation

Group 1 EEL presentation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Group 1 EEL presentation Arnaldo Garcia PID 2414790 7/19/2010

  2. 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; • }

  3. 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

  4. 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

  5. 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.

  6. Program breakdown • #include <stdio.h> • This is the standard input and output header of the program. • It contains many functions like printf and scanf.

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

  8. 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.

  9. 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 .

  10. 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.

  11. 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.

  12. 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.

  13. Program breakdown • return 0;} • Return 0 is used to end the program and to demonstrate that it runs properly. • THE END

More Related