CP Chapter 5
CP Chapter 5
All these memory locations have one collective name and type.
The elements of array is accessed with reference to its position in array, that is call index or
subscript.
Properties of arrays
Arrays in C++ are zero-bounded; that is the index of the first element in the array is 0 and the last
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element
An individual element of an array is identified by its own unique index (or subscript).
The index must be an integer and indicates the position of the element in the array. Thus the elements
elements field (which is always enclosed in square brackets []), specifies how many of these
where in intExp1 and intExp2 are constant expressions yielding positive integer values.
The two expressions, intExp1 and intExp2, specify the number of rows and the number of columns,
Form eg. declares a two-dimensional array sales of 10 rows and 5 columns, in which every
component is of type double. As in the case of a one-dimensional array, the rows are
last array subscript changing while the first subscript holds steady.
int theArray[5][3];
The first three elements go int theArray[0]; the next three into theArray[1]; and so forth.
below.
The compiler ignores the inner braces, which clarify how the numbers are distributed.
Each value should be separated by comma, regardless of whether inner braces are include.
The entire initialization must set must appear within braces, and it must end with a semicolon.
Accessing array component
To access the components of a two-dimensional array, you need a pair of indices:
one for the row position and
one for the column position.
The syntax to access a component of a two-dimensional array is:
arrayName[ indexExp1][ indexExp2]
E.g: theArray [0][2] =3
Example1 Output
#include <iostream>
s[1][1]=55;
// write this code and practice
by ur self s[2][0]=333;
s[0][0]=23; return 0;
s[0][1]=34; }
s[1][0]=44;
Example2
#include <iostream>
using namespace std;
void arr2(){
int twod[3][2]={23,34,46,56,67,89};
int sum=0;
for(int i=0;i<3;i++){
for(int j=0;j<2;j++){
sum=sum+twod[i][j];
}
}
cout<<"Sum = "<<sum<<endl;
}
Structure
Structure
Thus far we have worked with variable’s whose data types are very simple
They are a numbers of either integer or floating-point format with a specific range.
These types of variables, who only have a single value to their name, are known as basic
variables or primitives
This isn’t so bad, but what happens if you want to store two dates and not one?
You’d have to create three more variables and give them unique names:
The term structure in C++ means both a user-defined type which is a grouping of variables as
The type itself has a name, just like ‘int’, ‘double’, or ‘char’
But it is defined by the user and follows the normal rules of identifiers
Defining a structure is giving the compiler a blue print for creating your type
When you create a variable based on the structure definition, all of the member variables are created
Writing a structure definition begins with the word ‘struct’ followed by the type-to-be, and
type
Declare them as you would normal variables, but do not try to initialize them.
The data members (synonym for member variables) of a structure won’t actually be created until a
If you define a structure in a function, you will only be able to use it there.
#include <iostream>
using namespace std;
struct date{
int day, month, year;
};
int main(){
return 0;
}
You cannot initialize member variables in the structure definition ...
This is because that definition is only a map, or plan, of what a variable based on this type will be made of.
You can, however, initialize the member variables of a structure variable. To initialize a structure variable’s
members, you follow the original declaration with the assignment operator (=).
Next you define an initialization block which is a list of initializers separated by commas and enclosed in
Although you can assign a value to a variable in the same way you initialize it, the same is not true with
structures
You can use a member variable in any place you’d use a normal variable,
But you must specify it by the structure variable’s name as well as the member variable’s name using the
member operator
To specify that you want a member of a specific structure variable, you use the structure member
structure.member
int main() {
...
struct student{
student s1,s2;
int id;
cout<<"\n Enter Student Id";
Char name[22];
cin>>s1.id;
};
cout<<"\nEnter Name";
cin>>s1.name;
cout<<"\n Enter Student Id";
cin>>s2.id;
cout<<"\nEnter Name";
cin>>s2.name;
cout<<"\nStudents Information";
cout<<"\n Student id\t Student Name";
cout<<endl<<s1.id<<"\t"<<s1.name;
cout<<endl<<s2.id<<"\t"<<s2.name;
return 0;
}
Thank You!