Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

Arrays and Structures

Objectives
• Introduce arrays
• Introduce structures
– Connect part I and II of Fundamentals of
Programming
What is Array
• A collection of identical data objects, which are stored in
consecutive memory locations under a common heading
or a variable name
• The individual values in array are called elements and are
also variables
• Syntax
• Type arryName[arrySize]; arraySize must be constant and
refers to the # of elements the array has
• Eg: int studGrade[50];//declaration
– the array variable studGrade together with index can be used
to refer to individual elements like studGrade[3] refers to the
grade of the fourth student since arrays are zero bounded
meaning refer to 0 to arraySize-1
Initializing Arrays
• Declaring an array of local scope (within a
function) will not initialize, so its content is
undetermined.
• Declaring a global array (outside any function)
 initialize all elements to zero.
• Ex. Global array declaration: int day [7];
• every element of day will be set initially to 0:
Day 0 1 2 3 4 5 6
0 0 0 0 0 0 0
Initializing Arrays…
• when we declare an Array, we have the possibility to assign initial
values to each one of its elements using curly brackets { } .
• For example:
int day [7] = { 16, 4, 77, 40, 12, 71,88 };
Day 0 1 2 3 4 5 6
16 4 77 40 12 71 88

• Number of elements within { } must be equal or less than the length


of elements within [ ].
• Note: If we have less number of items for the initialization, the rest
will be filled with zero
• Eg int day[7]={16,2,77};
Day 0 1 2 3 4 5 6
16 4 77 0 0 0 0
Initializing Arrays…
• C++ allows the possibility of leaving empty the brackets
[ ], where the number of items in the initialization
bracket will be counted to set the size of the array
int day [ ] = { 16, 4, 7, 44, 12,99 }; //size of day set to 6
• You can use the for loop to display the content of the
arrays
– for(int index=0; index<arraySize;index++)
cout<<“day[“<<index<<“] =“<<day[index]<<“,”;
cout<<“End”;
• You can access any element eg the third day as day[2] and
to display it to the screen
cout<<day[2];
Introduction to Structures
• A structure is a collection of one or more variable
types grouped together
• Structure referred using a single name (group
name) and a member name
• Structure can be refer as a single variable, and
• You also can initialize, read and change the parts
of a structure (the individual variables that make
it up)
• Each element (called a member) in a structure
can be of different data type
Syntax:
struct [structure tag]
{
Member definition;
Member definition;

Member definition;
}[one or more structure variables];
Example
/*user defined data type Student can be later
used to declare variables of that type and the
structure is planned to store students’ record*/
struct Student
{ char ID[8];
char fullName[30];
char sex;
int age;
float CGPA;
};
Student studIs;
Initialization of Members of a Structure
• To refer to the members of a structure use dot operator (.)
• The General syntax::
VarName.Member;
• Where VarName is the variable name of the structure variable and Member is
variable name of the member of the structure
Eg:- For the above student structure:
– Initialization of the elements depends on their type (uses strcpy for string
values or array of characters and assignment operator for single values, i.e
as in fullName and CGPA respectively
strcpy(studIs.fullName, "Abebe Kebede”); //assigned Abebe Kebede as Full
Name
studIs.CGPA=3.21; //assigns 3.21 as CGPA value of Abebe Kebede
To display the content into the screen:
cout<<studIs.fullName; //display the name
cout<<studIs.CGPA; // display the CGPA of Abebe Kebede

You might also like