Arrays
Arrays <ul><li>a series of elements of the same type placed in contiguous memory locations that can be individually refere...
What are Arrays? <ul><li>An array is a data structure which holds multiple variables of the same data type. </li></ul><ul>...
Array Structure num 0  1  2  3  4 Name of Array Index numbers
Declaring an ARRAY <ul><li>General Syntax: </li></ul><ul><li>type name [elements]; </li></ul><ul><li>Where:  </li></ul><ul...
<ul><li>e.g. </li></ul><ul><li>int sample [ 5 ]; </li></ul><ul><li>The elements field within brackets [ ] which represents...
Declaring Arrays <ul><li>#include <iostream.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int num...
Initializing ARRAYS <ul><li>When declaring a regular array of local scope (within a function, for example), if we do not s...
<ul><li>int sample [5] = { 16, 2, 77, 40, 12071 };   </li></ul><ul><li>The amount of values between braces { } must not be...
Initializing Arrays <ul><li>#include <iostream.h> </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>int count...
Accessing the values of an ARRAY <ul><li>In any point of a program in which an array is visible, we can access the value o...
<ul><li>e.g., to store the value 75 in the third element of sample:  </li></ul><ul><li>sample[2] =   75 </li></ul><ul><li>...
An Array example <ul><li>int num[4]; </li></ul><ul><li>num[0] = 101; </li></ul><ul><li>num[1] = 232; </li></ul><ul><li>num...
<ul><li>Square brackets [ ]  perform two different tasks:  </li></ul><ul><ul><li>to specify the size of arrays when they a...
Some Valid Operations with Arrays <ul><li>sample[0] = a; </li></ul><ul><li>sample[a] = 75; </li></ul><ul><li>b = sample [a...
Sample Implementation: <ul><li>// arrays example </li></ul><ul><li>#include <iostream.h> </li></ul><ul><li>int sample [] =...
Programming Exercise: <ul><li>Create a program that allows input of integer values in an array and prints them in reverse ...
Programming Quiz: <ul><li>Translate the following algorithm to C++ source code: </li></ul><ul><ul><li>create an array with...
Character Sequences <ul><li>the C++ Standard Library implements a powerful string class, which is very useful to handle an...
<ul><li>array of characters can store shorter sequences than its total length  </li></ul><ul><li>a special character is us...
Upcoming SlideShare
Loading in …5
×

2 arrays

996 views
889 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
996
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
55
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

2 arrays

  1. 1. Arrays
  2. 2. Arrays <ul><li>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. </li></ul><ul><li>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. </li></ul>Sample
  3. 3. What are Arrays? <ul><li>An array is a data structure which holds multiple variables of the same data type. </li></ul><ul><li>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. </li></ul><ul><li>In C++, indexing starts at zero . </li></ul><ul><li>Arrays, like other variables in C++, must be declared before they can be used. </li></ul>
  4. 4. Array Structure num 0 1 2 3 4 Name of Array Index numbers
  5. 5. Declaring an ARRAY <ul><li>General Syntax: </li></ul><ul><li>type name [elements]; </li></ul><ul><li>Where: </li></ul><ul><ul><li>type is a valid type (like int , float ...) </li></ul></ul><ul><ul><li>name is a valid identifier </li></ul></ul><ul><ul><li>elements field (which is always enclosed in square brackets [ ]), specifies how many of these elements the array has to contain. </li></ul></ul>
  6. 6. <ul><li>e.g. </li></ul><ul><li>int sample [ 5 ]; </li></ul><ul><li>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. </li></ul>Sample
  7. 7. Declaring Arrays <ul><li>#include <iostream.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int numbers[100]; </li></ul><ul><li>int averages[]= {10,20,30}; </li></ul><ul><li>numbers[0] = 10; </li></ul><ul><li>} </li></ul>
  8. 8. Initializing ARRAYS <ul><li>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. </li></ul><ul><li>Global and static arrays, on the other hand, are automatically initialized filled with zeros. </li></ul><ul><li>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 { }. </li></ul>
  9. 9. <ul><li>int sample [5] = { 16, 2, 77, 40, 12071 }; </li></ul><ul><li>The amount of values between braces { } must not be larger than the number of elements that we declare for the array between square brackets [ ]. </li></ul><ul><li>When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty [ ]. </li></ul><ul><li>int sample [] = { 16, 2, 77, 40, 12071 }; </li></ul>16 40 77 2 12071 Sample
  10. 10. Initializing Arrays <ul><li>#include <iostream.h> </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>int count; </li></ul><ul><li>int values[100]; </li></ul><ul><li>for( count = 0; count < 100; </li></ul><ul><li>count++ ) </li></ul><ul><li>values[count] = 10; </li></ul><ul><li>} </li></ul>
  11. 11. Accessing the values of an ARRAY <ul><li>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. </li></ul><ul><li>name[index] </li></ul>Sample[3] Sample[0] Sample[2] Sample[1] Sample[4] Sample
  12. 12. <ul><li>e.g., to store the value 75 in the third element of sample: </li></ul><ul><li>sample[2] = 75 </li></ul><ul><li>e.g., to pass the value of the third element of sample to a variable called a: </li></ul><ul><li>a = sample[2]; </li></ul>
  13. 13. An Array example <ul><li>int num[4]; </li></ul><ul><li>num[0] = 101; </li></ul><ul><li>num[1] = 232; </li></ul><ul><li>num[2] = 231; </li></ul><ul><li>num[3] = 0; </li></ul>The array num has a length of 4
  14. 14. <ul><li>Square brackets [ ] perform two different tasks: </li></ul><ul><ul><li>to specify the size of arrays when they are declared; </li></ul></ul><ul><ul><li>to specify indices for concrete array elements. </li></ul></ul><ul><li>int sample[5]; // declaration of a new Array </li></ul><ul><li>sample[2] = 75; // access to an element of the Array. </li></ul>
  15. 15. Some Valid Operations with Arrays <ul><li>sample[0] = a; </li></ul><ul><li>sample[a] = 75; </li></ul><ul><li>b = sample [a+2]; </li></ul><ul><li>sample[sample[a]] = sample[2] + 5; </li></ul>
  16. 16. Sample Implementation: <ul><li>// arrays example </li></ul><ul><li>#include <iostream.h> </li></ul><ul><li>int sample [] = {16, 2, 77, 40, 12071}; </li></ul><ul><li>int n, </li></ul><ul><li>result=0; </li></ul><ul><li>int main () </li></ul><ul><li>{ for ( n=0 ; n<5 ; n++ ) </li></ul><ul><li>{result += sample[n];} </li></ul><ul><li>cout << result; </li></ul><ul><li>return 0;} </li></ul>
  17. 17. Programming Exercise: <ul><li>Create a program that allows input of integer values in an array and prints them in reverse order. </li></ul><ul><li>The size of the array is 10. </li></ul><ul><li>Sample Run: </li></ul><ul><ul><li>Input: 5,10,15,20,25,30,35,40,45,50… </li></ul></ul><ul><ul><li>Output: … 50,45,40,35,30,25,20,15,10… </li></ul></ul>
  18. 18. Programming Quiz: <ul><li>Translate the following algorithm to C++ source code: </li></ul><ul><ul><li>create an array with a maximum of 200 float elements; initialize. </li></ul></ul><ul><ul><li>enter positive numbers (elements) into array. </li></ul></ul><ul><ul><li>total all elements. </li></ul></ul><ul><ul><li>get the average. </li></ul></ul><ul><ul><li>scan the array (once again) </li></ul></ul><ul><ul><li>count the number of elements with values greater than 10% above average. </li></ul></ul><ul><ul><li>output results. </li></ul></ul>
  19. 19. Character Sequences <ul><li>the C++ Standard Library implements a powerful string class, which is very useful to handle and manipulate strings of characters. </li></ul><ul><li>However, strings can also be represented as plain arrays of char elements. </li></ul><ul><li>e.g. char sampchar [20]; </li></ul>
  20. 20. <ul><li>array of characters can store shorter sequences than its total length </li></ul><ul><li>a special character is used to signal the end of the valid sequence: the null character , whose literal constant can be written as '' (backslash, zero). </li></ul><ul><li>Hence, to initialize array of characters: </li></ul><ul><li>char myword[] = { 'H', 'e', 'l', 'l', 'o', '' }; </li></ul>sampchar

×