Quantcast
Channel: Java Question Bank » Cpp Programs
Viewing all articles
Browse latest Browse all 22

Arrays in C++

$
0
0

 

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 &lt;iostream.h&gt;</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&lt;&lt;”Enter the student number&lt;&lt;endl;</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span>              </span><span>  </span><span>                </span><span> </span>cin&gt;&gt;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&lt;&lt;”Enter the student name”&lt;&lt;endl;</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span>                </span><span>                </span>cin&gt;&gt;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&lt;&lt;”Enter the student marks”&lt;&lt;endl;</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span>                </span><span>                </span>for(i=0;i&lt;5;++i)</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span>                </span><span>                                </span>cin&gt;&gt;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&lt;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&lt;&lt;”S NO=&lt;&lt;sno&lt;&lt;endl;</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span>                </span><span>                </span>cout&lt;&lt;”S NAME=&lt;&lt;sname&lt;&lt;endl;</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span>                </span><span>                </span>cout&lt;&lt;”MARKS =&lt;&lt;endl;</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span>                </span><span>                </span>for(i=0;i&lt;5;++i)</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span>                </span><span>                                </span>cout&lt;&lt;marks[i]&lt;&lt;endl;</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: 150%;"><span>                </span><span>                </span>cout&lt;&lt;”TOTAL =&lt;&lt;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&lt;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]&lt;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&lt;&lt;”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&lt;&lt;”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


Viewing all articles
Browse latest Browse all 22

Trending Articles