SlideShare une entreprise Scribd logo
1  sur  47
F2037 - PROGRAMMING
FUNDAMENTAL WITH C++
Unit 4.1 - Understand the use of array
INDEX
 Objective
 Introduction to Array

 One dimensional array

 Two dimensional array
OBJECTIVES
   At the end of this module, students should be
    able to:
     Declare and use an array
     Use the array statement in C++ program
     Accessing element in an array
ARRAY DEFINITION
   Array is a collection of data elements of the
    same type that are referenced by a common
    name.

   Used to process a collection of data all of which is
    of the same type, such as list of name, list of
    temperature.
ARRAY DEFINITION
 Array’s element consist of memory allocation and
  identified by index.
 Array size : 9


1st Index                 Indexs


    0       1   2     3      4     5   6     7       8
   Two types of arrays
       One-dimensional array
       Two-dimensional array
DECLARING ONE DIMENSIONAL
ARRAY
   Will have a single row and can have any number
    of columns.
 Will have only one subscript. Subscript refers to
  the dimension of the array.
 Array declaration of 10 alphabet
     type array_name[size]
     Eg : char huruf[10];
INITIALIZING ONE DIMENSIONAL
ARRAY
 Initialization is the process of assigning values to
  the array you have created.
 To assign initial values to each one of array’s
  elements we must enclose the values in curly
  braces ({ }) and separate them with comma (,). 
     Eg   : char huruf[5] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’};
INITIALIZING ONE DIMENSIONAL
ARRAY
  Eg: int nombor[3] = {3, 24, 31};



         first index   0        1     2


            nombor         3    24   31



 nombor[0];//3
 nombor[1];//24
 nombor[2];//31
 nombor[0+1];//nombor[1];//24
 nombor[3];
ACCESSING ELEMENT OF ONE
DIMENSIONAL ARRAY
   Element is accessed by its index
   Array index refers to the location of the values in an
    array.
   The first element will always have the array index as
    0.

Syntax :
<Variable name>[Array index] = Value;

For example:
  marks[0]=95;
  marks[1]=85;
  marks[2]=75;
ACCESSING ELEMENT OF ONE
DIMENSIONAL ARRAY
   Eg:
      int my_array[5] =    {11, 22, 33, 44, 55};

   to store the value 75 in the third element
    of my_array, we could write the following
    statement:
      my_array[2] = 75;

   to pass the value in 4th element of my_array and
    store the value into temporary variable,
    temp_value:
      int temp_value = my_array[3]; // also
      equals to 44
ACCESSING ELEMENT OF ONE
DIMENSIONAL ARRAY
   if the name of an array is name, then name[0] is
    the name of the element that is in position 0,
    name[1] is the name of the element that is in
    position 1, etc.
   in general, the nth element is in position n-1. So
    if the array has n elements, their names are
    name[0], name[1], name[2], …, name[n-
    1].
   it is important to be able to clearly distinguish
    between the two uses that brackets [ ] have
    related to arrays:
      int name[5]; // declaration of a new array
      name[2] = 75; // access to an element of the
      array.
EXAMPLE
   Program Student_Marks.cpp will illustrate how
    to declare an array, initialize and access its
    elements.
#include<iostream>
using namespace std;
void main()
{                                    marks[0]   : 95
  int marks[]={95,85,75,80,65};      marks[1]   : 85
  cout<<"marks[0] : "<<marks[0];     marks[2]   : 75
                                     marks[3]   : 80
  cout<<"nmarks[1] : "<<marks[1];
                                     marks[4]   : 65
  cout<<"nmarks[2] : "<<marks[2];
  cout<<"nmarks[3] : "<<marks[3];
  cout<<"nmarks[4] : "<<marks[4];
}
EXAMPLE
   Program Onedim_Int_Array.cpp illustrates how
    to initialize integer array and display its
    contents.
