SlideShare a Scribd company logo
ARRAYS Lecture 2.8
Prepared by: Mian Saeed Akbar
Arrays
Array
 A collection of same datatype data, having a common name, a
unique index and are store in consecutive memory locations
To refer to an element
 Specify array name and position number (index)
 Format: arrayname[ position number ]
 First element at position 0
N-element array c
c[ 0 ], c[ 1 ] … c[ n - 1 ]
 Nth element as position N-1
Arrays
Array elements are treated like other variables
 Assignment, printing for an integer array c
c[ 0 ] = 3;
cout << c[ 0 ];
Can perform operations inside subscript
c[ 5 – 2 ] same as c[3]
Declaring Arrays
When declaring arrays, specify
 Name
 Type of array
 Any data type
 Number of elements
 type arrayName[ arraySize ];
Declaring multiple arrays of same type
 Use comma separated list, like regular variables
int c[ 10 ]; // array of 10 integers
float d[ 3284 ]; // array of 3284 floats
int b[ 100 ], x[ 27 ];
Initializing arrays
 For loop
 Set each element
 Initializer list
 Specify each element when array declared
 If not enough initializers, rightmost elements 0
 If too many syntax error
 To set every element to same value
 If array size omitted, initializers determine size
 5 initializers, therefore 5 element array
int n[ 5 ] = { 1, 2, 3, 4, 5 };
int n[ 5 ] = { 0 };
int n[] = { 1, 2, 3, 4, 5 };
Passing Arrays to Functions
Specify name without brackets
 To pass array myArray to myFunction
 Array size usually passed, but not required
 Useful to iterate over all elements
Arrays passed-by-reference
 Functions can modify original array data
 Value of name of array is address of first element
 Function knows where the array is stored
 Can change original memory locations
Individual array elements passed-by-value
 Like regular variables
int myArray[ 24 ];
myFunction( myArray, 24 );
square( myArray[3] );
Passing Arrays to Functions
Functions taking arrays
 Function prototype
 Names optional in prototype
 Both take an integer array and a single integer
 No need for array size between brackets
 Ignored by compiler
 If declare array parameter as const
 Cannot be modified (compiler error)
void modifyArray( int b[], int arraySize );
void modifyArray( int [], int );
void doNotModify( const int [] );
Thank you
Please subscribe my channel for more videos
and courses

More Related Content

PPT
7.basic array
PPTX
ch 7 single dimension array in oop .pptx
PPTX
Module 7 : Arrays
PPTX
Arrays in Data Structure and Algorithm
PPTX
PDF
Chapter 4 (Part I) - Array and Strings.pdf
PPTX
arrays in c# including Classes handling arrays
PPT
7.basic array
ch 7 single dimension array in oop .pptx
Module 7 : Arrays
Arrays in Data Structure and Algorithm
Chapter 4 (Part I) - Array and Strings.pdf
arrays in c# including Classes handling arrays

Similar to Lecture 2.8 Arrays.pdf (20)

PPTX
2D Array
PDF
Arrays
PPT
Mesics lecture 8 arrays in 'c'
PPTX
Intro to C# - part 2.pptx emerging technology
PPT
ARRAYS.ppt
PPTX
Arrays in programming
PPT
2 arrays
PPT
Arrays Basicfundamentaldatastructure.ppt
PPTX
Computer programming 2 Lesson 13
PPT
Algo>Arrays
PDF
Introduction to Arrays in C
PPT
ARRAYS.ppt
PPT
Array and 2D Array and with syntax working
PPT
Arrays are used to store multiple values in a single variable, instead of dec...
PPT
ARRAYS.ppt
PPT
ARRAYS.ppt
PPTX
object oriented programing in python and pip
2D Array
Arrays
Mesics lecture 8 arrays in 'c'
Intro to C# - part 2.pptx emerging technology
ARRAYS.ppt
Arrays in programming
2 arrays
Arrays Basicfundamentaldatastructure.ppt
Computer programming 2 Lesson 13
Algo>Arrays
Introduction to Arrays in C
ARRAYS.ppt
Array and 2D Array and with syntax working
Arrays are used to store multiple values in a single variable, instead of dec...
ARRAYS.ppt
ARRAYS.ppt
object oriented programing in python and pip

More from MianSaeedAkbar1 (20)

