際際滷

際際滷Share a Scribd company logo
Arrays
Arrays a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.  e.g.  we can store 5 values of type  int  in an array without having to declare 5 different variables, each one with a different identifier.  Sample
What are Arrays? An array is a data structure which holds multiple variables of the same data type. An array is a multi-element box, a bit like a filing cabinet, and uses an indexing system to find each variable stored within it.  In C++, indexing starts at  zero . Arrays, like other variables in C++, must be declared before they can be used.
Array Structure num 0  1  2  3  4 Name of Array Index numbers
Declaring an ARRAY General Syntax: type name [elements]; Where:  type is a valid type (like  int ,  float ...) name is a valid identifier  elements field (which is always enclosed in square brackets [ ]), specifies how many of these elements the array has to contain.
e.g. int sample [ 5 ]; The elements field within brackets [ ] which represents the number of elements the array is going to hold, must be a  constant  value, since arrays are blocks of non-dynamic memory whose size must be determined before execution. In order to create arrays with a variable length, dynamic memory is needed. Sample
Declaring Arrays #include <iostream.h> void main() { int numbers[100]; int averages[]= {10,20,30}; numbers[0] = 10; }
Initializing ARRAYS When declaring a regular array of local scope (within a function, for example), if we do not specify otherwise, its elements will not be initialized to any value by default, so their content will be undetermined until we store some value in them.  Global and static arrays, on the other hand, are automatically initialized filled with zeros.  In both cases, local and global, we have the possibility to assign initial values to each one of its elements by enclosing the values in braces { }.
int sample [5] = { 16, 2, 77, 40, 12071 };   The amount of values between braces { } must not be larger than the number of elements that we declare for the array between square brackets [ ].  When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty [ ].  int sample [] = { 16, 2, 77, 40, 12071 }; 16 40 77 2 12071 Sample
Initializing Arrays #include <iostream.h> main() { int count; int values[100]; for( count = 0; count < 100;  count++ ) values[count] = 10; }
Accessing the values of an ARRAY In any point of a program in which an array is visible, we can access the value of any of its elements individually as if it was a normal variable, thus being able to both read and modify its value.  name[index]  Sample[3] Sample[0] Sample[2] Sample[1] Sample[4] Sample
e.g., to store the value 75 in the third element of sample:  sample[2] =   75 e.g., to pass the value of the third element of sample to a variable called a:  a = sample[2];
An Array example int num[4]; num[0] = 101; num[1] = 232; num[2] = 231; num[3] = 0; The array  num  has a length of 4
Square brackets [ ]  perform two different tasks:  to specify the size of arrays when they are declared;  to specify indices for concrete array elements.  int sample[5];  // declaration of a new Array sample[2] = 75;  // access to an element of the Array.
Some Valid Operations with Arrays sample[0] = a; sample[a] = 75; b = sample [a+2]; sample[sample[a]] = sample[2] + 5;
Sample Implementation: // arrays example #include <iostream.h> int sample [] = {16, 2, 77, 40, 12071}; int n,  result=0; int main () {  for ( n=0 ; n<5 ; n++ )  {result += sample[n];}  cout << result;  return 0;}
Programming Exercise: Create a program that allows input of integer values in an array and prints them in reverse order. The size of the array is 10. Sample Run: Input:  5,10,15,20,25,30,35,40,45,50 Output:   50,45,40,35,30,25,20,15,10
Programming Quiz: Translate the following algorithm to C++ source code: create an array with a maximum of 200  float  elements; initialize.  enter positive numbers (elements) into array. total all elements.  get the  average.  scan the array (once again) count the number of elements with values  greater than 10% above average.  output results.
Character Sequences the C++ Standard Library implements a powerful string class, which is very useful to handle and manipulate strings of characters.  However, strings can also be represented as plain arrays of char elements.  e.g.  char sampchar [20];
array of characters can store shorter sequences than its total length  a special character is used to signal the end of the valid sequence: the  null character , whose literal constant can be written as '\0' (backslash, zero).  Hence, to initialize array of characters: char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' };   sampchar

More Related Content

What's hot (20)

SPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in CSPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in C
Mohammad Imam Hossain
Array Presentation
Array PresentationArray Presentation
Array Presentation
Deep Prajapati Microplacer
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
悖忰惆 忰惆
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
Uma mohan
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
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
Array in c++
Array in c++Array in c++
Array in c++
Mahesha Mano
Array lecture
Array lectureArray lecture
Array lecture
Joan Sa単o
Arrays
ArraysArrays
Arrays
Kulachi Hansraj Model School Ashok Vihar
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Shubham Sharma
Arrays
ArraysArrays
Arrays
Trupti Agrawal
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
Tareq Hasan
Array and string
Array and stringArray and string
Array and string
prashant chelani
Array C programming
Array C programmingArray C programming
Array C programming
Prionto Abdullah
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
Hareem Naz
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
Ashim Lamichhane
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
Rajendran
Chap09
Chap09Chap09
Chap09
Terry Yoast
Array
ArrayArray
Array
PRN USM

Viewers also liked (20)

Presentation about arrays
Presentation about arraysPresentation about arrays
Presentation about arrays
slave of Allah
Arrays
ArraysArrays
Arrays
fahadshakeel
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
Praveen Vishwakarma
Response To Intervention
Response To InterventionResponse To Intervention
Response To Intervention
Paul Schumann
Math grade 2 training
Math grade 2 trainingMath grade 2 training
Math grade 2 training
Anthony Smith
Grade 2 module 6 pd Katrice Hall
Grade 2 module 6 pd Katrice HallGrade 2 module 6 pd Katrice Hall
Grade 2 module 6 pd Katrice Hall
Katrice Hall
Dll esp 2
Dll esp 2Dll esp 2
Dll esp 2
Mary Ann Encinas
Filipino 3-tg-draft-4-10-2014
Filipino 3-tg-draft-4-10-2014Filipino 3-tg-draft-4-10-2014
Filipino 3-tg-draft-4-10-2014
jennifer Tuazon
Dll GRADE 2 MATH
Dll GRADE 2 MATHDll GRADE 2 MATH
Dll GRADE 2 MATH
Mary Ann Encinas
Stack Data structure
Stack Data structureStack Data structure
Stack Data structure
B Liyanage Asanka
Banghay
BanghayBanghay
Banghay
Zarm Dls
Pandiwa
PandiwaPandiwa
Pandiwa
Lea Mae Ann Violeta
K to 12 Grade 2 DLL MOTHER TONGUE BASED (Q1 Q4)
K to 12 Grade 2 DLL MOTHER TONGUE BASED (Q1  Q4)K to 12 Grade 2 DLL MOTHER TONGUE BASED (Q1  Q4)
K to 12 Grade 2 DLL MOTHER TONGUE BASED (Q1 Q4)
LiGhT ArOhL
Mission Impossible Part 2 EsP 8 Modyul 2
Mission Impossible Part 2 EsP 8 Modyul 2Mission Impossible Part 2 EsP 8 Modyul 2
Mission Impossible Part 2 EsP 8 Modyul 2
Edna Azarcon
K to 12 Grade 2 DLL Filipino (Q1 Q4)
K to 12 Grade 2 DLL Filipino  (Q1  Q4)K to 12 Grade 2 DLL Filipino  (Q1  Q4)
K to 12 Grade 2 DLL Filipino (Q1 Q4)
LiGhT ArOhL
Modyul 2
Modyul 2Modyul 2
Modyul 2
Betty Lapuz
Banghay aralin sa edukasyon sa pagpapakatao 9
Banghay aralin sa edukasyon sa pagpapakatao 9Banghay aralin sa edukasyon sa pagpapakatao 9
Banghay aralin sa edukasyon sa pagpapakatao 9
Jacobo Z. Gonzales Memorial National High School
K to 12 Araling Panlipunan Grade 2 (4th Quarter 1st Summative Test)
K to 12 Araling Panlipunan Grade 2 (4th Quarter 1st Summative Test)K to 12 Araling Panlipunan Grade 2 (4th Quarter 1st Summative Test)
K to 12 Araling Panlipunan Grade 2 (4th Quarter 1st Summative Test)
LiGhT ArOhL
K to 12 Araling Panlipunan Grade 2 (3rd Periodical Exam)
K to 12 Araling Panlipunan Grade 2  (3rd Periodical Exam)K to 12 Araling Panlipunan Grade 2  (3rd Periodical Exam)
K to 12 Araling Panlipunan Grade 2 (3rd Periodical Exam)
LiGhT ArOhL
K to 12 ENGLISH Grade 2 (4th Quarter 1st Summative Test)
K to 12 ENGLISH Grade 2 (4th Quarter 1st Summative Test)K to 12 ENGLISH Grade 2 (4th Quarter 1st Summative Test)
K to 12 ENGLISH Grade 2 (4th Quarter 1st Summative Test)
LiGhT ArOhL
Presentation about arrays
Presentation about arraysPresentation about arrays
Presentation about arrays
slave of Allah
Response To Intervention
Response To InterventionResponse To Intervention
Response To Intervention
Paul Schumann
Math grade 2 training
Math grade 2 trainingMath grade 2 training
Math grade 2 training
Anthony Smith
Grade 2 module 6 pd Katrice Hall
Grade 2 module 6 pd Katrice HallGrade 2 module 6 pd Katrice Hall
Grade 2 module 6 pd Katrice Hall
Katrice Hall
Filipino 3-tg-draft-4-10-2014
Filipino 3-tg-draft-4-10-2014Filipino 3-tg-draft-4-10-2014
Filipino 3-tg-draft-4-10-2014
jennifer Tuazon
Banghay
BanghayBanghay
Banghay
Zarm Dls
K to 12 Grade 2 DLL MOTHER TONGUE BASED (Q1 Q4)
K to 12 Grade 2 DLL MOTHER TONGUE BASED (Q1  Q4)K to 12 Grade 2 DLL MOTHER TONGUE BASED (Q1  Q4)
K to 12 Grade 2 DLL MOTHER TONGUE BASED (Q1 Q4)
LiGhT ArOhL
Mission Impossible Part 2 EsP 8 Modyul 2
Mission Impossible Part 2 EsP 8 Modyul 2Mission Impossible Part 2 EsP 8 Modyul 2
Mission Impossible Part 2 EsP 8 Modyul 2
Edna Azarcon
K to 12 Grade 2 DLL Filipino (Q1 Q4)
K to 12 Grade 2 DLL Filipino  (Q1  Q4)K to 12 Grade 2 DLL Filipino  (Q1  Q4)
K to 12 Grade 2 DLL Filipino (Q1 Q4)
LiGhT ArOhL
K to 12 Araling Panlipunan Grade 2 (4th Quarter 1st Summative Test)
K to 12 Araling Panlipunan Grade 2 (4th Quarter 1st Summative Test)K to 12 Araling Panlipunan Grade 2 (4th Quarter 1st Summative Test)
K to 12 Araling Panlipunan Grade 2 (4th Quarter 1st Summative Test)
LiGhT ArOhL
K to 12 Araling Panlipunan Grade 2 (3rd Periodical Exam)
K to 12 Araling Panlipunan Grade 2  (3rd Periodical Exam)K to 12 Araling Panlipunan Grade 2  (3rd Periodical Exam)
K to 12 Araling Panlipunan Grade 2 (3rd Periodical Exam)
LiGhT ArOhL
K to 12 ENGLISH Grade 2 (4th Quarter 1st Summative Test)
K to 12 ENGLISH Grade 2 (4th Quarter 1st Summative Test)K to 12 ENGLISH Grade 2 (4th Quarter 1st Summative Test)
K to 12 ENGLISH Grade 2 (4th Quarter 1st Summative Test)
LiGhT ArOhL

