Lecture 11
Lecture 11
1
Cont.
Cout<<“x= ”<<x;
2
Cont.
• For example, suppose we want to arrange the percentage marks
obtained by 100 students in ascending order
• In such a case we have two options to store these marks in memory:
• Declare 100 variables to store percentage marks obtained by 100
different students, i.e. each variable containing marks of single student
int m1, m2, m3 ……… m100;
• Declare one variable (called array or subscripted variable) capable of
storing or holding all the hundred values
3
Array declaration
int marks[10];
0 1 2 3 4 5 6 7 8 9
500 504 508 512 516 520 524 528 532 530
Memory address of array elements 4
How to access array elements
int x; int marks[10];
x= 2; marks[0] = 2;
Cout<<x; marks[1] = 3;
Cout<<x; Cout<<marks[2])
Cout<<“marks [2] = ”<< marks[2])
x 43
2 Output
695 x = 43 Output
marks [2] = 16
0 1 2 3 4 5 6 7 8 9
2 3 16
500 504 508 512 516 520 524 528 532 530
5
Points to remember
• Array is a collection of elements having same data type
• Memory allocate to array elements are continuous
• Array size must be mentioned in array declaration ( int marks
[10]; )
• Array index always starts with 0
• In 10 elements array, index of first elements is 0 and index of
last element is 9
• Array element can be access using array index
6
A Simple Program Using Array
• Write a program that take 10 integer from user and then display
those integers
Write a program
7
Array initialization