SlideShare a Scribd company logo
1
Chapter 2
Array and Structure
Part II
Structure
Computer Programming II (Seng-2021)
2
Objectives
3
Structure
 structures are used to group related components of different types
 Components of the such a structures are called fields
 A structure is a collection of variables of different data types under a single name.
 A structure is a user-defined data type in C/C++, store Heterogeneous data type,
 Recall that elements of arrays must all be of the same type
 In some situations, we wish to group elements of different types
employee R. Jones 123 Elm 6/12/55 $14.75
scores : 85 79 92 57 68 80 . . .
employee R. Jones 123 Elm 6/12/55 $14.75
4
Cont.
 structures are used to group related components of different types
 Components of the such a structures are called fields
 In C++

called a struct
 fields called members
 C++ struct

structured data type

fixed number of components

elements accessed by name, not by index

components may be of different types
employee R. Jones 123 Elm 6/12/55 $14.75
employee R. Jones 123 Elm 6/12/55 $14.75
struct PartStruct{
char descrip [31], part_num [11];
float unit_price;
int qty; };
5
Structure Declaration
 Structure declaration creates a user defined data type that can hold different types of
logically related data items.

Eg: A structure defined to hold book database.
struct book
{
char name[20];
char author[15];
int pages;
float price;
};
 structkeyword used to declare a structure. Tells the compiler what follows is a data type definition.
 What is enclosed within the structure is the body of the structure.
 Body of the structure contains the elements of the structure, where each element declaration ends up with a
semicolon.
 Structure definition is a single statement and is terminated within a semicolon.
6
Cont.
General syntax: struct <structure-name>
{
data-type member 1;
data-type member 2;
…………………
data-type member n;
};
 Here structure –name is called the structure tag and elements are called the members.
 Structure declaration does not create any structure variables.
 It does not tell the compiler to reserve any space in memory. It just defines the form of the structure.
Declaration of Structure variables:
1. Structure variables can be declared as: Structure name var1,var2,…..var n;
Eg: struct book
{
char name[20];
char author[15];
int pages;
float price;
};
book mybook1,mybook2; Two structure variables of type book is
Eg: struct book
{
char name[20];
char author[15];
int pages;
float price;
}mybook1, mybook2;
1. It’s possible to combine the declaration of the structure
type and the variable declaration in one statement.
7
Memory allocation for the structure variables:
 Space in memory is set aside for structure variables when they are declared.
 It makes space available to hold all the elements of the structure.
 Also structure elements are placed in contiguous memory locations.
Eg: struct
{
char name[20];
char author[15];
int pages;
float price;
}mybook1;
Here 41 bytes is reserved for the variable mybook1. (20 bytes
for name, 15 bytes for author,2 bytes for pages and 4 for
price).
Initializing Structure Data
– Giving values to the members of the structure at the time of the declaration of structure variables.
– Initial values are enclosed in braces separated by commas in the order of the elements defined in the structure
declaration.
– The initialization statement must end with a semicolon.
8
Cont.
Eg: struct book
{
char name[20];
char author[15];
int pages;
float price;
}mybook1={“Let us C++”,Tom,760,320.75};
book mybook2={“Let us C+
+”,Tom,760,320.75};
Accessing Structure Elements
The structure elements can be accessed by the use of the variables of the structure and the dot operator (member operator).
For e.g:
mybook1.name;
mybook1.author;
Note:
 A structure may be local (to a function) if defined within a function. That is, no function other than the
one, which defines it, can access it.
 A structure may be global (to all functions within the program) if defined outside all functions. That is,
any functions can access it.
9
Cont.
Example 1:
//Using a structure to deal with student information
#include<iostream.h>
void main()
{
struct student
{ char name[10];
int rollno;
float mark[5];
}stud1;
cout<<"Enter the name:";
cin>>stud1.name;
cout<<"Enter the rollno:";
cin>>stud1.rollno;
for(int i=0;i<3;i++)
{
cout<<"Enter the mark for subject["<<i<<"]";
cin>>stud1.mark[i];
}
}
10
Structure and Array
 Arrays and structure can be combined together to form complex data objects.
 There may be structures within an array; also there may be array as elements of a
