Nothing Special   »   [go: up one dir, main page]

One Dimensional Array

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Array

An array allows you to store a group of items of the


same data type together in memory.

The contents of an array are stored in contiguous


memory. It can be individually referenced by adding an
index to a unique identifier.

It is special type of variable that can contain or hold


one or more values of the same data type with
reference to only one variable name. The data items in
an array are referred to as elements.

IT105 Computer Programming 2


Array Elements and Subscripts

A Normal Variable

An Array Variable

[0] [1] [2] [3] [4]

// this is an array variable of five elements

IT105 Computer Programming 2


Array Elements and Subscripts

An array can be distinguished through a pair of


square brackets and a specified number inside the
square brackets [ ].

The number inside the square brackets is called an


index or element.

Each element is assigned a unique number known as


subscripts.
Subscripts are used to identify specific elements in an
array.

IT105 Computer Programming 2


Array Elements and Subscripts

An Array Variable

x [0] [1] [2] [3] [4]


// this is an array variable of five elements

Each element can be referred to as :

x[0] x[1] x[2] x[3] x[4]

IT105 Computer Programming 2


Declaration and Definition of Array

data type arrayname [index];

int num [5];

char name [10];


Note: In a fixed-length array, the size of the array is
constant and must have a value at compilation time.

IT105 Computer Programming 2


Initialization

An array can be initialized when it is declared, when


initializing the array, the value for their various indexed
variables are enclosed in braces and separated by commas.

int num[5] = { 10,20,30,40,50 };


0 1 2 3 4

10 20 30 40 50

char choice[5] = { ‘a’,’b’,’c’,’d’,’e’};

IT105 Computer Programming 2


Initialization

num[0] = 10 ; choice[0] = ‘a’ ;


num[1] = 20 ; choice[1] = ‘b’ ;
num[2] = 30 ; choice[2] = ‘c’ ;
num[3] = 40 ; choice[3] = ‘d’ ;
num[4] = 50 ; choice[4] = ‘e’ ;

Initialization without size


int num[] = { 10,20,30,40,50 };
Partial initialization
int num[5] = { 10,20};
(The rest are filled with 0’s)
IT105 Computer Programming 2
Sample C++ Statements using Array

single variable array

num = 20 ; num[0] = 20 ;
num[x] = 20 ;

a = a + 1 ; a[1] = a[1] + 1 ;

c = a + b ; c = a[1] + b[2] ;

c[0]= a[1]+b[2] ;
IT105 Computer Programming 2
Sample C ++ Statements using Array

single variable array

cin >> x; cin >> x[0];

cout << x; cout << x[1];

if (grade ›=75) if (grade[3] ›=75)


{ {
cout<<“passed”; cout<<“passed”;
} }

IT105 Computer Programming 2


Sample Programs
// reading 5 values into an array variable
for ( i = 0 ; i<=4; i++)
{
cin>>num[i];
}

// displaying stored values of an array variable


for ( i = 0 ; i<=4; i++)
{
cout<< num[i];

}
IT105 Computer Programming 2
Sample Programs

for ( i=4 ; i›=0; i--)


{
cout<< num[i];
}

for ( i=0 ; i<=4; i+=2)


{
cout<< num[i]
}

ACT 123 Advanced Programming


Sample Programs
// adding 5 to each stored value of an array variable

for ( i = 0 ; i<=4; i ++)


{
cout<< num[i] + 5;

for ( i = 0 ; i<=4; i ++)


{
num[i]= num[i] + 5;
cout<< num[i];
}

ACT 123 Advanced Programming


Sample Programs
// copying the contents of an array a to array b

for ( i = 0 ; i<=4; i ++)


{
b[i] = a[i];
}
// copying the contents of an array a to array b in reversed order
y=4;
for ( i = 0 ; i<=4; i++)
{
b[y]= a[i];
y--;
}
ACT 123 Advanced Programming
Sample Programs
// copying the contents of an array a to array b in reversed order
y=4;
for ( i = 0 ; i<=4; i ++)
{
b[i]= a[y];
y--;
}
y=0;
for ( i = 4 ; i›=0; i--)
{
b[i]= a[y];
y++;
}
ACT 123 Advanced Programming
Sample Programs
// copying the contents of an array a to array b in reversed order
y=4;
for ( i = 0 ; i<=4; i ++)
{
b[y-i]= a[i];
}

for ( i = 0 ; i<=4; i ++)


{
b[4-i]= a[i];
}

ACT 123 Advanced Programming


Sample Programs
// calculating the average of 10 numbers
cout<<"\nEnter the 10 numbers:\n";
for(int i = 0; i < 10; i ++)
{
cin>>number[i];
sum = sum + number[i];
}
average = sum/10;
cout<<"\nAverage of the ten numbers
entered is: “<< average;

ACT 123 Advanced Programming


Sample Programs
Computing the area of 3 rectangles with
#include <iostream>
normal variables
using namespace std;
int a,l,w,x;

int main()
{

for( x=0;x<=2;x++)
{
cout<< "Rectangle "<< x+1 <<endl;
cout<< "Length : " ;
cin>>l;
cout<< "Width : ";
cin>> w;
a= l * w;
cout<< " Area : " << a;
cout<<"\n\n\n";
}
}
ACT 123 Advanced Programming
Sample Programs
#include <iostream>
Computing the area of 3 rectangles with 3
using namespace std;
array variables (for area, length, width)
int a[3], l[3],w[3],x;

int main()
{
for( x=0;x<=2;x++)
{
cout<< "Rectangle "<< x+1 <<endl;
cout<< "Length : " ;
cin>> l[x];
cout<< "Width : ";
cin>> w[x];
a[x]= l [x]* w[x];
cout<< " Area : " << a[x];
cout<<"\n\n\n";
}
cout<<a[0];

return 0;
}
ACT 123 Advanced Programming
Sample Programs
#include <iostream>
Computing the area of 3 rectangles with 1
using namespace std;
array variable (for area, length, width)
int a[9],x;

int main()
{
for( x=0;x<=2;x++)
{
cout<< "Rectangle "<< x+1 <<endl;
cout<< "Length : " ;
cin>> a[x];
cout<< "Width : ";
cin>> a[x+3];
a[x+6]= a[x]* a[x+3];
cout<< " Area : " << a[x+6];
cout<<"\n\n\n";
}

cout<<a[6];

return 0;
} ACT 123 Advanced Programming

You might also like