#include<iostream>
using namespace std;
void main()
{
  int y[4]={8,7,6,4};
  for(int i=0;i<4;i++)
  {
       cout<<y[i]<<"n";
  }
}
EXAMPLE
   Program Onedim_Char_Array_Name.cpp
    illustrates how to initialize a character array and
    display its contents.
#include<iostream>
using namespace std;
void main()
{
  char stud_name[]={‘M',‘A',‘F','I','A'};
  for(int i=0;i<=4;i++)
  {
       cout<<stud_name[i];
  }
}
ENTERING DATA INTO AN ARRAY
    When more number of values are to be stored
     in an array, a for loop can be used.
    The sample code shows how to use a for loop
     in an array.


      for(int i=0;i<5;i++)
      {
        cout<<“Enter the marks: ";
        cin>>marks[i];
      }
READING DATA FROM AN ARRAY
   You can use a for loop with a single cout
    statement to print the values from an array.


    for (int i=0;i<5;i++)
    {
        cout<<"Marks : "+marks[i]);
    }
EXAMPLE
   Program One_Int_Array.cpp illustrates how to
    accept five marks from the user and prints the
    values on the screen.
#include <iostream>
using namespace std;
void main()
{
       int marks[5];
       //Accepting the marks
       for(int i=0;i<5;i++){
          cout<<"Enter mark :";
          cin>>marks[i];
       }
       cout<<"nThe marks you have enter is"<<endl;
       //Displaying the array
       for(int i=0;i<5;i++){
           cout<<"Marks:"<<marks[i]<<endl;
       }
}
IN CLASS EXERCISE 4.1
 Declare an array alpha of 15 elements of type
  int.
 Access the value of tenth element of array alpha.

 Set the value of fifth element of array alpha to
  35.
 Set the value of ninth element of array alpha to
  the sum of fifth and sixth element of array
  alpha.
   Declare an array alpha of 15 elements of type
    int.
    int alpha [15];

   Access the value of tenth element of array alpha.
    alpha [9];

   Set the value of fifth element of array alpha to
    35.
    alpha [4] = 35;
   Set the value of ninth element of array alpha to
    the sum of fifth and sixth element of array
    alpha.
    alpha [8] = alpha [4] + alpha [5]
   What is the output

#include<iostream>
using namespace std;
void main()
{
  double num []= {2.0, 4.0, 6.5, 8.7};
  cout<<num[1+2];
}
   How to fill in value into array     Output:
                                      //program output
    #include <iostream>               1
    using namespace std;              2
     void main()                      3
      {                               4
      for(int i = 0; i < 10; ++i)     5
        {                             6
        cout << i+1 << “n";
                                      7
        }
                                      8
      }
                                      9
                                      10
#include <iostream>
using namespace std;
void main()
{
  int num[]={1,2,3,4,5,6,7,8,9,10};
  for(int i = 0; i < 10; ++i)
   {
       cout << num[i]<< "n";
   }
}
TWO-DIMENSIONAL ARRAY
 Two-dimensional arrays can be described as
  "arrays of arrays".
 For example, a two-dimensional array can be
  imagined as a two-dimensional table made of
  elements of a same uniform data type.
PRESENTATION OF TWO-
DIMENSIONAL ARRAY
   Assume that there are 5 students in a class and
    each of them study three different subjects, for
    example Mathematics, Physics and Chemistry.
Example
        int marks_table [5][3];
 
Syntax
    <Data type> <Variable name> [Row][Column];
TWO-DIMENSIONAL ARRAY
   Table jimmy represents a bidimensional array of
    3 by 5 elements of type int.




   The way to declare this array in C++ would be:
    int jimmy [3][5];
                                   column
                         row
INITIALIZING TWO-DIMENSIONAL
ARRAY
   Eg:
     int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
        Output:

          123
          456
     int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };
        Output:

         123
         450
       int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };
          Output:

           120
           400