structure.
Arrays of Structures:

Having structures as elements of array is. arrays of structure
 For example consider the structure student in Example 1. To store the details of 100 students we
need to create an array of structure ‘student’.

To declare an array of structure, define a structure, then declare an array variable of that type.

E.g: student stud[100];

To access specific structure, index the structure name. E.g: stud[1].name;
11
Cont.
Example 2: //Array of student structure
#include<iostream.h>
void main()
{ struct student
{ char name[10];
int rollno;
float mark[5];
}stud[10];
int n,i,j;
cout<<"Enter the number of students;";
cin>>n;
for(i=0;i<n;i++)
{ cout<<"Enter the name:";
cin>>stud[i].name;
cout<<"Enter the rollno:";
cin>>stud[i].rollno;
for(j=0;j<3;j++)
{ cout<<"Enter the mark for
subject["<<j<<"]"; cin>>stud[i].mark[j];
}
}
cout<<"nName:tRollnotMark1tMark2tMark3n";
for(i=0;i<n;i++)
{ cout<<"n"<<stud[i].name<<"
t"<<stud[i].rollno<<"t";
for(j=0;j<3;j++)
{
cout<<stud[i].mark[j]<<"t";
}}}
12
Nested Structures:
Structures as members of another structure.
Eg: struct Address
{
int hno;
char city[10];
};
struct student
{
char name[10];
Address add;
int rollno;
float mark[5];
}stud;
Members of the nested structure is referenced from the outermost to innermost with the help of dot
operators.
stud.add.hno=10;
Assignment of a complete structure

One structure variable can be assigned to another only when they are of the same structure type.

Only assignment is possible for two similar structures. Other operations such as comparisons are not
possible.
13
Passing & Returning Structures in Functions
 A function is a named unit of a group of program statements which can be invoked from other parts of
the program.
 If structure and structure variable is global then it is available to all functions in the program and so
not necessary to pass as arguments.
 If a structure is local to a function and if its value is needed in other functions,
 we can pass the structure as arguments to the function.
 It can be achieved in 2 ways:
1) by passing individual structure elements.
(2) by passing entire structure.
 For e.g: function1(stud1.name,stud1.rollno);
 here function1 is called by passing values of individual structure elements of structure student.
 Now, If the values of structure elements should not be altered by the function, then structure elements
are passed by value and if the values of structure elements can be altered by the function, then structure
elements are passed by reference.
 Array elements will automatically be passed by reference as the arrays cannot be passed by value.
 Passing entire structure makes the most sense when the structure is relatively compact. Entire
structure can be passed by value or reference.
14
Call by reference
Exampe: Call by reference
#include<iostream.h>
struct student
{
char name[10];
int rollno;
float mark[5];
};
void read(student *stud)
{
int i;
cout<<"Enter the name:";
cin>>(*stud).name;
cout<<"Enter the rollno:";
cin>>(*stud).rollno;
for(i=0;i<3;i++)
{
cout<<"Enter the mark for subject["<<i<<"]";
cin>>(*stud).mark[i];
}
}
void display(student *stud)
{
cout<<"Display...........n";
cout<<"n"<<(*stud).name<<"
t"<<(*stud).rollno<<"t";
for(int j=0;j<3;j++)
{
cout<<(*stud).mark[j]<<"t";
}}
void main()
{
student st;
read(&st);
display(&st);
}
15
The arrow operator (->)
 This is a dereference operator that is used exclusively with pointers to objects with
members.
 This operator serves to access a member of an object to which we have a reference.
 In the example we used: (*stud).name which is equivalent to stud->name;
 Both expressions (*stud).name and stud->name;
 are valid and both mean that we are evaluating the member name of the structure pointed by a