PDF
Lecture 1.3 A Simple Program to Print a Line of Text.pdf
PDF
Lecture 2.2 Default Arguments.pdf
PDF
Lecture 2.6 Function Templates.pdf
PDF
Lecture 2.4 Recursion.pdf
PDF
Lecture 3 Structures in C++.pdf
PDF
Lecture 2.5 Function Overloading.pdf
PDF
Lecture 1.11 Break and Continue Statements.pdf
PDF
Lecture 2.1 Functions.pdf
PDF
Lecture 1.5 Variables.pdf
PDF
Lecture 1.2 Basics of a typical C++ Environment.pdf
PDF
Lecture 2.3 Inline Function.pdf
PDF
Lecture 1.8 Conditional Structures.pdf
PDF
Lecture 2.7 Type Conversion in C++.pdf
PDF
Lecture 1.6 Arithmetic and Relational Operators.pdf
PDF
Lecture 1.7 C++ Keywords.pdf
PDF
Lecture 1.4 Escape Sequence.pdf
PDF
Lecture 1.9 Switch Structure.pdf
PDF
Lecture 1.10 Repetition Structures.pdf
PDF
Lecture 0 Object Oriented Programming.pdf
PDF
Lecture 1.1 Introduction to Computer Languages.pdf
Lecture 1.3 A Simple Program to Print a Line of Text.pdf
Lecture 2.2 Default Arguments.pdf
Lecture 2.6 Function Templates.pdf
Lecture 2.4 Recursion.pdf
Lecture 3 Structures in C++.pdf
Lecture 2.5 Function Overloading.pdf
Lecture 1.11 Break and Continue Statements.pdf
Lecture 2.1 Functions.pdf
Lecture 1.5 Variables.pdf
Lecture 1.2 Basics of a typical C++ Environment.pdf
Lecture 2.3 Inline Function.pdf
Lecture 1.8 Conditional Structures.pdf
Lecture 2.7 Type Conversion in C++.pdf
Lecture 1.6 Arithmetic and Relational Operators.pdf
Lecture 1.7 C++ Keywords.pdf
Lecture 1.4 Escape Sequence.pdf
Lecture 1.9 Switch Structure.pdf
Lecture 1.10 Repetition Structures.pdf
Lecture 0 Object Oriented Programming.pdf
Lecture 1.1 Introduction to Computer Languages.pdf

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
master seminar digital applications in india
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Lesson notes of climatology university.
PDF
Computing-Curriculum for Schools in Ghana
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Cell Types and Its function , kingdom of life
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
RMMM.pdf make it easy to upload and study
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
master seminar digital applications in india
01-Introduction-to-Information-Management.pdf
Lesson notes of climatology university.
Computing-Curriculum for Schools in Ghana
GDM (1) (1).pptx small presentation for students
Cell Types and Its function , kingdom of life
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
RMMM.pdf make it easy to upload and study
TR - Agricultural Crops Production NC III.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Microbial disease of the cardiovascular and lymphatic systems
human mycosis Human fungal infections are called human mycosis..pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Basic Mud Logging Guide for educational purpose
Final Presentation General Medicine 03-08-2024.pptx

Lecture 2.8 Arrays.pdf

  • 1. ARRAYS Lecture 2.8 Prepared by: Mian Saeed Akbar
  • 2. Arrays Array  A collection of same datatype data, having a common name, a unique index and are store in consecutive memory locations To refer to an element  Specify array name and position number (index)  Format: arrayname[ position number ]  First element at position 0 N-element array c c[ 0 ], c[ 1 ] … c[ n - 1 ]  Nth element as position N-1
  • 3. Arrays Array elements are treated like other variables  Assignment, printing for an integer array c c[ 0 ] = 3; cout << c[ 0 ]; Can perform operations inside subscript c[ 5 – 2 ] same as c[3]
  • 4. Declaring Arrays When declaring arrays, specify  Name  Type of array  Any data type  Number of elements  type arrayName[ arraySize ]; Declaring multiple arrays of same type  Use comma separated list, like regular variables int c[ 10 ]; // array of 10 integers float d[ 3284 ]; // array of 3284 floats int b[ 100 ], x[ 27 ];
  • 5. Initializing arrays  For loop  Set each element  Initializer list  Specify each element when array declared  If not enough initializers, rightmost elements 0  If too many syntax error  To set every element to same value  If array size omitted, initializers determine size  5 initializers, therefore 5 element array int n[ 5 ] = { 1, 2, 3, 4, 5 }; int n[ 5 ] = { 0 }; int n[] = { 1, 2, 3, 4, 5 };
  • 6. Passing Arrays to Functions Specify name without brackets  To pass array myArray to myFunction  Array size usually passed, but not required  Useful to iterate over all elements Arrays passed-by-reference  Functions can modify original array data  Value of name of array is address of first element  Function knows where the array is stored  Can change original memory locations Individual array elements passed-by-value  Like regular variables int myArray[ 24 ]; myFunction( myArray, 24 ); square( myArray[3] );
  • 7. Passing Arrays to Functions Functions taking arrays  Function prototype  Names optional in prototype  Both take an integer array and a single integer  No need for array size between brackets  Ignored by compiler  If declare array parameter as const  Cannot be modified (compiler error) void modifyArray( int b[], int arraySize ); void modifyArray( int [], int ); void doNotModify( const int [] );
  • 8. Thank you Please subscribe my channel for more videos and courses