Chapter 3
Chapter 3
Computer programming 1
Cont..
In C++, square brackets bound all the index right after the name of the
array.
2
1 Array Elements
A B C D E
0 1 2 3 4
Index Array
length = 5
First
index = 0 3
Last
index = 4
Computer programming 2
Properties of arrays
Computer programming 3
Array declaration
Computer programming 4
Initializing Arrays
Computer programming 5
Initializing Arrays
Garbage allocation: defines data type, name & size of array.
Example: int grade[6];
Specify value: specify the data type, name & value of array.
Example: int grade[ ]= {98, 69,78, 86, 53}; //the best style
Specify value and size: defines all parameters of the array.
Example: int Age[3]={12,6,8};
Specify size and null : this rule miss the value of array.
Computer programming 6
Cont..
The number of constants in the initialize list must be
Less than or equal to size of the array.
The other way for initialization is passing a value for each indexed
array.
Example: int Age[3];
Age[0]=12; This style consumes memory and
time
Age[1]=6;
Age[2]=8;
Computer programming 7
Cont..
An array can be initialized to be all zeros by using an empty
initialization list.
Example:
float salary [ ] = {0, 0, 0, 0, 0, 0};
float salary [6] = {0, 0, 0};
float salary [6] = {0, 0, 0, 0, 0, 0};
Computer programming 8
Designated Initialization
When the value of an array is not assigned sequentially, we have
used designated initialization.
Example: int array [10 ] = {1, 0, 0, 0, 0, 2, 3, 0, 0, 0};
Here, we want
1 in position 0;
2 in position 5 and
3 in position 6
Computer programming 9
Cont..
When the initialization contains different styles, the designated
initialization wins other techniques.
Example: int a [ ] = {1, 2, 3, [2] = 4, [6] = 45};
What will be the value of the array a(2)?
The above array is equivalent to
int a [] = {1, 2, 4, 0, 0, 0, 45};
The index of the array never be negative value.
Computer programming 10
Example
#include<iostream>
using namespace std;
int main()
{
int array[5]={12,23,34,45,56};
cout<<"output of the above declaration is\n";
cout<<array[0]<<endl;
cout<<array[1]<<endl;
cout<<array[2]<<endl;
cout<<array[3]<<endl;
cout<<array[4]<<endl;
return 0;
}
Computer programming 11
Cont..
The above code can be easily written as follows:-
#include<iostream>
using namespace std;
int main()
{
int array[5]={12,23,34,45,56};
cout<<"output of the above declaration is\n";
for(int i=0;i<=4;i++)
cout<<array[i]<<endl;
return 0;
}
Computer programming 12
Cont..
Write c++ code that accept numbers from user input and display it.
#include<iostream>
using namespace std;
int main()
{
int n, num [100];
cout<<“Enter the number of array you want\n";
cin>>n;
for (int i=0; i<=n-1; i++)
{
cout<<“Enter Your "<< i <<" array ";
cin>>num[i];
}
cout<<"the contents of the array are\n";
for (int j=0;j<=n-1;j++)
cout<<"the "<<j <<"array is " <<num[j]<<endl;
return 0;
}
Computer programming 13
Common Errors on Array
Computer programming 14
Multidimensional Array
Computer programming 15
Cont..
Computer programming 16
Cont..
Computer programming 17
Size of Multidimensional Array
The length of an array can be specified by any positive integer
constant expression (int arr[5+5] ;).
The total number of elements stored in multidimensional array can
be calculated by multiplying size of all dimensions.
Example:
Float x[10][10]; // total of 100 elements with two
dimensions
int age[3[[6]; // age has 18 elements
int x [2][2] ={1,2,3,4}; // two column and two rows
int x [2][3]={{1,2,3},{4,5,6}}; //best one
Computer programming 18
Cont..
Computer programming 19
Cont..
// a program to accept input and cout<<"enter the "<<i<<" row and enter
display its content "<<j<<" column ";
cin>>nums[i][j]; }}
#include<iostream.h>
cout<<"the output is \n";
int main()
for (i=0; i<2; i++)
{ {
int i, j; cout<<" row "<<i<<"\t";
int nums[2][2]; for (j=0; j<2; j++)
for (i=0; i<2; i++) {
{ cout<<nums[i][j]<<"\t";
}
cout<<"the "<<i<<" row \n";
cout<<endl;
for (j=0; j<2; j++)
}
{ return 0;
}
Computer programming 20
Constant Array
Either one dimensional or two dimensional array can be made
constant by starting declaration with the keyword const.
One the is given a value, we can not modify that value.
Example const int array[5]={12,23,34,45,56};
Let as try to change one value of the array.
Array[2] = 45; //gives a response of Error
If we have very large elements in an array, how we determine the
size of array?
Use ‘sizeof’ operator (sizeof(array)).
Computer programming 21
Strings of Characters
What are Strings?
Strings of characters that allow us to represent successive
characters
Like words, sentences, names, texts, etc
Strings are used for storing texts and it should be surrounded by
either single or double quotes.
Computer programming 22
Declaring and Initialization of Strings
Example:
char mystring [] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char mystring [] = "Hello";
In both cases:
The Array or string of characters mystring is declared with a
size of 6.
The 5 characters that compose Hello & character ( '\0' )
The final null specifies the end of the string
In the second case, when using double quotes ( " )
It is automatically appended. 23
Computer programming
Cont..
The following expressions within a code are not valid for arrays.
mystring="Hello";
mystring[] = "Hello";
mystring = { 'H', 'e', 'l', 'l', 'o', '\0' };
Example:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char name [20]=“Melkamu";
cout << name[4];
return 0;
}
Computer programming 24
Cont..
The following expressions within a code are not valid for arrays.
mystring="Hello";
mystring[] = "Hello";
mystring = { 'H', 'e', 'l', 'l', 'o', '\0' };
Example:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char name [20]=“Melkamu";
cout << name[4];
return 0;
}
Computer programming 24
Thank You for Your Attention!!!
Computer Programming 25