PRESENTATION B
PRESENTATION B
َٰم
َّر ِن َّر ِم ِبْس ِم
in the name of God, Most Gracious, Most Merciful
UNIVERSITY OF
CENTRAL PUNJAB
NAME:-
Noor Hassan(21)
Ahmed Butt(5)
CLASS:-
BSCS SEM 1
Topic:
Arrays
Outline:-
Arrays
Advantages of Arrays
One dimensional Array
Array Initialization
Two dimensional Array
What are Arrays?
Anarray is a data structure that stores
a collection of elements of the same
data type (integers , floats , strings).
The size of an Array is declares at the
starting of the program.
Eachelement can be accessed directly
using its index (e.g., arr[0] for the first
element).
Thearrays of 5 elements can be
declared as:
Advantages of Arrays:-
Arrays can store a large number of values
with a single name.
Arrays are used to process many values
easily and quickly.
The values stored in an array can be
sorted easily.
A search process can be applied on arrays
easily.
One dimensional Array
Declaration:-
All elements are arranged in the form
of a list which is known as one
dimensional array.
It consists of one column or a row.
The specifying of specifying array
name , length and data type is called
Array declaration.
Syntax:-
Data_Type Identifier[Length];
Data type: It indicates the data
type of the values stored in the
array.
Identifier: It indicates the name
of the array.
Length: It indicates total
Program:-
This program takes input from user and displays it in an
array.
#include<iostream>
using namespace std;
int main()
{
int arr[4];
cout<<"Enter five integers
respectively"<<endl;
cin>>arr[0];
cin>>arr[1];
Program:
cin>>arr[3];
cout<<"The values in the array are as
follows:\n";
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;
cout<<arr[2]<<endl;
cout<<arr[3]<<endl;
}
Array Initialization:-
The process of assigning
values to array elements at the
time of declaration is known as
array initialization.
The values are separated with
commas.
The values in the array are
enclosed in braces.
Syntax:-
Data_Type Identifier[Length]={list
of values}
Data type: It indicates the data
type of the values stored in the
array.
Identifier: It indicates the name of
the array.
Length: It indicates total number
of elements in the array. It must
Program:-
#include <iostream>
using namespace std;
int main() {
// Declare and initialize an array with 5
elements
int arr[5] = {10, 20, 30, 40, 50};
return 0;
}
Two dimensional Array:-
2D Arrays can be defined as
an arrays of array.
It can also represent a
Matrix.
Each element is
represented as Arr[row]
[column], where Arr[][] is
Program:-
#include <iostream>
using namespace std;
int main() {
// Declare a 2D array
int matrix[3][3];
// Input elements into the array
cout << "Enter 9 elements for a 3x3
matrix:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> matrix[i][j];
}
cin >> matrix[i][j];
}
}
// Display the array
cout << "\nThe 3x3 matrix is:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
ANY
QUESTIONS?