Friday, 26 August 2016

Single Dimensional Array


It is a simple form of array. It consists of type, name and its elements(subscripts or indices).

Syntax: type array-name[size];
It's type is also called the base type. It starts with 0 and goes till 'size-1' . Let's understand this with an example.


Program:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, marks[3];
for(i=0; i<3; i++)                /* i is taken zero because our array starts with zero
                                             so it will make us easy to understand it's
                                            elements position
                                          */
{
 cout<<"Enter your Marks in paper "<<i+1<<": ";
 cin>>marks[i];
}
cout<<"\nHere are the Marks of Student\n";
for(i=0; i<3; i++)
{
 cout<<"Paper "<<i+1<< "= "<<marks[i]<<"\n";
}
getch();
}


Output:

Enter your Marks in paper 1: 20
Enter your Marks in paper 2: 17
Enter your Marks in paper 3: 18

Here are the Marks of Student
Paper 1: 20
Paper 2: 17
Paper 3: 18

The above array takes the Marks from the Student. The first values goes at position 0, second stores at position 1 and last at position 2. Then in the next Loop values stored in the Array marks is displayed.

No comments:

Post a Comment