0% found this document useful (0 votes)
43 views

Lesson 1 Introduction To Array

This document provides an introduction to arrays in C++. It defines an array as a collection of items of the same data type stored contiguously in memory that can be referenced using a single variable name and integer indexes. The document explains that arrays allow grouping of multiple values under one name, and that each element in an array has a unique index/subscript that is used to access it. It also covers declaring, initializing, and using arrays through examples of C++ code statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Lesson 1 Introduction To Array

This document provides an introduction to arrays in C++. It defines an array as a collection of items of the same data type stored contiguously in memory that can be referenced using a single variable name and integer indexes. The document explains that arrays allow grouping of multiple values under one name, and that each element in an array has a unique index/subscript that is used to access it. It also covers declaring, initializing, and using arrays through examples of C++ code statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT 2

Array

IT 105 Computer Programming 2


Lesson 1
Introduction to Array

IT 105 Computer Programming 2


LESSON 1 Introduction to Array
What is an 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 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.

IT 105 Computer Programming 2


LESSON 1 Introduction to Array
Array Elements and Subscripts
The data items in an array are referred to as elements.

Each element is assigned a unique number known as subscript or index.

It can be individually referenced by specifying the index inside the square


brackets [ ].

IT 105 Computer Programming 2


LESSON 1 Introduction to Array
Array Elements and Subscripts

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

// num is an array variable of five elements

Each element can be referred to as :

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

IT 105 Computer Programming 2


LESSON 1 Introduction to Array
Declaration of Array

data type arrayname [elements];

int num [5];

Note: In a fixed-length array, the size of the array is


constant and must have a value at compilation time.

IT 105 Computer Programming 2


LESSON 1 Introduction to Array
Initialization of Array
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’};

IT 105 Computer Programming 2


LESSON 1 Introduction to Array
Initialization of Array
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)

IT 105 Computer Programming 2


LESSON 1 Introduction to Array
Sample C++ Statements using Array
single variable array
num = 20 ; num[0] = 20 ;
num[x] = 20 ;
num[x-1]= 20 ;
a = a + 1 ; a[1] = a[1] + 1 ;

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

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

IT 105 Computer Programming 2


LESSON 1 Introduction to Array
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”;
} }

IT 105 Computer Programming 2

You might also like