chapter 9 - arrays - handout 5

Upload: zainabcom

Post on 03-Jun-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Chapter 9 - Arrays - Handout 5

    1/5

  • 8/13/2019 Chapter 9 - Arrays - Handout 5

    2/5

    19

    Exercise#2:

    Write a program that asks the user to enter the data of 4X4 array. Then the programshould reverse the elements in the matrix diagonal

    Example:If the input is

    Then, you should reverse diagonals as follows:(1)The main diagonal:

    a. Swap list2D[0][0] with list2D[3][3]b. Swap list2D[1][1] with list2D[2][2]

    (2)The opposite diagonal:

    a. Swap list2D[0][3] with list2D[3][0]b. Swap list2D[1][2] with list2D[2][1]

    #include using namespace std;

    const int rowSize=4; const int colSize=4;int main(){ double list2D[rowSize][colSize];int row, col; double temp;cout

  • 8/13/2019 Chapter 9 - Arrays - Handout 5

    3/5

    20

    Passing 2D arrays to functions

    Write a function that finds out the largest element in the main diagonal

    #include

    #include

    #includeusing namespace std;

    const int rowSize=4;

    const int colSize=4;

    void maxDiagonal(int List[ ][colSize]);int main()

    {

    int list2D[rowSize][colSize];int row, col;

    cout

  • 8/13/2019 Chapter 9 - Arrays - Handout 5

    4/5

    21

    Write a function that finds the number of vowels per row and columns (as shown in

    the output:

    #include using namespace std;

    void vowel(char A[ ][10], int rowSize, int colSize);

    int main(){ int row, col;char list2D[10][10]= { {a, b, c},

    {A, I, E}, {X, w, q} };

    cout

  • 8/13/2019 Chapter 9 - Arrays - Handout 5

    5/5

    22

    Arrays of strings

    Declare an array of 3 strings:

    string list[3];list[0]=ITCS; //store ITCS in index 0

    cout