Topic 1_week 1 Csc138
-
Upload
amirul-mutaqin -
Category
Documents
-
view
86 -
download
2
Transcript of Topic 1_week 1 Csc138
CSC138 Structured Programming
Topic 1:Topic 1:Multidimensional ArrayMultidimensional Array
CSC138 Structured Programming
DefinitionsDefinitionsARRAY A collection of a fixed number of elements
(called components) of the same type.
1-DIMENSIONAL ARRAY An array in which the elements are arranged
in a list form.
2-DIMENSIONAL ARRAY A collection of a fixed number of elements
(called components) arranged in rows and columns.
n-DIMENSIONAL ARRAY A collection of a fixed number of elements
arranged in n dimensions (n>=1).
CSC138 Structured Programming
2D Array declaration2D Array declaration
dataType arrayName [intExp1] [intExp2];SYNTAX
Example: double sales [10][5];
Specify thenumber of rows
Specify thenumber of columns
sales [0] [1] [2] [3] [4]
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
CSC138 Structured Programming
Accessing Array Accessing Array ComponentsComponents
arrayName [indexExp1][indexExp2];SYNTAX
Example: sales [5] [3] = 25.75;
Specify therow position
Specify theColumn position
sales [0] [1] [2] [3] [4]
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
CSC138 Structured Programming
2D Array Initialization During 2D Array Initialization During DeclarationDeclaration
int board [4] [3] = { {2, 3, 1}, {15, 25, 13}, {20, 4, 7},
{11,18,14} };
board [0] [1] [2]
[0] 2 3 1
[1]
[2]
[3]
To initialize a 2D array when it is declared:
1) The elements of each row are enclosed within curly braces and separated by commas.
2) All rows are enclosed within curly braces.
3) For number arrays, if all components of a row are not specified, the unspecified components are initialized to 0.
CSC138 Structured Programming
2D Arrays of different data 2D Arrays of different data typestypes
int a[30][10]; // declares an int array of 30 rows and 10 columns.
float matrix[20][25]; //declares a float array of 20 rows and 25 columns.
double wages[12][12]; //declares a double array of 12 rows and 12 columns.
char ticTacToeBoard[3][3] = {{'x', 'x', 'o'}, {'o', 'o', 'x'}, {'x', 'o', ' '} }; //declare and initialize a character array of 3 rows and 3 columns
CSC138 Structured Programming
2D Arrays of different data 2D Arrays of different data typestypes
char Name[6][10] = {"Mr. Bean", "Mr. Bush", "Nicole", "Kidman", "Arnold", "Jodie"};
//declare and initialize a character string array
CSC138 Structured Programming
2D Array Operations (1)2D Array Operations (1)A 2D array can be processed in three ways:
1) Process the entire array
2) Process a particular row of the array row processing
3) Process a particular column of the array column processing
CSC138 Structured Programming
2D Array Operations (2)2D Array Operations (2)Example:
const int NO_ROWS =4;const int NO_COLUMNS =3;
int matrix [NO_ROWS][NO_COLUMNS];
int row, col, sum, largest, temp;
matrix [0] [1] [2]
[0]
[1]
[2]
[3]
CSC138 Structured Programming
INITIALIZATIONINITIALIZATION
Initializing a single row:for (col = 0; col < NO_COLUMNS; col++)
matrix [3] [col] = 0;
Initializing a single row:for (col = 0; col < NO_COLUMNS; col++)
matrix [3] [col] = 0;
2D Array Operations (3)2D Array Operations (3)
Initializing the entire array:for (row = 0; row < NO_ROWS; row++)
for (col = 0; col < NO_COLUMNS; col++)matrix [row] [col] = 0;
Initializing the entire array:for (row = 0; row < NO_ROWS; row++)
for (col = 0; col < NO_COLUMNS; col++)matrix [row] [col] = 0;
CSC138 Structured Programming
PRINTPRINT
Output the components of the array:for (row = 0; row < NO_ROWS; row++){ for (col = 0; col < NO_COLUMNS; col++)
cout << matrix [row] [col] << “ “;cout << endl;
}
Output the components of the array:for (row = 0; row < NO_ROWS; row++){ for (col = 0; col < NO_COLUMNS; col++)
cout << matrix [row] [col] << “ “;cout << endl;
}
2D Array Operations (4)2D Array Operations (4)
CSC138 Structured Programming
INPUTINPUT
Input data into a single row:for (col = 0; col < NO_COLUMNS; col++)
cin >> matrix [4] [col] ;
Input data into a single row:for (col = 0; col < NO_COLUMNS; col++)
cin >> matrix [4] [col] ;
2D Array Operations (5)2D Array Operations (5)
Input data into each component of the array:for (row = 0; row < NO_ROWS; row++)
for (col = 0; col < NO_COLUMNS; col++)cin >> matrix [row] [col];
Input data into each component of the array:for (row = 0; row < NO_ROWS; row++)
for (col = 0; col < NO_COLUMNS; col++)cin >> matrix [row] [col];
CSC138 Structured Programming
SUM BY ROWSUM BY ROW
Find the sum of a single row:sum = 0;for (col = 0; col < NO_COLUMNS; col++)
sum = sum + matrix[row][col];
Find the sum of a single row:sum = 0;for (col = 0; col < NO_COLUMNS; col++)
sum = sum + matrix[row][col];
2D Array Operations (6)2D Array Operations (6)
Find the sum of each row separately:for (row = 0; row < NO_ROWS; row++){ sum = 0;
for (col = 0; col < NO_COLUMNS; col++)sum = sum + matrix [row] [col];
cout<<“Sum of row “<< row + 1 << “ = “ << sum << endl;}
Find the sum of each row separately:for (row = 0; row < NO_ROWS; row++){ sum = 0;
for (col = 0; col < NO_COLUMNS; col++)sum = sum + matrix [row] [col];
cout<<“Sum of row “<< row + 1 << “ = “ << sum << endl;}
CSC138 Structured Programming
SUM BY COLUMNSUM BY COLUMN
2D Array Operations (7)2D Array Operations (7)
Find the sum of each individual column:
for (col = 0; col < NO_COLUMNS; col++){sum = 0;
for (row = 0; row < NO_ROWS; row++)sum = sum + matrix [row] [col];
cout<<“Sum of column “<< col + 1 << “ = “ << sum << endl;
}
Find the sum of each individual column:
for (col = 0; col < NO_COLUMNS; col++){sum = 0;
for (row = 0; row < NO_ROWS; row++)sum = sum + matrix [row] [col];
cout<<“Sum of column “<< col + 1 << “ = “ << sum << endl;
}
CSC138 Structured Programming
LARGEST ELEMENT IN A SINGLE ROWLARGEST ELEMENT IN A SINGLE ROW
2D Array Operations (8)2D Array Operations (8)
Find the largest element in row number 4:row = 4;largest = matrix [row][0]; // Assume that the first element
//of the row is the largest.
for (col = 1; col < NO_COLUMNS; col++)if (largest < matrix [ row] [ col])
largest = matrix [row] [col];
cout <<“The largest element in row 4” << largest;
Find the largest element in row number 4:row = 4;largest = matrix [row][0]; // Assume that the first element
//of the row is the largest.
for (col = 1; col < NO_COLUMNS; col++)if (largest < matrix [ row] [ col])
largest = matrix [row] [col];
cout <<“The largest element in row 4” << largest;
CSC138 Structured Programming
LARGEST ELEMENT IN EACH ROW AND EACH COLUMNLARGEST ELEMENT IN EACH ROW AND EACH COLUMN
2D Array Operations (9)2D Array Operations (9)
Find the largest element in each row:for (row = 0; row < NO_ROWS; row++){largest = matrix [row][0]; // Assume that the first element
//of the row is the largest.for (col = 1; col < NO_COLUMNS; col++)
if (largest < matrix [ row] [ col])largest = matrix [row] [col];
cout <<“The largest element in row ” << row + 1 << “ = “ << largest << endl;
}
Find the largest element in each row:for (row = 0; row < NO_ROWS; row++){largest = matrix [row][0]; // Assume that the first element
//of the row is the largest.for (col = 1; col < NO_COLUMNS; col++)
if (largest < matrix [ row] [ col])largest = matrix [row] [col];
cout <<“The largest element in row ” << row + 1 << “ = “ << largest << endl;
}
CSC138 Structured Programming
LARGEST ELEMENT IN EACH ROW AND EACH COLUMNLARGEST ELEMENT IN EACH ROW AND EACH COLUMN
2D Array Operations (10)2D Array Operations (10)
Find the largest element in each column:for (col = 1; col < NO_COLUMNS; col++){largest = matrix [0][col]; // Assume that the first element
//of the row is the largest.for (row = 1; row < NO_ROWS; row++)
if (largest < matrix [ row] [ col])largest = matrix [row] [col];
cout <<“The largest element in column ” << col + 1 << “ = “ << largest << endl;
}
Find the largest element in each column:for (col = 1; col < NO_COLUMNS; col++){largest = matrix [0][col]; // Assume that the first element
//of the row is the largest.for (row = 1; row < NO_ROWS; row++)
if (largest < matrix [ row] [ col])largest = matrix [row] [col];
cout <<“The largest element in column ” << col + 1 << “ = “ << largest << endl;
}
CSC138 Structured Programming
Lab Assignment 1: 2D Array Basic Lab Assignment 1: 2D Array Basic OperationsOperations
1. Explain and differentiate types of programming paradigms: structured, OOP, logic, functional, etc.
2. Write a program that will declare and initialize the ScoresScores array.
• The program will perform the following processes on the Scores array:
a) find the total sum and the average of all elements in the array.
b) find the largest element in the array.c) find the smallest element in the array.d) Display all the elements in the array,
the total sum and the average of all elements, the largest element and the smallest element.
Scores [0] [1] [2] [3]
[0] 50.5 75.0 45.5 60.0
[1] 77.0 88.0 90.5 55.5
[2] 45.0 67.5 70.5 80.0