KEMBAR78
2 arrays | PPT
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

2 arrays

  • 1.
  • 2.
    Arrays a seriesof 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 num0 1 2 3 4 Name of Array Index numbers
  • 5.
    Declaring an ARRAYGeneral 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 Whendeclaring 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 valuesof 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 storethe 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 exampleint 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 Operationswith 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: Createa 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: Translatethe 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 theC++ 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 characterscan 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