F4104 : DATA STRUCTURE Laboratory Exercises LAB 2: Data Structure and Array Duration: 2 Hours Learning Outcomes This Labsheet encompasses activities 2A, 2B, and 2C. By the end of this laboratory session, you should be able to: 1. Differentiate between data structure and array. 2. Explain and give example the relationship between data structure and array. 3. Define, declare access array of data structure. 4. Use array of data structures as function argument 5. Use data structure as an array member 6. Differentiate between array of data structures and array as a data structure member. 7. Write program using array of data structures and array as a data structure. Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0) Activity 2A Activity Outcome: Write, compile and run a program using structure in C++. Program structArray.cpp will create a structure named studentRec contains four members and one structure variable with array data typed. The array size is 50. Using for loop, then insert the values in the structure. Procedure: Step 1: Open C++ Programming and type the following sample structure code contains three members. // an array structure of student information #include <iostream> using namespace std; struct studentRec { char id[6];// student id number, max. 5 integer number char name[50]; // student name, max 49 characters char gender; // student gender Male or Female int age; // student age }; Step 2: Then define a variable array of studentRec with 20 size void main() { // declaring array of 20 element of structure type // and some of the element also are arrays struct studentRec student[20]; Page 1 of 6 F4104 : DATA STRUCTURE Laboratory Exercises int i = 0; } Step 3: Assign the values in the structure. cout<<"Keying in student data and then display\n"; cout<<"---------------------------------------\n"; cout<<"Enter student data\n"; for(i=0; i<2; i++) { // storing the data cout<<"\nID number (4 integer number) student #"<<i<<": "; cin>>student[i].id; cout<<"First name student #"<<i<<": "; cin>> student [i].name; cout<<"Gender (M or F) student #"<<i<<": "; cin>> student [i].gender; cout<<"Age student #"<<i<<": "; cin>> student [i].age; } Step 4: displaying the data stored cout<<"\n----------Display the data---------\n"; cout<<"You can see that the data storage\n"; cout<<"has been reserved for the structure!\n"; cout<<"------------------------------------\n"; for(i=0; i<2; i++) { // displaying the stored data cout<<"\nID number student # "<<i<<": "<<stud[i].id; cout<<"\nFirst name student # "<<i<<": "<<stud[i].name; cout<<"\nGender student cout<<"\nAge student # "<<i<<": "<<stud[i].gender; # "<<i<<": "<<stud[i].age<<"\n"; } } Step 5: Save your activity as structArray.cpp Step 6: Compile and Run the program and see the output appeared. Activity 2B Activity Outcome: Write, compile and run a program using typedef in C++. Page 2 of 6 F4104 : DATA STRUCTURE Laboratory Exercises This program will show the student structure that contains various data type members. You will assign values to structure by input from user. Procedure: Step 1: Open C++ Programming and type the following code. #include<iostream.h> #include<string.h> #define BIL 3 void main() { /* Declare student struct containing various data type members */ struct student { char name[20]; char IDNo[11]; char Class[5]; int year; float cgpa; }DIP_Student[BIL]; /* Declare array structure */ int i; // Assign values to struct through input by user for( i = 0; i <= BIL; i++ ) { cout<<“Student’s Name : ”; cin>>DIP_Student[i].name; cout<<“Student’s ID : ”; cin>>DIP_Student[i].IDNo; cout<<“Class : ”; cin>>DIP_Student[i].Class; cout<<“Year : ”; cin>>DIP_Student[i].year; cout<<“CGPA : ”; cin>>DIP_Student[i].cgpa; } Page 3 of 6 F4104 : DATA STRUCTURE Laboratory Exercises // Print out student’s details for( i = 0; i <= BIL; i++ ) { cout<<“Student’s Details ”<<i<<endl; cout<<“Student Name : ”<<DIP_Student[i].name <<endl; cout<<“Student ID : ”<<DIP_Student[i].IDNo <<endl; cout<<“Class : ”<<DIP_Student[i].Class <<endl; cout<<“Year : ”<<DIP_Student[i].year <<endl; cout<<“CGPA : ”<<DIP_Student[i].cgpa <<endl; cout<<endl; } } Step 2: Save your program as Activity2B.cpp. Step 3: Compile and run the program. Correct the errors if any Step 4: Write the output. Activity 2C Activity Outcome: Write, compile and run a program in C++. This program will used to calculate number of days starting from 1st Jan for a date inserted. Procedure: Step 1: Open C++ Programming and type the following code. #include <iostream.h> struct date { Page 4 of 6 F4104 : DATA STRUCTURE Laboratory Exercises int date; int month; int year; }; struct date readDate(); int calculateDays(struct date); int daysInMonth(int, int); void main() { struct date tkh; int bil; tkh = readDate(); bil = calculateDays(tkh); cout<<tkh.date<<“ /”<<tkh.month<<“/”<<tkh.year<< “is the ”<<bil<<“ days in a year ”<<tkh.year<<endl; } struct date readDate() { struct date t; cout<<“Enter date with dd/mm/yyyy format : ”; cin>>t.date>>t.month>>t.year; return t; } int calculateDays(struct date t) { int n = 0, i; for(i=1; i<t.month; i++) n += daysInMonth(i, t.year); n += t.date; return n; } int daysInMonth(int month, int yr) { switch(month) { case 2 : if (yr % 4 == 0) return 29; else return 28; case 4 : case 6 : case 9 : case 11 : return 30; default: return 31; } } Step 2: Save your program as Activity2C.cpp. Step 3: Compile and run the program. Correct the errors if any Step 4: Write the output. Page 5 of 6 F4104 : DATA STRUCTURE Page 6 of 6 Laboratory Exercises