140 likes | 251 Views
STRUCTURES WITH ARRAYS. Felipe Navarro Raul Amorin Ashley Dierivo Nicolas Caballero Leony Gonzalez. EEL 2880. GROUP #3. What is a Structure?.
E N D
STRUCTURES WITH ARRAYS Felipe Navarro Raul Amorin Ashley Dierivo Nicolas Caballero Leony Gonzalez EEL 2880 GROUP #3
What is a Structure? A structure is a way that we can model real life objects in the programming language by encapsulating all the defining characteristics of the object we are describing into a single data structure.
First Half of Code This statement includes the standard I/O header files for use within the program 2) This statement declares the structure named “donor”. Each donor structure will have a floating point number named “amount” to represent the amount that was donated by said person, a character array named “fname” to represent the person’s first name and a character array named “lname” to represent the person’s last name. 3) This is the main method of the program. 4) This statement initialized a donor structure named “rec”.
Second Half 5) The two printf statements wo strings; One for the first print strings to prompt the user to enter tname and one for the last name. The scanf statement scans the user’s input and stores the strings in the character arrays of the donor structure named “rec”. 6) The printf statement prompts the user to enter the donation amount and the scanf statement scan’s the user’s input and stores the value in the floating point variable of the donor structure named “rec”. 7) The printf statement prints each data member of the donor structure named “rec”.
The Code Output 1 2 3 4 5
Additional Information As you can see from the program, The data members of structures are called by the syntax structure-name.member-name. For example, a structure named “car” may have a data member of type int named “miles” to represent its mileage. This may be accessed with the syntax car.miles.
Block Diagram #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; } Standard I/O Header Imported Donor Structure Defined Amount First and Last Names Donor “rec” created rec Donor “rec” Assigned First and Last Name rec Donor “rec” Assigned Amount rec Donor “rec” Data Members Printed rec
Similar Program Online struct month { intnumber_of_days; char name[4]; }; static struct month this_month = { 31, "Jan" }; this_month.number_of_days = 31; strcpy( this_month.name, "Jan" ); printf("The month is %s\n", this_month.name );
What We Have Learned • Properly declaring a structure. • How arrays can help when using structures. • How to use, declare, and define arrayed structures. • How pointers can be used within these kinds of structures. • The usefulness of arrayed structures for the entering and storage of lists of information of different types that may need to later be recalled.
Works cited page • Examples came from: • Example 1: http://ftp.tuwien.ac.at/languages/c/programming-bbrown/c_064.htm • Example 2: http://msdn.microsoft.com/en-us/library/b6fae073(VS.71).aspx • Also Referenced: The Complete Reference by Herbert Schildt