You are on page 1of 5

18 Exercise (2D arrays): Write a program that asks the user to enter the data of 4X4 array, the

program then should swap the rows of a 2D array Sample output:


please enter data of list2D: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Matrix with Reversed rows: 13 14 15 16 9 10 11 12 5 6 7 8 1 2 3 4

#include <iostream> using namespace std; const int rowSize=4; const int colSize=4; int main( ) { double list2D[rowSize][colSize]; int row, col; double temp; cout<<"please enter data of list2D:"<<endl; for(row=0; row<rowSize;row++) { for(col=0; col<colSize;col++) cin>>list2D[row][col]; } for(row=0; row<rowSize/2;row++) { for(col=0; col<colSize;col++) { temp = list2D[row][col]; list2D[row][col]= list2D[rowSize-row-1][col]; list2D[rowSize-row-1][col]=temp; } } cout<<"Matrix with Reversed rows:"<<endl; for(row=0; row<rowSize;row++) { for(col=0; col<colSize;col++) cout<<list2D[row][col]<<"\t"; cout<<endl; } system("pause"); return 0; }

19

Exercise#2: Write a program that asks the user to enter the data of 4X4 array. Then the program should reverse the elements in the matrix diagonal Example: If the input is
List2D [0] [1] [2] [3] [0] 1 0 0 14 [1] 0 2 13 0 [2] 0 12 3 0 [3] 11 0 0 4

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 <iostream> using namespace std; const int rowSize=4; const int colSize=4; int main() { double list2D[rowSize][colSize]; int row, col; double temp; cout<<"please enter data of list2D:"<<endl; for(row=0; row<rowSize;row++) { for(col=0; col<colSize;col++) cin>>list2D[row][col]; } //reverse main diagonal for(row=0; row<rowSize/2;row++) { temp = list2D[row][row]; list2D[row][row]= list2D[rowSize-1-row][rowSize-1-row]; list2D[rowSize-1-row][rowSize-1-row]=temp; } //reverse opposite diagonal for(row=0; row<rowSize/2;row++) { temp = list2D[row][rowSize-row-1]; list2D[row][rowSize-row-1]= list2D[rowSize-row-1][row]; list2D[rowSize-row-1][row]=temp; } cout<<"Matrix with Reversed diagonal:"<<endl; for(row=0; row<rowSize;row++) { for(col=0; col<colSize;col++) cout<<list2D[row][col]<<"\t"; cout<<endl; } system("pause"); return 0; }

20 Passing 2D arrays to functions Write a function that finds out the largest element in the main diagonal #include <iostream> #include <iomanip> #include<fstream> using 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<<"please enter data of list2D:"<<endl; for(row=0; row<rowSize;row++) { for(col=0; col<colSize;col++) cin>>list2D[row][col]; } maxDiagonal(list2D); system("pause"); return 0; } void maxDiagonal(int List[ ][colSize]) { int largest = List[0][0]; for(int row=1; row<rowSize;row++) { if(largest < List[row][row]) largest = List[row][row]; } cout<<"The largest value in diagonal = "<<largest; }

21 Write a function that finds the number of vowels per row and columns (as shown in the output: #include <iostream> The data in list2D: using namespace std; abc void vowel(char A[ ][10], int rowSize, int colSize); AIE int main() Xwq { int row, col; The number of vowels per row: char list2D[10][10]= { {a, b, c}, Row 1 = 1 {A, I, E}, {X, w, q} }; Row 2 = 3 cout<<The data in list2D:<<endl; Row 3 = 0 for(row=0; row<3;row++) The number of vowels per column: { Column 1 = 2 for(col=0; col<3;col++) Column 2 = 1 cout<<list2D[row][col]<< ; Column 3 = 1 cout<<endl; } vowel(list2D,3,3); system(pause); return 0; }
void vowel(char A[ ][10], int rowSize, int colSize) { int row, col; int num; cout<<The number of vowels per row:<<endl; for(row=0; row<rowSize;row++) { num=0; for(col=0; col<colSize;col++) { switch(A[row][col]) { case A: case E: case I: case O: case U: case a: case e: case i: case o: case u: num++; break; } } cout<<Row <<row+1<< = <<num<<endl; } cout<<The number of vowels per column:<<endl; for(col=0; col<colSize;col++) { num=0; for(row=0; row<rowSize;row++) { switch(A[row][col]) { case A: case E: case I: case O: case U: case a: case e: case i: case o: case u: num++; break; } } cout<<Column <<col+1<< = <<num<<endl; } }

22 Arrays of strings Declare an array of 3 strings: string list[3]; list[0]=ITCS; //store ITCS in index 0 cout<<list[0]; //print the string ITCS

0 1 3

ITCS

Use for loop to read the 3 strings and save them in the array for(int i=0; i<3;i++) cin>>list[i]; Another way of declaring 2D arrays typedef int table2D[4][4]; table2D A; // A is a 2D array of 4X4 elements
A [0] [1] [2] [3] [0] [1] [2] [3]

A function named Initialize that accepts a2D array of type table2D and initializes the 2D array elements to zer0 void Initialize (table2D A) { for(int i=0;i<4;i++) { for(j=0;j<4;j++) A[i][j]=0; } } Multi-dimensional Arrays Declare a three-dimensional array, where the size of the first dimension is 2, and the size of the second dimension is 4, and the third dimension has the size 4. int list3D[2][3][4]; Initializing list3D[0][0][0] to zero list3D[0][0][0] = 23; Initializing each element in list3D to zero for( i=0;i<2;i++) for(j=0;j<3;j++) for(k=0;k<4;k++) list3D[i][j][k]=0;

You might also like