pointer called stud.
 Possible combinations of pointers and structure members:
Expression What is evaluated Equivalent
a.b Member b of object a
a->b Member b of object pointed by a (*a).b
*a.b Value pointed by member b of object a *(a.b)
16
Function Returning Structures
 If a function read() wants to return a structure of type student its declaration is as
student read().
 If the return value of the function read() is being assigned to a variable, then that
variable must be a structure variable of type student. student std=read();
Example: Call by value & function returning structure
#include<iostream.h>
struct student
{
char name[10];
int rollno;
float mark[5];
};
student read(student stud)
{
int i;
cout<<"Enter the name:"; cin>>stud.name;
cout<<"Enter the rollno:";
cin>>stud.rollno;
for(i=0;i<3;i++)
17
Cont.
cout<<"Enter the mark for subject["<<i<<"]";
cin>>stud.mark[i];
}
return stud;
}
void display(student stud)
{
cout<<"Display...........n";
cout<<"n"<<stud.name<<"
t"<<stud.rollno<<"t";
for(int j=0;j<3;j++)
{
cout<<stud.mark[j]<<"t";
}
}
void main()
{
student st;
st=read(st);
display(st);
}
18
Cont.
Example: Passing Array of Structure in function
#include<iostream.h>
struct student
{
char name[10];
int rollno;
float mark[5];
};
int read(student stud[])
{int i,n,j;
cout<<"Enter the total number of students:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the name:";
cin>>stud[i].name;
cout<<"Enter the rollno:";
cin>>stud[i].rollno;
for(j=0;j<3;j++)
{
cout<<"Enter the mark for
subject["<<i<<"]";
cin>>stud[i].mark[j];
}
}return n;
void display(student stud[],int n)
{ for(int i=0;i<n;i++)
{ cout<<"n"<<stud[i].name<<"
t"<<stud[i].rollno<<"t";
for(int j=0;j<3;j++)
{ cout<<stud[i].mark[j]<<"
t";
}}}
void main()
{
student st[10]; int s;
s=read(st);
display(st,s);
}
19
Work sheet
Suppose you are designing a program to store information about employees in a company. You decide to
use a struct to represent the employee's data. The struct definition is as follows:
1) Declare a variable named "emp1" of type "Employee".
2) Initialize the "emp1" variable with the following values:
employeeID: 1001
name: "John Smith"
age: 32
salary: 50000.0
3) Declare another variable named "emp2" of type "Employee".
4) Prompt the user to enter values for the "emp2" variable, including the employeeID, name, age, and
salary.
5) Write a function named "printEmployee" that takes an "Employee" object as a parameter and prints the
employee's information in the following format:
6) Example output: "Employee ID: 1001, Name: John Smith, Age: 32, Salary: $50000.00"
7) Call the "printEmployee" function twice, once for each of the "emp1" and "emp2" variables.
Any Question

More Related Content

Similar to Chapter 2 part II array and structure.pptx (20)

PPTX
data structure and c programing concepts
kavitham66441
 
PDF
Principals of Programming in CModule -5.pdf
anilcsbs
 
PDF
Pointers and Structures
Gem WeBlog
 
PPT
C Language_PPS_3110003_unit 8ClassPPT.ppt
NikeshaPatel1
 
PPTX
Structure
Rokonuzzaman Rony
 
PDF
Structure In C
yndaravind
 
PPTX
Structure in c language
sangrampatil81
 
PPTX
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
PPTX
U5 SPC.pptx
thenmozhip8
 
PPT
Structures
archikabhatia
 
PPT
C1320prespost
FALLEE31188
 
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
PPT
Structures and Unions in C-Language with Examples.ppt
IQACGCN
 
PPTX
Chapter 2 OOP using C++ (Introduction).pptx
FiraolGadissa
 
DOC
Unit 5 (1)
psaravanan1985
 
PPTX
Coding - L30-L31-Array of structures.pptx
happycocoman
 
PPTX
Structured Languages
Mufaddal Nullwala
 
