Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamental

Similar presentations


Presentation on theme: "Programming Fundamental"— Presentation transcript:

1 Programming Fundamental
Instructor Name: Muhammad Safyan Lecture-Structure-19

2 Introduction-Structure
Structure is an interesting part of C & C++ language. Concept of classes are quite similar to ‘structures’. Structure has the ability to store the data of multiple types Consider the example of student, different information has to be stored Name, Addresses, Date of birth, GPA and Courses of study All above information is about a single entity i.e. student.

3 Introduction-Structure
Consider another example of Car, has specification model, manufacturer company, number of seats Data processing applications requires relevant data should be grouped and handled as a group. Data in a group may be of different type unlike array. This need introduce the concept of structure. A structure is a collection of variables under a single name. These variables can be of different types, and each has a name that is used to select it from the structure”

4 Declaration of Structure
Declaration of a Structure Defined with the word struct. So struct is another keyword that cannot be used as variable name. Followed by the name of the structure. The data, contained in the structure, is defined in the curly braces. All the variables that we have been using can be part of structure. For example: struct student{ char name[60]; char address[100]; float GPA; };

5 Declaration of Structure
Structure ‘student’ containing different elements. Name of the student is declared as char array Address, we have declared an array of hundred characters. GPA, we defined it as float variable type. The variables which are part of structure are called data members . i.e. name, address and GPA are data members of student.

6 Structure After declaring structure, New data type which can be written as: student std1, std2; Here std1 and std2 are variables of type student like int x, y; This is another C and C++ language extensibility, create new data types depending upon the requirements. struct student{ char name[60]; char address[100]; float GPA; }std1, std2;

7 Structure use in Structure
We may have structure with in the other structure like struct address { char streetAddress[100]; char city[50]; char country[50]; } string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

8 Structure in Structure
Now the structure address can be a part of student structure. We can rewrite student structure as under: We may have structure with in the other structure like struct student{ char name[60]; address stdAdd; float GPA; }; Here stdAdd is a variable of type Address and a part of student structure string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

9 Array of Structure There are also arrays of structure student s[100];
card fullSet[52]; string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

10 Pointers in structure Structures can also contain pointers, pointer to something as a part of a structure We know that pointer hold the memory address of the variable. If we have a pointer to an array, it will contain the memory address of the first element of the array. Similarly, the pointer to the structure points to the starting point where the data of the structure is stored. string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

11 Pointer in Structure The pointers to structure can be defined in the following manner i.e. student *sptr; Here sptr is a pointer to a data type of structure student. string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

12 Summary of Structure Briefly speaking, we have defined a new data type. Using structures we can declare: Simple variables of new type Pointers to structure Arrays of structure string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

13 Initializing Structures
struct student{ char name[64]; char course[128]; int age; int year; }; student s1, s2, s3; student s1 = {“Ali”, “CS201”, 19, 2002 }; string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

14 Assign value to data members of Structures
Dot operator (.) operator is used to assigne the value to the data members of structure.. This may be written as: s1.age = 20; s1.year = 2002; cin>>s1.age; string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

15 Assign value to data members of Structures
Dot operator (.) operator is used to access the value of the data members of structure.. This may be written as: cout<<s1.age; cout<<s1.year ; string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

16 Assigning Structure value to other Structure
if s1 and s2 are of type student structure, we can say that s1 = s2. The assignment works because the structure is identical. So the name will be copied to the name, address to address and so on. If we want to display the structure with cout, it will also work. The cout is a very intelligent function as it interprets the structure besides showing the output. string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

17 Structure-Example main() { struct student{ char name[64]; char course[128]; int age; int year; }; student s1={"ali", "programing fundamental", 19, 2012}; cout<<"displying structure data"<<endl; cout<<"name="<<s1.name<<endl; cout<<"name="<<s1.course<<endl; cout<<"name="<<s1.age<<endl; cout<<"name="<<s1.year<<endl; getch(); } string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

18 Functions and structures
Structures are passed into functions as per the C/C++ calling conventions by value. A copy of entire structure is put on the stack. We can also pass the structures by reference to function. This can be performed in the same way we do with the normal variables i.e. pass the address of the structure to the function. This is call by reference. string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