Similar to 2 arrays (20)

Arrays
ArraysArrays
Arrays
Notre Dame of Midsayap College
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
ajajkhan16
Arrays
ArraysArrays
Arrays
Steven Wallach
Array
ArrayArray
Array
hjasjhd
Arrays
ArraysArrays
Arrays
VenkataRangaRaoKommi1
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
berekethailu2
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
Munazza-Mah-Jabeen
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
TaseerRao
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
chanchal ghosh
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
Lecture 7
Lecture 7Lecture 7
Lecture 7
Mohammed Khan
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Dr.Subha Krishna
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
Ain-ul-Moiz Khawaja
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
KristinaBorooah
Chyuuuuuuuuuuryyyyyyyyyyy123456789786341.pptx
Chyuuuuuuuuuuryyyyyyyyyyy123456789786341.pptxChyuuuuuuuuuuryyyyyyyyyyy123456789786341.pptx
Chyuuuuuuuuuuryyyyyyyyyyy123456789786341.pptx
RobertCarreonBula
Arrays
ArraysArrays
Arrays
Chukka Nikhil Chakravarthy
Module 4- Arrays and Strings
Module 4- Arrays and StringsModule 4- Arrays and Strings
Module 4- Arrays and Strings
nikshaikh786
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
trupti1976