ACCESSING ELEMENT IN TWO-
DIMENSIONAL ARRAY
 Element is accessed by the index of its row and
  column.
 Eg:
     Toaccess the element in the 2nd row and at the 4th
     column of this two-dimentional array, we can used
     the following code:
       jimmy[1][3];
#include<iostream>
using namespace std;
void main()
{
  int array2[ 2 ][ 3 ] = {{ 1, 2, 3} ,{4, 5,6 }};

    for(int index1=0;index1<2;index1++)
    {
         for(int index2=0;index2<3;index2++)
                cout<<array2[index1][index2] << " ";
         cout<<endl;
    }
}
OUTPUT
WHAT IS OUTPUT?
#include<iostream>
using namespace std;
void main()
{
   int marks_table[5][3] = {{83,99,74},
                           {88,90,72},{89,88,82},
                           {98,93,75},{78,60,65}};
   cout<<marks_table[1][2];
 }
#include<iostream>
#include<string>
using namespace std;
void main()
{
string Data [2][3];
//For first fow
Data[0][0] = "Lisa"; //lastname
Data[0][1] = "Sulaiman"; //firstname
Data[0][2] = "Kedah"; //location
    //Second row
    Data[1][0] = "Ali"; //lastname
    Data[1][1] = "Muhammad"; //firstname
    Data[1][2] = "Johor"; //location
    cout<<"LastnametFirstnametLocationn";
for(int i=0;i<2;i++){
      for(int j=0;j<3;j++){
      cout<<Data[i][j]<<"tt";
      }
      cout<<"n";//move to new line
      }
}
#include <iostream>
using namespace std;
void main()
{
           int array2[ 23 ][ 4 ];
           //Accepting the marks
           for (int row=0; row<2; row++) {
                        for(int col=0; col<3; col++){
                                    cout<<"Enter mark ["<<(row)<<"][" <<col <<"]: ";
                                    cin>>array2[row][col];
                        }
                                    cout<<endl;
                      }
           //display
           for(int row=0; row<2; row++){
                        for(int col=0; col<3; col++)
                        cout<<array2[row][col] << " ";
                        cout<<endl;
           }
}
OUTPUT
IN CLASS EXERCISE 4.2
 Declare an array beta of 10 rows and 20 columns
  of type int.
 Examine the following:

    double values[ ] [ ] = {
                       {1.2, 9.0, 3.2},
                       {9.2, 0.5, 1.5},
                       {7.3, 7.9, 4.8} } ;
    What is the value of values[2][1]?
   Which of the following statements constructs
    an array with 5 rows of 7 columns?
      long stuff[5][7];
      long[5][7];
      long stuff[7][5];
      long [7][5];
   Declare an array beta of 10 rows and 20 columns
    of type int.
    int beta [10][20]

o   Value of values[2][1]? 7.9

o   long stuff[5][7];
SUMMARY
 An array is a structured data type with a fixed
  number of elements.
 Every element of an array is of the same type
  and can be accessed by their index.
 Array index started with 0.

 Array can be initialized during declaration.

 A one-dimensional array has one subscript.

 In two-dimensional array, elements are arranged
  in table form.
 To access element from two-dimensional array,
  pair of indices is needed (index for row and index
  for column).

Contenu connexe

Tendances

Tendances (20)

Tsp branch and bound
Tsp branch and boundTsp branch and bound
Tsp branch and bound
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Array in C
Array in CArray in C
Array in C
 
Bipartite graph
Bipartite graphBipartite graph
Bipartite graph
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C language
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
 
Hamiltonian path
Hamiltonian pathHamiltonian path
Hamiltonian path
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
MatplotLib.pptx
MatplotLib.pptxMatplotLib.pptx
MatplotLib.pptx
 
Array
ArrayArray
Array
 
Strings in c
Strings in cStrings in c
Strings in c
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 

En vedette

FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3 rohassanie
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2rohassanie
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6rohassanie
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3rohassanie
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2rohassanie
 

En vedette (12)

FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
Fp201 unit1 1
Fp201 unit1 1Fp201 unit1 1
Fp201 unit1 1
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
Unit 1
Unit 1Unit 1
Unit 1
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 2
Unit 2Unit 2
Unit 2
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 

Similaire à Fp201 unit4 (20)

Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
Ch5 array nota
Ch5 array notaCh5 array nota
Ch5 array nota
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
Array
ArrayArray
Array
 
02 arrays
02 arrays02 arrays
02 arrays
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Array BPK 2
Array BPK 2Array BPK 2
Array BPK 2
 
Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
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
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
 

Plus de rohassanie

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012rohassanie
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5rohassanie
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2rohassanie
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1rohassanie
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3rohassanie
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2) rohassanie
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201rohassanie
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201rohassanie
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012rohassanie
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1rohassanie
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 studrohassanie
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 studrohassanie
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3rohassanie
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2rohassanie
 
Chapter 1 part 1
Chapter 1 part 1Chapter 1 part 1
Chapter 1 part 1rohassanie
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++rohassanie
 

Plus de rohassanie (20)

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
 
Lab ex 1
Lab ex 1Lab ex 1
Lab ex 1
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2)
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
 
Labsheet 5
Labsheet 5Labsheet 5
Labsheet 5
 
Labsheet 4
Labsheet 4Labsheet 4
Labsheet 4
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2
 
Chapter 1 part 1
Chapter 1 part 1Chapter 1 part 1
Chapter 1 part 1
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 

