0% found this document useful (0 votes)
30K views

Week 07

Arrays allow storing multiple values of the same type in a single variable. An array has a fixed size and contiguous block of memory that stores elements of the same data type. Arrays use indexes to access elements, with indexes starting at 0. Arrays can be initialized, looped through, manipulated by copying elements or changing values at specific indexes. Care must be taken to not access elements outside the bounds of the array, as this causes undefined behavior.

Uploaded by

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

Week 07

Arrays allow storing multiple values of the same type in a single variable. An array has a fixed size and contiguous block of memory that stores elements of the same data type. Arrays use indexes to access elements, with indexes starting at 0. Arrays can be initialized, looped through, manipulated by copying elements or changing values at specific indexes. Care must be taken to not access elements outside the bounds of the array, as this causes undefined behavior.

Uploaded by

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

Arrays

Lecture Link:
https://www.youtube.com/watch?v=C1Y7G8V0JiU&list=PLVEVLI2v6thVDz7U
xUPnURUKaqWFK7Z7v&index=24
Arrays
 Arrays are used to store multiple values in a single variable,
instead of declaring separate variables for each value.
 Array stores a fixed-size sequential collection of elements of
the same type. An array is used to store a collection of
data, They are special kind of data type.
 They are like data structures in which identical data types
are stored.
 In C++ each array has
– name
– data type
– size
 They occupy continuous area of memory.
Storage of an array in memory
Name memory

C[0] 24

C[1] 59
C[2] 35
C[3] ...
C[4] ...
C[5] ...

C[6] ...

C[7] ...
C[8] ...
C[9] ...

Index
Declaration of Arrays
 To declare an array, define the variable type, specify the
name of the array followed by square brackets and specify
the number of elements it should store:
arrayType arrayName [numberOfElements ];
For example ,
int age [ 10 ] ;
 More than one array can be declared on a line
int age [10] , height [10] , names [20] ;

 Mix declaration of variables with declaration of arrays


int i , j , age [10] ;
Cont..
 To create an array of three integers, you could write:
// store only 3 elements in the array
int x[6] = {19, 10, 8};
Empty array elements are automatically assigned value 0.
Referring to Array Elements

Array name [index number]

int age [3]; // array age of size 3


age [ 0]; // array index accessing first value

 Array index will start from 0. means that if you have


declared an array of size 3 then its index will be 0,1
and 2.
Initializing an Array

int age [ 10 ] = { 0,0,0,0,0,0,0,0,0,0 } ;

int age[ 10 ] = { 0 } ;
Initializing an Array

int age [ 10 ] ;

for ( i = 0 ; i < 10 ; i ++ )
{
age [ i ] = 0 ;
}
Initializing an Array
int age [ ] = { 1,2,3,4,5,6,7,8,9,10 } ;

for ( i = 0 ; i < 10 ; i ++ )
‘ i ‘ will have value from 0 to 9
Initializing array by taking input from user

for ( i = 0 ; i < 10 ; i ++ )
{
cin >> age [ i ] ;
}
Accessing array element
#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
return 0;
}

Note: Array indexes start with 0: [0] is the first element. [1]
is the second element,
Loop Through an Array
#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for(int i = 0; i < 4; i++) {
cout << i << ": " << cars[i] << "\n";
}
return 0;
}
Example
#include <iostream>
using namespace std;
main ( )
{
int c [ 100 ] ;
int z , i = 0 ;

do
{
cout<<"Please enter numbers";
cin >> z ;
if ( z != -1 )
c[ i ] = z ;
i ++ ;
} while ( z != -1 && i < 100 ) ;
cout << "The total number of positive integers entered by user is " << i -1;
}
Example
#include <iostream>
using namespace std;
int main() {
int numbers[5];
cout << "Enter 5 numbers: " << endl;

// store input from user to array


for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}
cout << "The numbers are: ";

// print array elements


for (int n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}

return 0;
}
Manipulation of Array
Elements

 Lecture Link:
 https://www.youtube.com/watch?v=zaQfj2bl3j4&list=PLVEVLI2v6thVDz7UxUPnURUK
aqWFK7Z7v&index=25
Copying Arrays
– Data types should be
identical

– Size should be same


int a [ 10 ] ;
int b [ 10 ] ;
Copying Arrays
To copy from array “ a ” to array “ b ” :

b[0]=a[0];
b[1]=a[1];
b[2]=a[2];
b[3]=a[3];
………
………
b [ 10 ] = a [ 10 ] ;
Copying Arrays

for ( i =0 ; i < 10 ; i ++ )
b[i]=a[i];
Example
#include <iostream>
using namespace std;
int main() {
int a [ 10 ] ={3,4,5,6,7,8,9,10,11,12};
int b [ 10 ] ;

for ( int i =0 ; i < 10 ; i ++ )


{
b[i]=a[i];
}
for ( int a =0 ; a < 10 ; a++ )
{
cout<<"Array elements in b array:"<<b [ a ]<< "\n";
}

}
Example
Take the sum of squares of 10 different numbers which are stored in an
array

#include <iostream>
using namespace std;
int main() {

int a [ 5 ] ={1,2,3,4,5};
int arraySize =5 ;
int sumOfSquares = 0 ;
for ( int i = 0 ; i < arraySize ; i ++ )
{
sumOfSquares = sumOfSquares + a [ i ] * a [ i ] ;
}
cout<<"Sum of squares of array elements are: "<<sumOfSquares;

}
Example
#include <iostream>
using namespace std;
int main()
{
int i, n;
float arr[100];
cout << "Enter total number of elements(1 to 100): ";
cin >> n;
cout << endl;
// Store number entered by the user
for(i = 0; i < n; ++i)
{
cout << "Enter Number " << i + 1 << " : ";
cin >> arr[i];
}
// Loop to store largest number to arr[0]
for(i = 1;i < n; ++i)
{
// Change < to > if you want to find the smallest element
if(arr[0] < arr[i])
arr[0] = arr[i];
}
cout << "Largest element = " << arr[0];
return 0;
}
const
const int arraySize = 100 ;

 It creates an identifier “ arraySize ” and


assigns a value 100. This is called
integer constant . It is not a variable
 Its value cannot be changed
Bound Checking
 If we declare an array of size 10, then the array will contain
elements from index 0 to 9. However, if we try to access
the element at index 10 or more than 10, it will result in
Undefined Behaviour.

 Bounds checking is any method of detecting whether a


variable is within some bounds before it is used. It is
usually used to ensure that a number fits into a given type
(range checking), or that a variable being used as an
array index is within the bounds of the array (index
checking).
int main()
{
int i[2] = {0, 1};
return i[2];
}
 Here an array of two elements is defined,
and the third is returned. The behavior of
this program is undefined; it may crash, or
it may return some unknown value.

You might also like