230 likes | 317 Views
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;
E N D
Group 3Presents… ARRAY OF STRUCTURES By Andres Quintero Oskar Pio Philip Dacosta Javier Palomino Christopher Ballesteros Calvin Remy
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;
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];
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;
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;
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];
Outline Define Structures Create Variables used User Interface loop (Three Data Inputs * Four) Output
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.
Line 15 struct entry list[4]; • This declares a 4-element array of structures of type entry.
Lines 19-45 main() function
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.
Line 36 printf("\n\n"); • This just prints two blank lines between the user input area and the output results
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
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