Lecture_3 Array - part2
Lecture_3 Array - part2
Lecture 3
Multidimensional Arrays
Introduction
you have used one-dimensional arrays to model linear
collections of elements.
You can use a two-dimensional array to represent a
matrix or a table.
For example, the following table that describes the
distances between the cities can be represented using a
two-dimensional array.
dataType arrayName[intexp1][intexp2];
3.3
Initialization of two-dimensional array
int Array[3][7];
Array[3][7]
3 rows 7 Columns
3.5
Accessing Two-Dimensional Array Elements
Caution
3.6
Processing a 2-D Array
A one-dimensional array is usually processed via a for
loop. Similarly, a two-dimensional array may be processed
with nested for loops:
// an array with 5 rows and 2 columns.
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
output
3.7
Example: Taking Input for Two Dimensional Array
#include <iostream>
using namespace std;
We have used a nested
for loop to take the
int main() { input of the 2d array.
int numbers[2][3];
Once all the input has
cout << "Enter 6 numbers: " << endl; been taken, we have
used another nested
// Storing user input in the array
for (int i = 0; i < 2; ++i) { for loop to print the
for (int j = 0; j < 3; ++j) { array members.
cin >> numbers[i][j];
}
} Enter 6 numbers:
1
cout << "The numbers are: " << endl; 2
3
// Printing array elements 4
for (int i = 0; i < 2; ++i) { 5
for (int j = 0; j < 3; ++j) { 6
cout << "numbers[" << i << "][" << j << "]: The numbers are:
" << numbers[i][j] << endl; numbers[0][0]: 1
}
numbers[0][1]: 2
}
cin.get();
numbers[0][2]: 3
return 0;} numbers[1][0]: 4
numbers[1][1]: 5
numbers[1][2]: 6
3.8
Example: Sum by Row 0 0 0
The following for loop finds the sum of each row of a 1 1 1
matrix. 2 2 2
3 3 3
#include <iostream>
using namespace std; 4 4 4
0 0 0
1 1 1
Assignment 1 2 2 2
3 3 3
4 4 4
3.10
Example: Largest element in each row 0 1 3
The following C++ code determines the largest element in 4 7 3
each row of the matrix 5 9 8
#include <iostream> 9 6 2
using namespace std; 7 3 1
int main() {
int largest; matrix[5][3]
// an array with 5 rows and 3 columns.
int matrix[5][3] = { {0,1,3}, {4,7,3}, {5,9,8}, {9,6,2},{7,3,1}};
for (int row = 0; row < 5; row++)
{
largest = matrix[row][0]; //Assume that the first element
//of the row is the largest.
for (int col = 1; col < 3; col++)
if (matrix[row][col] > largest)
largest = matrix[row][col];
cout << "The largest element in row " << row + 1 << " = "<<
largest << endl;
}
The largest element in row 1 = 3
cin.get(); The largest element in row 2 = 7
return 0; The largest element in row 3 = 9
}
The largest element in row 4 = 9
The largest element in row 5 = 7
3.11
Largest element in each column?
0 1 3
4 7 3
Assignment 2 5 9 8
9 6 2
7 3 1
Save the file as (Largest_clm.cpp)
matrix[5][3]
3.12
Higher-Dimensional Arrays
An array can be declared with multiple dimensions.
3.13
Initializing Three-Dimensional Array
Initialization in Three-Dimensional array is same as that of
Two-dimensional arrays. The difference is as the number of
dimension increases so the number of nested braces will also
increase.
Method 1:
int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 1,
3, 4, 5, 6, 6, 6, 6, 9, 9, 9, 9};
4 columns
Better Method:
int x[2][3][4] =
3 rows
Array 1
{
{ {0,1,2,3}, {4,5,6,7}, {8,9,1,1} },
{ {1,3,4,5}, {6,6,6,6}, {9,9,9,9} }
Array 2
};
x[2][3][4]
2 Arrays 4 Columns
3 Rows 3.14
Initializing Three-Dimensional Array
int x[10][5][7];
3.15
Processing a 3-D Array
• Following is a simple C++ program to initialize three-dimensional
(3D) array of dimensions 2*3*4, then it will access some
elements present in the array and display the element on the
screen :
3.17
Two-Dimensional Array Manipulation
3x3 arrays' subscripts and
The following example prints the 3 x 3 array’s their respective elements
subscript and their element. --------------------------
[0][0]=10
#include <iostream> [0][1]=25
using namespace std; [0][2]=33
int main() [1][0]=21
{ [1][1]=32
int i, j; [1][2]=43
int x[3][3]={{10,25,33}, [2][0]=20
{21,32,43},{20,42,51}}; [2][1]=42
cout<<"\n3x3 arrays' subscripts and\n"; [2][2]=51
cout<<"their respective elements\n";
cout<<"--------------------------\n";
// the outer for loop, reading the row by row...
for(i=0; i<3; i++)
// the inner loop,read every column by column...
for(j=0; j<3; j++)
cout<<"["<<i<<"]"<<"["<<j<<"]"<<"="<<x[i][j]<<"\n";
system("pause");
return 0;
}
3.18
Example1 - Array Size
#include <iostream>
#include <string>
using namespace std;
int main() {
// Declare Array Variable
int array[5]= { 18, 24, 23, 34, 15 };
int size;
size = sizeof(array)/sizeof(int);
Output
3.19
EXERCISE 1
Identify error(s), if any, in the following array declarations.
If a statement is incorrect, provide the correct statement.
a. int primeNum[99];
b. int testScores[0];
c. string names[60];
d. int list100[0..99];
e. double[50] gpa;
3.20
EXERCISE 2
What is the output of the following program segment?
double list[5];
for (int i = 0; i < 5; i++)
list[i] = pow(i, 3) + i / 2.0;
cout << fixed << showpoint << setprecision(2);
3.21
EXERCISE 3
What is stored in list after the following C++ code executes?
int list[8];
list[0] = 1;
list[1] = 2;
list ? ? ? ? ? ? ? ?
3.22
EXERCISE 4
Determine whether the following array declarations are
valid. If a declaration is invalid, explain why.
a. int list[61];
b. strings names[20];
c. double gpa[];
d. double[-50] ratings[];
e. string flowers[35];
3.23
Assignment 1
uses a two-dimensional array grades to store the grades of a number of
students on multiple exams. ( use 10x3 array)
Show two-dimensional array in a tabular format, along with each student's
semester average.
Show Highest/Lowest grade of any student for the semester.
Show a bar chart of the distribution of all student grades for the semester.
Example of output:
The grades are:
Test 1 Test 2 Test 3 Overall grade distribution:
Average 0-9:
Student 1 87 96 70 84.33 10-19:
Student 2 68 87 90 81.67
20-29:
Student 3 94 100 90 94.67
Student 4 100 81 82 87.67 30-39:
Student 5 83 65 85 77.67 40-49:
Student 6 78 87 65 76.67 50-59:
Student 7 85 75 83 81.00 60-69: ***
Student 8 91 94 100 95.00 70-79: ******
Student 9 76 72 84 77.33
80-89: ***********
Student 10 87 93 73 84.33
90-99: *******
Lowest grade in the grade book is 65 100: ***
Highest grade in the grade book is 100
3.25
Bonus Assignment (+2)
Write a program to solve N-Queens puzzle problem by generating random
solutions. The program will stop when a correct solution is found.
Represent the generated solution using NxN matrix as shown in the
example below.