1 / 23

Group 3 Presents…

Group 3 Presents…. ARRAY OF STRUCTURES By Andres Quintero Oskar Pio Philip Dacosta Javier Palomino Christopher Ballesteros Calvin Remy. ARRAY OF STRUCTURES. Four Basic Data Types. int = Integer (These are whole numbers) int sum; sum = 20;

risa
Télécharger la présentation

Group 3 Presents…

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 3Presents… ARRAY OF STRUCTURES By Andres Quintero Oskar Pio Philip Dacosta Javier Palomino Christopher Ballesteros Calvin Remy

  2. ARRAY OF STRUCTURES

  3. Four Basic Data Types • int = Integer (These are whole numbers) • int sum; • sum = 20; • float = Floating point (Numbers containing fractional parts) • float money; • money = 0.12; • double = Double (There are exponential numbers) • double = big; • big = 312E+7; • char = Character (These are single characters) • char letter; • letter = A;

  4. Arrays • Arrays are a data structure which holds multiple variables of the same data type • Ex: Imagine you have to keep track of a number of people within an organization, without an array we would need to create a specific variable for each user • Int name1 = 101; • Int name2 = 102; • Int name3 = 103; • It becomes increasingly more difficult to keep track of this as the number of variables increases. Using arrays offer a solution to this problem • Here we can see how arrays can make the previous job much easier to complete • int names[4] • Names[0] = 101; • Names[1] = 102; • Names[3]= 103; • We created an array called names, which has space for four integer variables. • Arrays have the following syntax, using square brackets to access each indexed value. Called an element • X[i] • So that x[5] refers to the sixth element in an array called x. • Assigning values to array elements is done by • X[10] = g; • Assigning array elements to a variable is done by • g = x[10];

  5. Structures • A structure is a data type suitable for grouping data elements together. • Lets create a new data structure suitable for storing the date, the elements or fields which make up the structure use the four basic data types. As the storage requirements for a structure cannot be known by the compiler, a definition for the structure is first required. This allows the compiler to determine the storage allocation needed and also identifies the different sub-fields of the structure • Struct date{ • Int month; • Int day; • Int year; • }; • This declares a NEW data type called date. This date structure of three basic data elements of all type int. • Also Other data structures may be defined as consisting of the same composition as the date structure • Struct date todays_date;

  6. Arrays of Structures • Consider the following • Struct date { • Int month, day, year; • }; • Lets now create an array called birthdays of the same data type as the structure date • Struct date birthdays[5]; • This creates an array of 5 elements which have the structure of date • birthdays[1].month = 12; • birthdays[1].day = 04; • birthdays[1].year = 1998; • --birthdays[1].year;

  7. Description • The array of structures program is designed to define a structure and then declare an array variable of that type. • Example: struct char fname [20]; struct char lname [20]; struct char phone [10];

  8. Block Diagram

  9. Outline Define Structures Create Variables used User Interface loop (Three Data Inputs * Four) Output

  10. Parts of actual code

  11. Parts of actual code (cont.)

  12. Described Code • Lines 7 – 11 struct entry { char fname[20]; char lname[20]; char phone[10]; }; • Define the structure entry with three declared arrays in it, fname, lname, phone.

  13. Line 15 struct entry list[4]; • This declares a 4-element array of structures of type entry.

  14. Lines 19-45 main() function

  15. Lines 24-32 • forloop used to input data for four people. • scanf() will save whatever input the user writes in the appropriate array index and list.

  16. Line 36 printf("\n\n"); • This just prints two blank lines between the user input area and the output results

  17. Lines 40-44 • forloop used to display the data saved in the structure. • Line 42 – prints first and last name of the person • Line 43 – prints the phone number of the person

  18. Program Output Example

  19. Similar Example Program

  20. Similar Example Program

  21. Similar Example Program

  22. Output of Sample Program

  23. What we learned: • A data structure gives us the ability to organize the characteristics of an object. • We can join all the elements into one object. • Objects contained within the structure are referred to as members. • Another advantage we can use is, we can use the same members variables over and over again. • Variables in an array are called elements, and are accessed using square brackets as an index. • You can use many different data types. • scanf() - is a function that reads data and can be stored in the structure in this example. • EX. scanf("%s", list[i].fname); • The keyword struct tells the compiler that a structure is being declared • Another lesson learned, overall the structure encapsulates all the data in an easy to access manner

More Related