2 arrays

  • 2. Arrays a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. e.g. we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Sample
  • 3. What are Arrays? An array is a data structure which holds multiple variables of the same data type. An array is a multi-element box, a bit like a filing cabinet, and uses an indexing system to find each variable stored within it. In C++, indexing starts at zero . Arrays, like other variables in C++, must be declared before they can be used.
  • 4. Array Structure num 0 1 2 3 4 Name of Array Index numbers
  • 5. Declaring an ARRAY General Syntax: type name [elements]; Where: type is a valid type (like int , float ...) name is a valid identifier elements field (which is always enclosed in square brackets [ ]), specifies how many of these elements the array has to contain.
  • 6. e.g. int sample [ 5 ]; The elements field within brackets [ ] which represents the number of elements the array is going to hold, must be a constant value, since arrays are blocks of non-dynamic memory whose size must be determined before execution. In order to create arrays with a variable length, dynamic memory is needed. Sample
  • 7. Declaring Arrays #include <iostream.h> void main() { int numbers[100]; int averages[]= {10,20,30}; numbers[0] = 10; }
  • 8. Initializing ARRAYS When declaring a regular array of local scope (within a function, for example), if we do not specify otherwise, its elements will not be initialized to any value by default, so their content will be undetermined until we store some value in them. Global and static arrays, on the other hand, are automatically initialized filled with zeros. In both cases, local and global, we have the possibility to assign initial values to each one of its elements by enclosing the values in braces { }.
  • 9. int sample [5] = { 16, 2, 77, 40, 12071 }; The amount of values between braces { } must not be larger than the number of elements that we declare for the array between square brackets [ ]. When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty [ ]. int sample [] = { 16, 2, 77, 40, 12071 }; 16 40 77 2 12071 Sample
  • 10. Initializing Arrays #include <iostream.h> main() { int count; int values[100]; for( count = 0; count < 100; count++ ) values[count] = 10; }
  • 11. Accessing the values of an ARRAY In any point of a program in which an array is visible, we can access the value of any of its elements individually as if it was a normal variable, thus being able to both read and modify its value. name[index] Sample[3] Sample[0] Sample[2] Sample[1] Sample[4] Sample
  • 12. e.g., to store the value 75 in the third element of sample: sample[2] = 75 e.g., to pass the value of the third element of sample to a variable called a: a = sample[2];
  • 13. An Array example int num[4]; num[0] = 101; num[1] = 232; num[2] = 231; num[3] = 0; The array num has a length of 4
  • 14. Square brackets [ ] perform two different tasks: to specify the size of arrays when they are declared; to specify indices for concrete array elements. int sample[5]; // declaration of a new Array sample[2] = 75; // access to an element of the Array.
  • 15. Some Valid Operations with Arrays sample[0] = a; sample[a] = 75; b = sample [a+2]; sample[sample[a]] = sample[2] + 5;
  • 16. Sample Implementation: // arrays example #include <iostream.h> int sample [] = {16, 2, 77, 40, 12071}; int n, result=0; int main () { for ( n=0 ; n<5 ; n++ ) {result += sample[n];} cout << result; return 0;}
  • 17. Programming Exercise: Create a program that allows input of integer values in an array and prints them in reverse order. The size of the array is 10. Sample Run: Input: 5,10,15,20,25,30,35,40,45,50 Output: 50,45,40,35,30,25,20,15,10
  • 18. Programming Quiz: Translate the following algorithm to C++ source code: create an array with a maximum of 200 float elements; initialize. enter positive numbers (elements) into array. total all elements. get the average. scan the array (once again) count the number of elements with values greater than 10% above average. output results.
  • 19. Character Sequences the C++ Standard Library implements a powerful string class, which is very useful to handle and manipulate strings of characters. However, strings can also be represented as plain arrays of char elements. e.g. char sampchar [20];
  • 20. array of characters can store shorter sequences than its total length a special character is used to signal the end of the valid sequence: the null character , whose literal constant can be written as '\0' (backslash, zero). Hence, to initialize array of characters: char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' }; sampchar