Arrays:
An Array is a collection of similar elements under a common variable name. It is also called as subscripted variable . Each individual value in an array is called element. The general syntax of array declaration is
Datatype arrayname[size];
Here the datatype represents the type of data i.e., grouped. The size represents total numberof values that can be grouped under array name. The size should be a positive integer or a symbolic constant but not a negative value or float value or a character.
Initialization of an Array:
The values of an array can be initialized as follows:
int x[5]= {10,20,30,40,50};
If we initialize less number of values the remaining values are stored as garbage values. The values in an
array are stored in continuous memory locations i.e., a sequential memory address
x
10 |
20 |
30 |
40 |
50 |
0 1 2 3 4
1000 1002 1004 1006 1008
In arrays each value is identified by a special identifier known as Index or script. To access the value its index must be enclosed. With array name. The array index starts from Zero and the maximum index is size minus one.
To read the values of an array.
for(i=0;i<5;++i)
cin>>x[i];
To print the values of an array.
for(i=0;i<5;++i)
cout<<x[i];
Example: Program to calculate total marks of the student.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | </p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;">#include <iostream.h></p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;">void main()</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;">{</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>int <span> </span>sno;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>cout<<”Enter the student number<<endl;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span><span> </span><span> </span>cin>>sno;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>char<span> </span>sname[40];</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>cout<<”Enter the student name”<<endl;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>cin>>sname;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>int marks[5],I;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>cout<<”Enter the student marks”<<endl;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>for(i=0;i<5;++i)</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>cin>>marks[i];</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>int total=0;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>for(i=o;i<5;i++)</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>total=total+marks[i];</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>cout<<”S NO=”<<sno<<endl;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>cout<<”S NAME=”<<sname<<endl;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>cout<<”MARKS =”<<endl;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>for(i=0;i<5;++i)</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>cout<<marks[i]<<endl;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>cout<<”TOTAL =”<<total;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>char status =’p’;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>for(i=o;i<5;i++)</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>{</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>if(marks[i]<35)</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>status=’f’;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>}</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>if (status==’p’)</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>cout<<”Student is pass”;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>else</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span>cout<<”Student is fail”;</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span> </span><span> </span>getch();</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;">}</p> <p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"> |
Output:
SNO = 10
SNAME= RAJEEV
MARKS =
100
70
60
40
50
TOTAL = 320
Student is pass
22 total views, 22 views today