Lecture 1: Overview - City University of New York

Download Report

Transcript Lecture 1: Overview - City University of New York

Lecture 21:Arrays and Strings(cont.)
Introduction to Computer Science
Spring 2006
1
Arrays

Array - a collection of a fixed number of elements



All the elements of an array must be the same data type;
The elements of an array are stored sequentially in memory.
One-dimensional array - an array in which the components are
arranged in a list form

The syntax for declaring a one-dimensional array is:
dataType arrayName[intExp];
where intExp is any expression that evaluates to a positive integer

Example: int num[5];
2
#include <iostream>
#include <iomanip>
using namespace std;
const int arraySize = 10;
void fill_Array(double list[], int listSize)
{
for(int i=0; i<listSize; ++i)
cin>>list[i];
}
void print_Array(double list[], int listSize)
{
for(int i=0; i<listSize; ++i)
cout<<setw(5)<<list[i];
}
void copy_Array(const double listOne[], double listTwo[], int listOneSize)
{
for(int i=0; i<listOneSize; ++i)
listTwo[i] = listOne[i];
}
void square_Array(double list[], int listSize)
{
int k;
for (int i = 0; i < listSize; i++)
{
k = i + 1;
/* i runs from 0 to 9 */
/* k runs from 1 to 10 */
list[i] = k*k;
cout << "The square of " << k << " is " << list[i] << endl;
}
}
int main()
{
double listA[arraySize]={0};
double listB[arraySize];
fill_Array(listA, arraySize);
print_Array(listA, arraySize);
copy_Array(listA, listB, arraySize);
square_Array(listA, arraySize);
}
3
Two-Dimensional Arrays

Two-dimensional Array: a collection of a fixed number of
components arranged in two dimensions

The syntax for declaring a two-dimensional array is:
dataType arrayName[intexp1][intexp2];
where intexp1 and intexp2 are expressions yielding positive integer values



The two expressions intexp1 and intexp2 specify the number of rows and
the number of columns, respectively, in the array
Two-dimensional arrays are sometimes called matrixes or tables
Two-dimensional arrays are stored in row order

The first row is stored first, followed by the second row, followed by the
third row and so on
4
Two-Dimensional Arrays(cont.)

Example: double sales[10][5];
5
Accessing Array Components

The syntax to access a component of a two-dimensional array is:
arrayName[indexexp1][indexexp2]
where indexexp1 and indexexp2 are expressions yielding nonnegative integer
values


indexexp1 specifies the row position and indexexp2 specifies the
column position
Example: sales[5][3]=25.75;
6
Accessing Array Components

A common way to access the elements of a two-dimensional arrays is
with nested for loops.
const int MAXI = 50;
const int MAXJ = 75;
int main()
{
int i;
int j;
float values[MAXI][MAXJ];
}
for (i = 0; i < MAXI; i++) {
for (j = 0; j < MAXJ; j++) {
values[i][j] = whatever;
}
}
7
Initialization
Like one-dimensional arrays



Two-dimensional arrays can be initialized when they are declared
Example:

int board[4][3] = { {2, 3, 1},
{15, 25, 13},
{20, 4, 7},
{11, 18, 14} }
To initialize a two-dimensional array when it is declared:
• Elements of each row are enclosed within braces and separated by
commas
• All rows are enclosed within braces
• For number arrays, if all components of a row are not specified, the
unspecified components are initialized to zero
8
Processing Two-Dimensional Arrays

A two-dimensional array can be processed in three
different ways:
1.
Process the entire array
2.
Process a particular row of the array, called row processing
3.
Process a particular column of the array, called column
processing
9
Processing Two-Dimensional Arrays :
Row processing and Column processing

We can process a particular row or column of a twodimensional array as a one-dimensional array


use algorithms similar to processing one-dimensional arrays
For example:

the following for loop initializes row four to zero:
row = 4;
for(col = 0; col < columns; col++)
matrix[row][col] = 0;

The following for loop inputs data in row 4 of matrix:
row = 4;
for(col = 0; col < columns; col++)
cin>>matrix[row][col];
10
Processing Two-Dimensional Arrays :
Entire Array


We can process the entire array by using nested for
loop
Example:

The following nested for loop initialize the entire matrix:
for(row = 0; row < rows; row++)
{
for(col = 0; col < columns; col++)
{
matrix[row][col] = 0;
}
}
11
Print

The following nested for loops print the components
of matrix, one row per line:
for(row = 0; row < rows; row++)
{
for(col = 0; col < columns; col++)
{
cout<<setw(5)<<matrix[row][col]<<" ";
}
cout<<endl;
}
12
Input

The following nested loop input data in each
component of the matrix
for(row = 0; row < rows; row++)
for(col = 0; col < columns; col++)
cin>>matrix[row][col];
13
Passing Two-Dimensional Arrays as
Parameters to Functions




Two-dimensional arrays can be passed as parameters to a
function
By default, arrays are passed by reference
The base address, that is, the address of the first component of
the actual parameter is passed to the formal parameter
When declaring a two-dimensional array as a formal parameter


Can omit size of first dimension, but not the second
Number of columns must be specified
14
#include <iostream>
#include <iomanip>
using namespace std;
const int MAXI = 10;
const int MAXJ = 15;
void init_matrix(double matrix[MAXI][MAXJ])
{
for(int i=0; i<MAXI; ++i)
for(int j=0; j<MAXJ; ++j)
matrix[i][j]=i+j;
}
void print_matrix(double matrix[MAXI][MAXJ])
{
for(int i=0; i<MAXI; ++i)
{
for(int j=0; j<MAXJ; ++j)
cout<<setw(5)<<matrix[i][j];
cout<<endl;
}
}
double sum_matrix(double matrix[MAXI][MAXJ])
{
double sum=0.0;
for(int i=0; i<MAXI; ++i)
for(int j=0; j<MAXJ; ++j)
sum = sum + matrix[i][j];
return sum;
}
int main()
{
double matrix[MAXI][MAXJ];
double sum;
init_matrix(matrix);
print_matrix(matrix);
sum = sum_matrix(matrix);
}
cout<<"The sum of all the elements of matrix is "<<sum<<endl;
15
End of lecture 21
Thank you!
16