Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

* Programming

Fundamentals

Lecture 07

Instructor: Rao Umer Farooq


*Programming Fundamentals
Arrays
• An array is a consecutive group of memory locations that all
have the same type.
• To refer to a particular location or element in the array, we
specify the name of the array and the position number of the
particular element in the array.
Or
• An array is a collection of data elements that are of the same
type (e.g., a collection of integers, collection of characters,
collection of doubles).
*Programming Fundamentals
Arrays
*In programming, one of the frequently arising problem is to handle
numerous data of same type.
*Consider this situation, you are taking a survey of 100 people and
you have to store their age.
* To solve this problem in C++, you can create an integer array
having 100 elements.
*An array is a collection of data that holds fixed number of values of
same type.
 For example: int age[100];
*Here, the age array can hold maximum of 100 elements of integer
type.
*Programming Fundamentals
Array Declaration
*Syntax:
<type> <arrayName>[<array_size>]
Example: int Ar[10];

*The array elements are all values of the type <type>.


*The size of the array is indicated by <array_size>, the number of
elements in the array.
*<array_size> must be an int constant or a constant expression.
Note that an array can have multiple dimensions.
*Programming Fundamentals
Array Declaration
// array of 10 uninitialized ints
int Ar[10];

0 1 2 3 4 5 6 7 8 9
Ar -- -- -- -- -- -- -- -- -- --

0 1 2 3 4 5
*Programming Fundamentals
Array
How to Refer Array Elements
*We can reference array elements by using the array’s subscript. The
first element has a subscript of 0. The last element in an array of
length n has a subscript of n-1.
*When we write a program, we refer to an individual array element
using indexing.
*To index a subscript, use the array name and the subscript in a pair
of square brackets:
a[12];
Declaration Examples
Subscripting
*Declare an array of 10 integers:
int Ar[10]; // array of 10 ints
*To access an individual element we must apply a subscript to
array named Ar.
* A subscript is a bracketed expression.
* The expression in the brackets is known as the index.
* First element of array has index 0.
Ar[0]
* Second element of array has index 1, and so on.
Ar[1], Ar[2], Ar[3],…
* Last element has an index one less than the size of the array.
Ar[9]
*Incorrect indexing is a common error.
Subscripting

// array of 10 uninitialized ints


int Ar[10]; --

Ar[3] = 1; 1 --
int x = Ar[3];
-- -- --

0 1 2 3 4 5 6 7 8 9
Ar -- -- -- 1 -- -- -- -- -- --
Ar[0]Ar[1]
Ar[2]Ar[3]
Ar[4]
Ar[5]
Ar[6]Ar[7]Ar[8]Ar[9]
*Programming Fundamentals
Array
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:
string cars[4];
To insert values to it:

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


*Programming Fundamentals
Array
Array Creation:
To create an array of three integers:
int Num[3] = {10, 20, 30};
Access the Elements of an Array
*Access an array element by referring to the index number inside
square brackets [].
*This statement accesses the value of the first element in cars:
Example: string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
// Outputs Volvo
*Programming Fundamentals
Array
*Change an Array Element
*To change the value of a specific element, refer to the index
number:
*cars[0] = "Opel";
*Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
// Now outputs Opel instead of Volvo
*Accessing Array Elements
*Individual Elements
*Name of array
*Syntax: to access an individual member of an array:
*Array_name[index]
*Example:
* Int marks[5]
* marks[0] = 20;
* marks[1] = 10;
* ….
* …..
* marks[4] = 40;
*Accessing Array Elements
*Individual Elements
*Using loop
*An easier and faster way of accessing an array.
*Example: it shows how array elements can be accessed
using for loop.
*int marks[5];
*for (int i=0; i<5; i++)
*marks[i]= I;
*Programming Fundamentals
Array
Using For Loop:
string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
for (int i = 0; i < 5; i++) {
cout << cars[i] << "\n";
}
*Programming Fundamentals
Array
Using For Loop:
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
cout << myNumbers[i] << "\n";
}
Array Initialization Examples
*Programming Fundamentals
Array
*The foreach Loop
*There is also a "for-each loop" (introduced in C++ version 11
(2011), which is used exclusively to loop through elements in an
array:
*Syntax
*for (type variableName : arrayName) {
// code block to be executed
}
*Programming Fundamentals
Array
*The foreach Loop
*int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << "\n";
}
*Programming Fundamentals
Array
To change the value of an array:

int mark[5] = {19, 10, 8, 17, 9}


// change 4th element to 9
mark[3] = 9;
*Programming Fundamentals
2 Dimensional Array - (2D Arrays)
* 2-D arrays can be defined as an array of arrays.
* It can also represent a Matrix.
* Each element is represented as :
* Arr [row][column] , where Arr[][] is a 2-D array.
*Programming Fundamentals
2 Dimensional Array - (2D Arrays)
* Initializing a 2-D array:
* int arr[4][2] = {
* {1234, 56},
* {1212, 33},
* {1434, 80},
* {1312, 78}
* };
* we initialize a 2D array arr, with 4 rows and 2 columns as an array
of arrays.
* Each element of the array is yet again an array of integers.
*Programming Fundamentals
2 Dimensional Array - (2D Arrays)
* Initializing a 2-D array:

* We can also initialize a 2D array in the following way.

* int arr[4][2] = {1234, 56, 1212, 33, 1434, 80, 1312, 78};
*Programming Fundamentals
2 Dimensional Array - (2D * { 40, 41 }
Arrays) * };
* Printing a 2-D array: * int i,j;
* #include<iostream> cout<<"Printing a 2D Array:\
n";
* using namespace std;
* for(i=0;i<4;i++)
* main( )
* {
* {
* for(j=0;j<2;j++)
* int arr[4][2] = {
* { cout<<"\
* { 10, 11 }, t"<<arr[i][j]; }
* { 20, 21 }, cout<<endl; }
* { 30, 31 }, * }
*Programming Fundamentals
Structures
* Structure is a collection of variables of different data types under
a single name.

* For example you want to store some information about a Person


* Name
* Age
* Salary
*Programming Fundamentals
Structures - How to declare a structure
*The struct keyword defines a structure type followed by an
identifier (name of the structure).
*Then inside the curly braces, you can declare one or more members
(declare variables inside curly braces) of that structure.
* For example:
* struct Person structure person is defined
which has three members:
*{
name, age and salary.
* char name[50];
* int age;
* float salary; };
*Programming Fundamentals
Structures - How to declare a structure
When a structure is created, no memory is allocated.

*How to define a structure variable?


*Once you declare a structure person as above.
*You can define a structure variable as:
*Person p1; struct Person
*Here, a structure variable p1 is defined which is of type structure
Person.
*When structure variable is defined, only then the required memory
is allocated by the compiler.
*Programming Fundamentals
*Structures - How to define a structure variable?
*Considering you have either 32-bit or 64-bit system, the memory of
float is 4 bytes, memory of int is 4 bytes and memory of char is 1
byte.
*Hence, 58 bytes of memory is allocated for structure variable p1.
*How to access members of a structure?
*The members of structure variable is accessed using a dot (.)
operator.
*Suppose, you want to access age of structure variable p1 and assign
it 50 to it. You can perform this task by using following code
below:
p1.age = 50;
*Programming Fundamentals
*Structures - How to define a structure variable?
*Programming Fundamentals
*Structures - How to define a structure variable?
* Output:

*Here a structure Person is declared which has three members


name, age and salary.
*Inside main() function, a structure variable p1 is defined. Then, the
user is asked to enter information and data entered by user is
displayed.

You might also like