SlideShare ist ein Scribd-Unternehmen logo
1 von 39
Arrays
Prepared by:
Kashif Nawab
St. of IT
UOG Narowal
What is Array?
An array is a group of consecutive memory locations
with same name and type. Simple variable is a single
memory location with unique name and type. But an
array is a collection of different adjacent memory
locations.
 These memory locations in the array are called
elements of array.
 The total number of elements in the array is called its
length.
Advantages of array:
o Arrays can store a large number of values with
single name.
o Arrays are used to process many values
quickly and easily.
o The values stored in an array can be sorted
easily.
o A search process can be applied on arrays
easily.
Declaring One dimensional
Array:
The syntax of declaring one-dimensional array is:
Data_type Identifier[length];
Data_type Indicates data type of values.
Identifier Indicates the name of array.
Length Indicates total number of elements in
array.
For Example:
int marks[5];
Index of each element 0 1 2 3 4
Name of array marks
Array initialization
The syntax of array initialization is :
Data_type Identifier[length]={list of values};
List of values It indicates the values to initialize the array. These
values must b constant.
Example:
int marks[5]={70,75,90,60,85};
Index of each element 0 1 2 3 4
Name of array marks 70 75 90 60 85
Accessing individual elements in an
array:
The syntax for accessing element is as follows :
Array_Name[index];
Array_Name Indicates the name of array.
Index Indicates the index element
to be accessed.
Example:
int marks[5];
int marks[0]=70;
int marks[1]=75;
int marks[2]=90;
int marks[3]=60;
int marks[4]=85;
A simple program:
Write a program that inputs five integers
from user and stores them in an array. It
then displays all values in array without
using loop.
#include<iostream>
Int main( )
{
int array[3];
cout<<“enter five integers: “<<endl;
cin>>array[0];
cin>>array[1];
cin>>array[2];
cin>>array[3];
cin>>array[5];
Cout<<“the values in array are : “<<endl;
cout<<array[0]<<endl;
cout<<array[1]<<endl;
cout<<array[2]<<endl;
cout<<array[3]<<endl;
cout<<array[4]<<endl;
return 0;
}
Write a program that inputs five integers
from user and stores them in an array. It
then displays all values in array using
loop.
#include<iostream>
Int main( )
{
int arr[5],i;
for(i=0 ; i<5 ; i++)
{
cout<<“Enter an integer : “;
cin>>arr[i];
}
cout<<“The values in array are : n
for(i=0 ; i<5 ; i++)
cout<<arr[i]<<endl;
return 0;
}
Output:
Enter an integer : 32
Enter an integer : 46
Enter an integer : 50
Enter an integer : 75
Enter an integer : 84
The values in array are :
32
46
50
75
84
Write a program that input five number
from user in array and display maximum
number.
#include<iostream>
using namespace std;
main()
{
int a[5],i,max;
for(i=0;i<5;i++)
{
cout<<"Enter number : ";
cin>>a[i];
}
max=a[0];
for(i=0;i<5;i++)
if(max<a[i])
max=a[i];
cout<<"Maximum Number is : "<<a[i];
return 0;
}
Output:
Enter number : 8
Enter number : 13
Enter number : 5
Enter number : 15
Enter number : 14
Maximum Number is 15
Two-Dimensional Arrays
Two-dimensional array can be considered as a table that
consists of rows and column. Each element in 2-D array is
referred with the help of two indexes. One index is used to
indicate the row and the second index indicates the column of
the element.
Syntax:
Data_type identifier[Rows][Cols];
For Example:
The following statement declares a 2-D array with four rows and three
columns.
Int Arr[4][3];
A 2-D array and its indexes
Accessing elements of 2-D array
The array name and indexes of row and column are used to access
individual element of a 2-D array. For example, the following statement will
store 20 in the second column of first row.
Int Arr[0][1]=100;
OR
By assigning variables to indexes:
R=0;
C=1;
Arr[R][C]=100;
Entering Data in 2-D array
You can enter data in any element of array using array name and index of
element. For example:
 Nested loops are frequently used to enter data in 2-D arrays.
 The outer loop are used to refer to the rows in array.
 Inner loop are used to refer to the columns in array.
Write a program that stores integer values
in an array of 2 rows and 4 columns.
#include<iostream>
using namespace std;
main()
{
int Array[2][4],i,j;
for(i=0;i<2;i++)
for(j=0;j<4;j++)
{
cout<<"enter values : ";
cin>>Array[i][ j];
}
cout<<"The values in array aren";
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
cout<<Array[i][ j]<<"t";
cout<<endl;
}
return 0; }
Output:
Initialization 2-D Arrays
The process of initialization is performed by assigning the initial values in
braces separated by commas at the time of declaration.
For example:
Int array[3][4]={ {12,5,22,84},
{95,3,41,59},
{77,6,53,62} };
OR
Int array[3][4]={ {12,5,22,84}, {95,3,41,59}, {77,6,53,62} };
Initialization can also be performed without using inner braces.
Int array[3][4]={ 12,5,22,84,
95,3,41,59,
77,6,53,62 }
OR
Int array[3][4]= { 12,5,22,84,95,3,41,59,77,6,53,62 }
Write a program that initializes a 2-D
array of 2 rows and 3 columns and then
display its values.
#include<iostream>
using namespace std;
main()
{
int i,j,a[2][3]={0,1,2,3,4,5};
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
cout<<"arr["<<i<<"]["<<j<<"] "<<a[i][ j]<<"t";
cout<<"n";
}
return 0;
}
Output:
Write a program that initializes a two-dimensional
array of 2 rows and 4 columns and then displays the
maximum and minimum number in the array.
#include<iostream>
using namespace std;
main()
{
int max,min,i,j;
int A[2][4]={15,21,9,84,33,72,18,47};
max=min=A[0][0];
for(i=0;i<2;i++)
for(j=0;j<4;j++)
Program continues
if(A[i][j]>max)
max=A[i][j];
if(A[i][j]<min)
min=A[i][j];
}
cout<<"Maximum number is : "<<max<<endl;
cout<<"Minimum Number is : "<<min<<endl;
return 0;
}
Output:
Maximum Number is 84
Minimum Number is 9

Weitere ähnliche Inhalte

Was ist angesagt?

Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)
Fatima Kate Tanay
 

Was ist angesagt? (20)

Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Array
ArrayArray
Array
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Circular queue
Circular queueCircular queue
Circular queue
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
Python strings
Python stringsPython strings
Python strings
 
Interface in java
Interface in javaInterface in java
Interface in java
 

Ähnlich wie Arrays in C++

arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
JayanthiM19
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
worldchannel
 

Ähnlich wie Arrays in C++ (20)

Array assignment
Array assignmentArray assignment
Array assignment
 
Array BPK 2
Array BPK 2Array BPK 2
Array BPK 2
 
Arrays
ArraysArrays
Arrays
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Arrays
ArraysArrays
Arrays
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
02 arrays
02 arrays02 arrays
02 arrays
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
Arrays
ArraysArrays
Arrays
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Arrays
ArraysArrays
Arrays
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 

Kürzlich hochgeladen

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Kürzlich hochgeladen (20)

Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 

Arrays in C++