Your SlideShare is downloading. ×
0
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

FP 201 - Unit4 Part 2

370

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
370
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. F2037 - PROGRAMMINGFUNDAMENTAL WITH C++Unit 4.2 - Understand the use of structures
  • 2. INDEX Objective Introduction to Structures Declare structure variable Assign and access values of structure variable Array of structure Write program using structures
  • 3. OBJECTIVES At the end of this module, students should:  Declare and use structure variable  Identify the difference between structures and array  Use array in structure
  • 4. INTRODUCTION Array can be used to store and process large amount of similar data. However, it is limited as does not allow to store and manipulate dissimilar data items. Structure can be used to store and manipulate dissimilar data items.
  • 5. INTRODUCTION TO STRUCTURE Structure is a collection of related data items stored in one place and referenced under one name. Each data item do not have to be the same type. struct student { char id [5]; char name [18]; char gender[10]; int age; };
  • 6. INTRODUCTION TO STRUCTUREIdNameGenderage
  • 7. STRUCTURE DECLARATION Used struct keyword The variable in a structure are called structure elements or members Syntax struct <structure name> { <struct member>; };
  • 8. STRUCTURE DECLARATION
  • 9. EXERCISE Create a struct named car with structure member type and year
  • 10. struct car{ char type[10]; int year;};
  • 11. DECLARING STRUCTURE VARIABLE First method struct student { char id [ 5 ]; char name [ 18 ]; char gender[10]; int age; } stud_1, stud_2; variable
  • 12. DECLARING STRUCTURE VARIABLE Second method struct student { char id [ 5 ]; char name [ 18 ]; char gender[10]; int age; }; variable struct student stud_1, stud_2;
  • 13. ASSIGN & ACCESS THE STRUCTURE A structure element can be accessed and assigned a value by using the structure variable name, the dot operator and the element’s name. For example;  stud_1.name = “Miriam”;  stud_1.age = 21;
  • 14. EXAMPLE#include<iostream>using namespace std;struct student{ char id[10]; char name[25]; int age;};void main(){ struct student stud1 = {"123","Ani",12}; cout<<"Student id: "<<stud1.id<<endl; cout<<"Student name: "<<stud1.name<<endl; cout<<"Student age: "<<stud1.age<<endl;}
  • 15. #include<iostream>using namespace std;struct student{ char id[10]; char name[25]; int age;}stud1={"123","Ani",12};void main(){ cout<<"Student id: "<<stud1.id<<endl; cout<<"Student name: "<<stud1.name<<endl; cout<<"Student age: "<<stud1.age<<endl;}
  • 16. #include<iostream>using namespace std;struct student{ char id[10]; char name[25]; int age;}stud1;void main(){ cout<<"enter your id:"; cin.getline(stud1.id,10); cout<<"enter your name:"; cin.getline(stud1.name,25); cout<<"enter your age:"; cin>>stud1.age; cout<<"nDisplay result"<<endl; cout<<"your id is:"<< stud1.id<<endl; cout<<"your name is:"<< stud1.name<<endl; cout<<"your age is:"<< stud1.age<<endl;}
  • 17. EXERCISE Write a program to declare a structure named parent with structure member name and age. Create two structure variable named father and mother . Assign these two variable with the value name of your father and mother and their age. Display all of these value
  • 18.  Write a program to declare a structure named parent with structure member name and age. #include<iostream.h> struct parent{ char name[25]; int age; };
  • 19.  Create two structure variable named father and mother . Assign these two variable with the value name of your father and mother and their age. #include<iostream.h> struct parent{ char name[25]; int age; }; void main(){ struct parent father={"Ngadengon", 73}; struct parent mother={"Satirah", 66};}
  • 20.  Display all of these value#include<iostream.h>struct parent{ char name[25]; int age; }; void main(){ struct parent father={"Ngadengon", 73}; struct parent mother={"Satirah", 66}; cout<<"Father:"<<father.name<<" Age:"<<father.age; cout<<"nMother:"<<mother.name<<" Age:"<<mother.age;}
  • 21. ARRAY OF STRUCTURE struct student { char id [5]; char name [18]; char gender[10]; int age; }; struct student stud [2];
  • 22. #include<iostream>using namespace std;struct student{ char id[10]; char name[25]; int age;}stud1[2];void main(){ for(int i=0;i<2;i++){ cout<<"nenter your id:"; cin>>stud1[i].id; cout<<"enter your name:"; cin>>stud1[i].name; cout<<"enter your age:"; cin>>stud1[i].age; } cout<<"nDisplay result"<<endl; for(int x=0;x<2;x++){ cout<<"your id is:"<< stud1[x].id<<endl; cout<<"your name is:"<< stud1[x].name<<endl; cout<<"your age is:"<< stud1[x].age<<endl; }}
  • 23. SUMMARY struct is a reserved keyword Members of struct can also be functions, including constructors and destructors. Member of struct by default are public.

×