19 Functions and structures
Array is passed by reference, If there is an array in strcture what will happened? if the array is a part of structure, it will be passed as value. Advantage The advantage of ‘pass by value’ process is that if the function makes some changes to the array elements, it does not affect the original array. Disadvantage Complete array is copied on the stack and we can run out of memory space Functions return value, int, char etc. Similarly functions can also return structures. string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

20 Pointer with structure
suppose we have a pointer to structure as student *sptr; here sptr is a pointer to student. Now s1 is a variable of type student and sptr = &s1 and sptr is pointing to s1. How can we access the data with sptr? We cannot say *sptr.name. The precedence of dot operator (.) is higher than * operator. So dot operator is evaluated first and then * operator. The compiler will give error on the above statement. To get the results, we have to evaluate * operator first i.e. (*sptr).name will give the desired result. string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

21 Functions and structures
short way to access the structure’s data member i.e. using the arrow (->) in place of dot operator. We normally use the arrow (-> i.e. minus sign and then the greater than sign) to manipulate the structure’s data with pointers. To access the name with sptr we will write: sptr->name; string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

22 main() { struct student{ char name[64]; char course[128]; int age; int year; }; student *ptr; student s1={"ali", "programing fundamental", 19, 2012}; ptr=&s1; cout<<"displying structure data"<<endl; cout<<"name="<<(*ptr).name<<endl; cout<<"name="<<(*ptr).course<<endl; cout<<"name="<<(*ptr).age<<endl; cout<<"name="<<(*ptr).year<<endl; cout<<"name="<<ptr->name<<endl; cout<<"name="<<ptr->course<<endl; cout<<"name="<<ptr->age<<endl; cout<<"name="<<ptr->year<<endl; getch(); } string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

23 Arrays of structures The declaration is similar as used to deal with the simple variables. The declaration of array of hundred students is as follows: student s[100]; To access the name of first student, the first element of the array s[0].name; string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

24 Sizeof operator Consider the student structure that contains two char arrays and two int data types. We can simply use the sizeof operator to determine its size. It will tell us how many bytes the structure is occupying. sizeof(s1); string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

25 Example-Structure Problem: Suppose we have ten students in a class. The attributes of student are name, course, age and GPA. Get the data input from the user to populate the array. Calculate the average age, average GPA of the class. Find out the grade of the class and student with max GPA. ========================== string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

26 Example-Structure Idea Solution:
declare a structure of student with name, course, age and GPA as data members. In a loop, we will get the data from the user to populate the array. Then in a loop, we will calculate the totalAge and totalGPA of the class besides determining the max GPA in that loop. Finally calculate the average age and average GPA by dividing the totalAge and totalGPA by the number of students. string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

27 main() { struct student{ char name[64]; char course[128]; int age; float gpa; }; student stu[10]; float tgpa,maxgpa, avggpa; int tage, avgage, index; tgpa=0; maxgpa=0; avggpa=0; tage=0; avgage=0; for(int i=0; i<10; i++) cout<<"enter the data of stduent no."<<i+1<<endl; cout<<"enter the sutdent name"<<endl; cin>>stu[i].name; cout<<"Enter the Student's Course : " <<endl; cin>>stu[i].course; cout<<"Enter the Student's age : " <<endl; cin>>stu[i].age; cout<<"Enter the Student's GPA : " <<endl; cin>>stu[i].gpa; } string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

28 for (int i=0; i<10; i++) { tage=tage+stu[i]. age; tgpa=tgpa+stu[i]
for (int i=0; i<10; i++) { tage=tage+stu[i].age; tgpa=tgpa+stu[i].gpa; if(stu[i].gpa>maxgpa) maxgpa=stu[i].gpa; } avgage=tage/10; avggpa=tgpa/10; cout<<"average age of the class="<<avgage<<endl; cout<<"average GPA of the class="<<avggpa<<endl; cout<<"Maximum GPA of the student="<<avggpa; getch(); string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);


Download ppt "Programming Fundamental"

Similar presentations


Ads by Google