PPTX
C programing -Structure
shaibal sharif
 
data structure and c programing concepts
kavitham66441
 
Principals of Programming in CModule -5.pdf
anilcsbs
 
Pointers and Structures
Gem WeBlog
 
C Language_PPS_3110003_unit 8ClassPPT.ppt
NikeshaPatel1
 
Structure In C
yndaravind
 
Structure in c language
sangrampatil81
 
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
U5 SPC.pptx
thenmozhip8
 
Structures
archikabhatia
 
C1320prespost
FALLEE31188
 
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
Structures and Unions in C-Language with Examples.ppt
IQACGCN
 
Chapter 2 OOP using C++ (Introduction).pptx
FiraolGadissa
 
Unit 5 (1)
psaravanan1985
 
Coding - L30-L31-Array of structures.pptx
happycocoman
 
Structured Languages
Mufaddal Nullwala
 
C programing -Structure
shaibal sharif
 

Recently uploaded (20)

PDF
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
DOCX
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
PDF
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
 
PDF
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
PDF
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
 
PPTX
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
 
PDF
Best Software Development at Best Prices
softechies7
 
PDF
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
 
PPTX
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
PDF
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
PDF
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
PDF
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
DOCX
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
 
PPTX
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
 
PDF
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
 
PDF
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
 
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
 
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
 
Best Software Development at Best Prices
softechies7
 
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
 
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
 
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
 
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
 
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
Ad

