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

CH - 7C++ Arrays

gvxvcvcxvc

Uploaded by

Mohammed Ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

CH - 7C++ Arrays

gvxvcvcxvc

Uploaded by

Mohammed Ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

C++

LANGUAGE
C++ Arrays
OBJECTIVES
 Introduction of an array
 Array definitions
 Accessing array elements
 Usage off Array
 Types of Array
 Examples of Array
 Summary
INTRODUCTION OF AN
ARRAY
An array is a collection of similar elements. These similar
elements could be all ints, or all floats, or all chars, etc.

Usually, the array of characters is called a ‘string’, whereas an


array of ints or floats is called simply an array.

Remember that all elements of any given array must be of the


same type. i.e. we cannot have an array of 10 numbers, of
which 5 are ints and 5 are floats.
INTRODUCTION OF AN
ARRAY
For example, suppose we wish to arrange the percentage marks
obtained by 100 students in ascending order. In such a case we have
two options to store these marks in memory:

a) Construct 100 variables to store percentage marks obtained by 100


different students, i.e. each variable containing one student’s marks.

b) Construct one variable (called array or subscripted variable)


capable of storing or holding all the hundred values.
ARRAY DEFINITIONS
 C++ provides a data structure, the array, which stores a fixed-size

sequential collection of elements of the same type.

 An array is used to store a collection of data, but it is often more useful

to think of an array as a collection of variables of the same type.

 Instead of declaring individual variables, such as number0, number1, ...,

and number99, you declare one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to represent individual
variables.
ACCESSING ARRAY
ELEMENTS
 A specific element in an array is accessed by an index.

 All arrays consist of contiguous memory locations.

 The lowest address(index) corresponds to the first element

and the highest address(index) to the last element.


DECLARING ARRAYS
To declare an array in C++, the programmer specifies the type
of the elements and the number of elements required by an
array as follows −
type arrayName [ arraySize ];

This is called a single-dimension array. (One dimentional array)


The arraySize must be an integer constant greater than zero
and type can be any valid C++ data type.
CONT..
For example, to declare a 10-element array called balance of
type double, use this statement −
double balance[10];
INITIALIZING ARRAYS

You can initialize C++ array elements either one by one or


using a single statement as follows −

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

The number of values between braces { } can not be larger


than the number of elements that we declare for the array
between square brackets [ ].
INITIALIZING ARRAYS

Following is an example to assign a single element of the array −

balance[5]=50.0;

The above statement assigns element number 5th in the array a


value of 50.0. Array with 4th index will be 5th, i.e., last element
because all arrays have 0 as the index of their first element
which is also called base index.
ARRAY SIZE
If you omit the size of the array, an array just big enough to
hold the initialization is created.
Therefore, if you write −
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
You will create exactly the same array as you did in the previous
example.
EXAMPLE #ONE
 #include <iostream>

 using namespace std;

 int main() {

 int numbers[5]={16,26,36,46,56};

 cout<<numbers[2];

 return 0;

}
USAGE OF ARRAY
 Arrays are used to process a large amount of data of same data

type. The data is stored in an array.

 The index values are used to access individual elements of the

array. A few statements are required to process data in an array.

 There fore, the use of arrays in a program reduces size of the

program
C++ ARRAYS AND LOOPS
 You can loop through the array elements with the for loop.

 The following example outputs the index of each element

together with its value:

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};

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

cout << i << ": " << cars[i] << "\n";

}
TYPES OF ARRAY
 One Dimensional Arrays

A[0] A[1] A[2] A[3] A[4] A[5]


 Two Dimensional Arrays

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


A[1][0] A[1][1] A[1][2] A[1][3] A[1][4]
 Multi Dimensional Arrays
 3D Array, 4D Arrays etc.
EXAMPLE OF ONE
DIMENSIONAL ARRAY
#include<iostream.h>
main()
{
float a[5];
int i;
a[0]=9.9;
a[1]=12.9;
a[2]=13.1;
a[3]=8.9;
a[4]=10.6;
for(i=0; i<=4; i++)
cout<<"Value in a ["<<i<<"]="<<a[i]<<endl;
}
ANOTHER EXAMPLE
#include <iostream.h>
int main()
{
double temp[5]={66.2,63.2,69.6,70.2,55.4};
int j;
//OutPut of The Screen
for(j=0; j<=4; j++)
cout <<temp[j]<<endl;
return 0;
}
TWO- DIMENSIONAL
ARRAY
 A multi-dimensional array is an array of more than one array.

 For Example, an array of two one-dimensional is called two-

dimensional array.

It also known as table or matrix.

 A two-dimensional array consists of columns and rows.

 Each elements of the two-dimensional array is referenced by its

index values or subscripts.


CONT..
 A two-dimensional array in C++ is the simplest form of a multi-
dimensional array. It can be visualized as an array of arrays. The image
below depicts a two-dimensional array.
DECLARATION OF TWO-
DIMENSIONAL ARRAY
 A two-dimensional array is declared by giving two indexed

values enclosed in square brackets.

 The first indexed value represents the total number of rows and

the second represents the total number of columns.

 Syntax:

Type array_name [r][c];


2D ARRAY EXAMPLE
#include <iostream.h>
int main()
{
double nums[2][3]={{66.2,63.2,69.6},{70.2,55.4,44.5}};
//OutPut of The Screen
cout <<nums[0][2]<<endl;
return 0;
}
ONE DIMENSIONAL
ARRAY VS
TWO DIMENSIONAL
ARRAY
 A one-dimensional array is not better than a two-dimensional array, they

just serve different purposes.


A one dimensional array is only one row, but multiple columns...
XXXXXXXXXXXXXXXXXXXX

 A two-dimensional array is multiple rows, multiple columns.

XXXXXXXXX
XXXXXXXXX
XXXXXXXXX
XXXXXXXXX
SUMMARY
 In C++, an array is a variable that can store multiple values of

the same type.

 A specific element in an array is accessed by an index.

 Remember that all elements of any given array must be of the

same type. i.e. we cannot have an array of 10 numbers, of


which 5 are ints and 5 are floats.
SUMMARY
 Arrays are used to process a large amount of data of same

data type. The data is stored in an array.

 There fore, the use of arrays in a program reduces size of the

program

 Variable holds single value of any type. i.e. int, float and char.

 Array variable holds multiple values of the same type.


THE END
ANY
QUESTIONS
?

You might also like