110 likes | 270 Views
Introduction Algorithm & Structure Data. Part III By Suzila Yusof. Structure Data & Array. Array and structure are two kinds of data structures storage in C + +. Array is a collection data consisting from same data type.
E N D
Introduction Algorithm & Structure Data Part III By Suzila Yusof
Structure Data & Array • Array and structure are two kinds of data structures storage in C + +. • Array is a collection data consisting from same data type. • The structure is a collection data consisting from different data types. • Defined as an array data structure is static, in which the user must define the size of an array when it is defined.
Array • A data collection consisting of a list of elements under a single name, in which elements of the same type of data. • Each element is referred using an index or subscript. • The number of elements in the array is finite and fixed. • Array can be divided into two - one-dimensional - multidimensional
Type of Array Structure • Defining two variables, such as pAli and pSani records for the structure of the student may be referred to a variable because it uses the same type of structure. • This can be solved using a variable array of type structures.
Example: Struct pelajar pelajarJTMK[10]; • Gambaran tatasusunan setelah pengisytiharan di atas dilakukan. Ahli data dalam struct pelajar pelajarJTMK pelajarJTMK[0] pelajarJTMK[1] pelajarJTMK[2] : pelajarJTMK[10]
Type of Array Structure • Size of array can be ignored if the initial value is given during the declaration. • Structured data elements in the array is also referred to by using subscripts. • Example : - looping for is used to change the subscript numbers to enable them one by one put in array data.
Example of coding cout<<"Masukkan rekod 10 pelajar seksyen 1/n"; for (i=0; i<=10; i++) { cout<<"NO METRIK:"; cin >>pelajarJTMK[i].nometrik; cout<<"NAMA:"; cin >>pelajarJTMK[i].nama; cout<<"TAJUK PROJEK:"; cin >>pelajarJTMK[i].projek; }
Example declaration struct KoleksiCD { char Tajuk[15]; char Artis[35]; int JumLagu; float Harga; char TarikhBeli[15]; }myCD[5];
Operations input to the first element cin>> myCD[0].Tajuk; cin>> myCD[0].Artis; cin>> myCD[0].JumLagu; cin>> myCD[0].Harga; cin>> myCD[0].TarikhBeli;
Operations output to the first element cout<<“Tajuk CD :” myCD[0].Tajuk<<endl; cout<<“Artis :” myCD[0].Artis<<endl; cout<<“Jumlah lagu :” myCD[0].JumLagu<<endl; cout<<“Harga :” myCD[0].Harga<<endl; cout<<“Tarikh Beli :” myCD[0].TarikhBeli<<endl;
Use Loop Control Structures For (i=0; i<10; i++) { cout<<“Tajuk CD :” myCD[0].Tajuk<<endl; cout<<“Artis :” myCD[0].Artis<<endl; cout<<“Jumlah lagu :” myCD[0].JumLagu<<endl; cout<<“Harga :” myCD[0].Harga<<endl; cout<<“Tarikh Beli :” myCD[0].TarikhBeli<<endl; }