Chapter 2 part II array and structure.pptx

  • 1. 1 Chapter 2 Array and Structure Part II Structure Computer Programming II (Seng-2021)
  • 3. 3 Structure  structures are used to group related components of different types  Components of the such a structures are called fields  A structure is a collection of variables of different data types under a single name.  A structure is a user-defined data type in C/C++, store Heterogeneous data type,  Recall that elements of arrays must all be of the same type  In some situations, we wish to group elements of different types employee R. Jones 123 Elm 6/12/55 $14.75 scores : 85 79 92 57 68 80 . . . employee R. Jones 123 Elm 6/12/55 $14.75
  • 4. 4 Cont.  structures are used to group related components of different types  Components of the such a structures are called fields  In C++  called a struct  fields called members  C++ struct  structured data type  fixed number of components  elements accessed by name, not by index  components may be of different types employee R. Jones 123 Elm 6/12/55 $14.75 employee R. Jones 123 Elm 6/12/55 $14.75 struct PartStruct{ char descrip [31], part_num [11]; float unit_price; int qty; };
  • 5. 5 Structure Declaration  Structure declaration creates a user defined data type that can hold different types of logically related data items.  Eg: A structure defined to hold book database. struct book { char name[20]; char author[15]; int pages; float price; };  structkeyword used to declare a structure. Tells the compiler what follows is a data type definition.  What is enclosed within the structure is the body of the structure.  Body of the structure contains the elements of the structure, where each element declaration ends up with a semicolon.  Structure definition is a single statement and is terminated within a semicolon.
  • 6. 6 Cont. General syntax: struct <structure-name> { data-type member 1; data-type member 2; ………………… data-type member n; };  Here structure –name is called the structure tag and elements are called the members.  Structure declaration does not create any structure variables.  It does not tell the compiler to reserve any space in memory. It just defines the form of the structure. Declaration of Structure variables: 1. Structure variables can be declared as: Structure name var1,var2,…..var n; Eg: struct book { char name[20]; char author[15]; int pages; float price; }; book mybook1,mybook2; Two structure variables of type book is Eg: struct book { char name[20]; char author[15]; int pages; float price; }mybook1, mybook2; 1. It’s possible to combine the declaration of the structure type and the variable declaration in one statement.
  • 7. 7 Memory allocation for the structure variables:  Space in memory is set aside for structure variables when they are declared.  It makes space available to hold all the elements of the structure.  Also structure elements are placed in contiguous memory locations. Eg: struct { char name[20]; char author[15]; int pages; float price; }mybook1; Here 41 bytes is reserved for the variable mybook1. (20 bytes for name, 15 bytes for author,2 bytes for pages and 4 for price). Initializing Structure Data – Giving values to the members of the structure at the time of the declaration of structure variables. – Initial values are enclosed in braces separated by commas in the order of the elements defined in the structure declaration. – The initialization statement must end with a semicolon.
  • 8. 8 Cont. Eg: struct book { char name[20]; char author[15]; int pages; float price; }mybook1={“Let us C++”,Tom,760,320.75}; book mybook2={“Let us C+ +”,Tom,760,320.75}; Accessing Structure Elements The structure elements can be accessed by the use of the variables of the structure and the dot operator (member operator). For e.g: mybook1.name; mybook1.author; Note:  A structure may be local (to a function) if defined within a function. That is, no function other than the one, which defines it, can access it.  A structure may be global (to all functions within the program) if defined outside all functions. That is, any functions can access it.
  • 9. 9 Cont. Example 1: //Using a structure to deal with student information #include<iostream.h> void main() { struct student { char name[10]; int rollno; float mark[5]; }stud1; cout<<"Enter the name:"; cin>>stud1.name; cout<<"Enter the rollno:"; cin>>stud1.rollno; for(int i=0;i<3;i++) { cout<<"Enter the mark for subject["<<i<<"]"; cin>>stud1.mark[i]; } }
  • 10. 10 Structure and Array  Arrays and structure can be combined together to form complex data objects.  There may be structures within an array; also there may be array as elements of a structure. Arrays of Structures:  Having structures as elements of array is. arrays of structure  For example consider the structure student in Example 1. To store the details of 100 students we need to create an array of structure ‘student’.  To declare an array of structure, define a structure, then declare an array variable of that type.  E.g: student stud[100];  To access specific structure, index the structure name. E.g: stud[1].name;
  • 11. 11 Cont. Example 2: //Array of student structure #include<iostream.h> void main() { struct student { char name[10]; int rollno; float mark[5]; }stud[10]; int n,i,j; cout<<"Enter the number of students;"; cin>>n; for(i=0;i<n;i++) { cout<<"Enter the name:"; cin>>stud[i].name; cout<<"Enter the rollno:"; cin>>stud[i].rollno; for(j=0;j<3;j++) { cout<<"Enter the mark for subject["<<j<<"]"; cin>>stud[i].mark[j]; } } cout<<"nName:tRollnotMark1tMark2tMark3n"; for(i=0;i<n;i++) { cout<<"n"<<stud[i].name<<" t"<<stud[i].rollno<<"t"; for(j=0;j<3;j++) { cout<<stud[i].mark[j]<<"t"; }}}
  • 12. 12 Nested Structures: Structures as members of another structure. Eg: struct Address { int hno; char city[10]; }; struct student { char name[10]; Address add; int rollno; float mark[5]; }stud; Members of the nested structure is referenced from the outermost to innermost with the help of dot operators. stud.add.hno=10; Assignment of a complete structure  One structure variable can be assigned to another only when they are of the same structure type.  Only assignment is possible for two similar structures. Other operations such as comparisons are not possible.
  • 13. 13 Passing & Returning Structures in Functions  A function is a named unit of a group of program statements which can be invoked from other parts of the program.  If structure and structure variable is global then it is available to all functions in the program and so not necessary to pass as arguments.  If a structure is local to a function and if its value is needed in other functions,  we can pass the structure as arguments to the function.  It can be achieved in 2 ways: 1) by passing individual structure elements. (2) by passing entire structure.  For e.g: function1(stud1.name,stud1.rollno);  here function1 is called by passing values of individual structure elements of structure student.  Now, If the values of structure elements should not be altered by the function, then structure elements are passed by value and if the values of structure elements can be altered by the function, then structure elements are passed by reference.  Array elements will automatically be passed by reference as the arrays cannot be passed by value.  Passing entire structure makes the most sense when the structure is relatively compact. Entire structure can be passed by value or reference.
  • 14. 14 Call by reference Exampe: Call by reference #include<iostream.h> struct student { char name[10]; int rollno; float mark[5]; }; void read(student *stud) { int i; cout<<"Enter the name:"; cin>>(*stud).name; cout<<"Enter the rollno:"; cin>>(*stud).rollno; for(i=0;i<3;i++) { cout<<"Enter the mark for subject["<<i<<"]"; cin>>(*stud).mark[i]; } } void display(student *stud) { cout<<"Display...........n"; cout<<"n"<<(*stud).name<<" t"<<(*stud).rollno<<"t"; for(int j=0;j<3;j++) { cout<<(*stud).mark[j]<<"t"; }} void main() { student st; read(&st); display(&st); }
  • 15. 15 The arrow operator (->)  This is a dereference operator that is used exclusively with pointers to objects with members.  This operator serves to access a member of an object to which we have a reference.  In the example we used: (*stud).name which is equivalent to stud->name;  Both expressions (*stud).name and stud->name;  are valid and both mean that we are evaluating the member name of the structure pointed by a pointer called stud.  Possible combinations of pointers and structure members: Expression What is evaluated Equivalent a.b Member b of object a a->b Member b of object pointed by a (*a).b *a.b Value pointed by member b of object a *(a.b)
  • 16. 16 Function Returning Structures  If a function read() wants to return a structure of type student its declaration is as student read().  If the return value of the function read() is being assigned to a variable, then that variable must be a structure variable of type student. student std=read(); Example: Call by value & function returning structure #include<iostream.h> struct student { char name[10]; int rollno; float mark[5]; }; student read(student stud) { int i; cout<<"Enter the name:"; cin>>stud.name; cout<<"Enter the rollno:"; cin>>stud.rollno; for(i=0;i<3;i++)
  • 17. 17 Cont. cout<<"Enter the mark for subject["<<i<<"]"; cin>>stud.mark[i]; } return stud; } void display(student stud) { cout<<"Display...........n"; cout<<"n"<<stud.name<<" t"<<stud.rollno<<"t"; for(int j=0;j<3;j++) { cout<<stud.mark[j]<<"t"; } } void main() { student st; st=read(st); display(st); }
  • 18. 18 Cont. Example: Passing Array of Structure in function #include<iostream.h> struct student { char name[10]; int rollno; float mark[5]; }; int read(student stud[]) {int i,n,j; cout<<"Enter the total number of students:"; cin>>n; for(i=0;i<n;i++) { cout<<"Enter the name:"; cin>>stud[i].name; cout<<"Enter the rollno:"; cin>>stud[i].rollno; for(j=0;j<3;j++) { cout<<"Enter the mark for subject["<<i<<"]"; cin>>stud[i].mark[j]; } }return n; void display(student stud[],int n) { for(int i=0;i<n;i++) { cout<<"n"<<stud[i].name<<" t"<<stud[i].rollno<<"t"; for(int j=0;j<3;j++) { cout<<stud[i].mark[j]<<" t"; }}} void main() { student st[10]; int s; s=read(st); display(st,s); }
  • 19. 19 Work sheet Suppose you are designing a program to store information about employees in a company. You decide to use a struct to represent the employee's data. The struct definition is as follows: 1) Declare a variable named "emp1" of type "Employee". 2) Initialize the "emp1" variable with the following values: employeeID: 1001 name: "John Smith" age: 32 salary: 50000.0 3) Declare another variable named "emp2" of type "Employee". 4) Prompt the user to enter values for the "emp2" variable, including the employeeID, name, age, and salary. 5) Write a function named "printEmployee" that takes an "Employee" object as a parameter and prints the employee's information in the following format: 6) Example output: "Employee ID: 1001, Name: John Smith, Age: 32, Salary: $50000.00" 7) Call the "printEmployee" function twice, once for each of the "emp1" and "emp2" variables.