Fp201 unit4

  • 1. F2037 - PROGRAMMING FUNDAMENTAL WITH C++ Unit 4.1 - Understand the use of array
  • 2. INDEX  Objective  Introduction to Array  One dimensional array  Two dimensional array
  • 3. OBJECTIVES  At the end of this module, students should be able to:  Declare and use an array  Use the array statement in C++ program  Accessing element in an array
  • 4. ARRAY DEFINITION  Array is a collection of data elements of the same type that are referenced by a common name.  Used to process a collection of data all of which is of the same type, such as list of name, list of temperature.
  • 5. ARRAY DEFINITION  Array’s element consist of memory allocation and identified by index.  Array size : 9 1st Index Indexs 0 1 2 3 4 5 6 7 8
  • 6. Two types of arrays  One-dimensional array  Two-dimensional array
  • 7. DECLARING ONE DIMENSIONAL ARRAY  Will have a single row and can have any number of columns.  Will have only one subscript. Subscript refers to the dimension of the array.  Array declaration of 10 alphabet  type array_name[size]  Eg : char huruf[10];
  • 8. INITIALIZING ONE DIMENSIONAL ARRAY  Initialization is the process of assigning values to the array you have created.  To assign initial values to each one of array’s elements we must enclose the values in curly braces ({ }) and separate them with comma (,).   Eg : char huruf[5] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’};
  • 9. INITIALIZING ONE DIMENSIONAL ARRAY  Eg: int nombor[3] = {3, 24, 31}; first index 0 1 2 nombor 3 24 31 nombor[0];//3 nombor[1];//24 nombor[2];//31 nombor[0+1];//nombor[1];//24 nombor[3];
  • 10. ACCESSING ELEMENT OF ONE DIMENSIONAL ARRAY  Element is accessed by its index  Array index refers to the location of the values in an array.  The first element will always have the array index as 0. Syntax : <Variable name>[Array index] = Value; For example: marks[0]=95; marks[1]=85; marks[2]=75;
  • 11. ACCESSING ELEMENT OF ONE DIMENSIONAL ARRAY  Eg: int my_array[5] = {11, 22, 33, 44, 55};  to store the value 75 in the third element of my_array, we could write the following statement: my_array[2] = 75;  to pass the value in 4th element of my_array and store the value into temporary variable, temp_value: int temp_value = my_array[3]; // also equals to 44
  • 12. ACCESSING ELEMENT OF ONE DIMENSIONAL ARRAY  if the name of an array is name, then name[0] is the name of the element that is in position 0, name[1] is the name of the element that is in position 1, etc.  in general, the nth element is in position n-1. So if the array has n elements, their names are name[0], name[1], name[2], …, name[n- 1].  it is important to be able to clearly distinguish between the two uses that brackets [ ] have related to arrays: int name[5]; // declaration of a new array name[2] = 75; // access to an element of the array.
  • 13. EXAMPLE  Program Student_Marks.cpp will illustrate how to declare an array, initialize and access its elements.
  • 14. #include<iostream> using namespace std; void main() { marks[0] : 95 int marks[]={95,85,75,80,65}; marks[1] : 85 cout<<"marks[0] : "<<marks[0]; marks[2] : 75 marks[3] : 80 cout<<"nmarks[1] : "<<marks[1]; marks[4] : 65 cout<<"nmarks[2] : "<<marks[2]; cout<<"nmarks[3] : "<<marks[3]; cout<<"nmarks[4] : "<<marks[4]; }
  • 15. EXAMPLE  Program Onedim_Int_Array.cpp illustrates how to initialize integer array and display its contents.
  • 16. #include<iostream> using namespace std; void main() { int y[4]={8,7,6,4}; for(int i=0;i<4;i++) { cout<<y[i]<<"n"; } }
  • 17. EXAMPLE  Program Onedim_Char_Array_Name.cpp illustrates how to initialize a character array and display its contents.
  • 18. #include<iostream> using namespace std; void main() { char stud_name[]={‘M',‘A',‘F','I','A'}; for(int i=0;i<=4;i++) { cout<<stud_name[i]; } }
  • 19. ENTERING DATA INTO AN ARRAY  When more number of values are to be stored in an array, a for loop can be used.  The sample code shows how to use a for loop in an array. for(int i=0;i<5;i++) { cout<<“Enter the marks: "; cin>>marks[i]; }
  • 20. READING DATA FROM AN ARRAY  You can use a for loop with a single cout statement to print the values from an array. for (int i=0;i<5;i++) { cout<<"Marks : "+marks[i]); }
  • 21. EXAMPLE  Program One_Int_Array.cpp illustrates how to accept five marks from the user and prints the values on the screen.
  • 22. #include <iostream> using namespace std; void main() { int marks[5]; //Accepting the marks for(int i=0;i<5;i++){ cout<<"Enter mark :"; cin>>marks[i]; } cout<<"nThe marks you have enter is"<<endl; //Displaying the array for(int i=0;i<5;i++){ cout<<"Marks:"<<marks[i]<<endl; } }
  • 23.
  • 24. IN CLASS EXERCISE 4.1  Declare an array alpha of 15 elements of type int.  Access the value of tenth element of array alpha.  Set the value of fifth element of array alpha to 35.  Set the value of ninth element of array alpha to the sum of fifth and sixth element of array alpha.
  • 25. Declare an array alpha of 15 elements of type int. int alpha [15];  Access the value of tenth element of array alpha. alpha [9];  Set the value of fifth element of array alpha to 35. alpha [4] = 35;
  • 26. Set the value of ninth element of array alpha to the sum of fifth and sixth element of array alpha. alpha [8] = alpha [4] + alpha [5]
  • 27. What is the output #include<iostream> using namespace std; void main() { double num []= {2.0, 4.0, 6.5, 8.7}; cout<<num[1+2]; }
  • 28. How to fill in value into array  Output: //program output #include <iostream> 1 using namespace std; 2 void main() 3 { 4 for(int i = 0; i < 10; ++i) 5 { 6 cout << i+1 << “n"; 7 } 8 } 9 10
  • 29. #include <iostream> using namespace std; void main() { int num[]={1,2,3,4,5,6,7,8,9,10}; for(int i = 0; i < 10; ++i) { cout << num[i]<< "n"; } }
  • 30. TWO-DIMENSIONAL ARRAY  Two-dimensional arrays can be described as "arrays of arrays".  For example, a two-dimensional array can be imagined as a two-dimensional table made of elements of a same uniform data type.
  • 32. Assume that there are 5 students in a class and each of them study three different subjects, for example Mathematics, Physics and Chemistry.
  • 33. Example int marks_table [5][3];   Syntax <Data type> <Variable name> [Row][Column];
  • 34. TWO-DIMENSIONAL ARRAY  Table jimmy represents a bidimensional array of 3 by 5 elements of type int.  The way to declare this array in C++ would be: int jimmy [3][5]; column row
  • 35. INITIALIZING TWO-DIMENSIONAL ARRAY  Eg:  int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };  Output: 123 456  int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };  Output: 123 450  int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };  Output: 120 400
  • 36. ACCESSING ELEMENT IN TWO- DIMENSIONAL ARRAY  Element is accessed by the index of its row and column.  Eg:  Toaccess the element in the 2nd row and at the 4th column of this two-dimentional array, we can used the following code: jimmy[1][3];
  • 37. #include<iostream> using namespace std; void main() { int array2[ 2 ][ 3 ] = {{ 1, 2, 3} ,{4, 5,6 }}; for(int index1=0;index1<2;index1++) { for(int index2=0;index2<3;index2++) cout<<array2[index1][index2] << " "; cout<<endl; } }
  • 39. WHAT IS OUTPUT? #include<iostream> using namespace std; void main() { int marks_table[5][3] = {{83,99,74}, {88,90,72},{89,88,82}, {98,93,75},{78,60,65}}; cout<<marks_table[1][2]; }
  • 40.
  • 41. #include<iostream> #include<string> using namespace std; void main() { string Data [2][3]; //For first fow Data[0][0] = "Lisa"; //lastname Data[0][1] = "Sulaiman"; //firstname Data[0][2] = "Kedah"; //location //Second row Data[1][0] = "Ali"; //lastname Data[1][1] = "Muhammad"; //firstname Data[1][2] = "Johor"; //location cout<<"LastnametFirstnametLocationn"; for(int i=0;i<2;i++){ for(int j=0;j<3;j++){ cout<<Data[i][j]<<"tt"; } cout<<"n";//move to new line } }
  • 42. #include <iostream> using namespace std; void main() { int array2[ 23 ][ 4 ]; //Accepting the marks for (int row=0; row<2; row++) { for(int col=0; col<3; col++){ cout<<"Enter mark ["<<(row)<<"][" <<col <<"]: "; cin>>array2[row][col]; } cout<<endl; } //display for(int row=0; row<2; row++){ for(int col=0; col<3; col++) cout<<array2[row][col] << " "; cout<<endl; } }
  • 44. IN CLASS EXERCISE 4.2  Declare an array beta of 10 rows and 20 columns of type int.  Examine the following: double values[ ] [ ] = { {1.2, 9.0, 3.2}, {9.2, 0.5, 1.5}, {7.3, 7.9, 4.8} } ; What is the value of values[2][1]?
  • 45. Which of the following statements constructs an array with 5 rows of 7 columns? long stuff[5][7]; long[5][7]; long stuff[7][5]; long [7][5];
  • 46. Declare an array beta of 10 rows and 20 columns of type int. int beta [10][20] o Value of values[2][1]? 7.9 o long stuff[5][7];
  • 47. SUMMARY  An array is a structured data type with a fixed number of elements.  Every element of an array is of the same type and can be accessed by their index.  Array index started with 0.  Array can be initialized during declaration.  A one-dimensional array has one subscript.  In two-dimensional array, elements are arranged in table form.  To access element from two-dimensional array, pair of indices is needed (index for row and index for column).