Opening Input/Output Files

advertisement
Opening Input/Output Files
ifstream infile;
ofstream outfile;
char inFileName[40];
char outFileName[40];
cout<<"Enter the input file name: ";
cin>>inFileName;
infile.open(inFileName); //open input file
.
.
.
cout<<"Enter the output file name: ";
cin>> outFileName;
outfile.open(outFileName);//open out putfile
Arrays
In this chapter, you will:
• Learn about arrays
• Explore how to declare and manipulate data into arrays
• Understand the meaning of “array index out of bounds”
• Become familiar with the restrictions on array processing
• Learn to use typedef statement
• Discover how to pass an array as a parameter to a function
• Learn about parallel arrays
• Discover how to manipulate data in a two-dimensional
array
• Learn about multidimensional arrays
• A data type is called simple if variables of that type can store
only one value at a time.
• A structured data type (or data structure) is one in which
each data item is a collection of other data items.
 An array is a collection of a fixed number of components
wherein all of the components are of the same data type.
 A one-dimensional array is an array in which the
components are arranged in a list form. The general form of
declaring a one-dimensional array is:
DataType arrayName[intExp];
Example:
The statement int list[10];
declares an array list of 10 components. Each component is of the
type int. The components are list[0], list[1]… list[9].
Accessing Array Components
The general form (syntax) of accessing an array component is:
arrayName[indexExp]
where indexExp, called index, is any expression whose value is a
nonnegative integer.
 Index value specifies the position of the component in the array.
 In C++, [] is an operator, called the array subscripting operator.
 In C++ array index starts at 0.
list[5] = 34;
Declaring arrays USING typedef
const int SIZE = 10;
typedef double ListType[SIZE];
ListType a;
ListType mylist;
which is equivalent to
double a[10];
double mylist[10];
Processing One-Dimensional Arrays
 Some of the basic operations performed on a one-dimensional array
are initialize, input data, output data stored in an array, find the
largest and/or smallest element.
 Each of these operations requires the ability to step through the
elements of the array.
 Stepping-through the elements of an array is easily accomplished by
a loop.
Consider a list declared as an array of the SIZE 100
 The following for loop steps-through each element of the
array list starting at the first element of list.
for(i = 0; i < SIZE; i++)
process list[i]
 If processing list requires inputting data into list, the
statement in Line 2 takes the from of an input statement,
such as the cin statement.
for(i = 0; i < SIZE; i++)
cin>>list[i];
Finding the sum and average of an array:
sum = 0;
for(index = 0; index < SIZE; index ++)
sum = sum + list[index];
average = sum / SIZE;
Finding the largest element in the array
maxIndex = 0;
for(index = 1; index < SIZE; index ++)
if(list[maxIndex] < list[index])
maxIndex = index;
largest = list[maxIndex];
Array Problems
 In C++, there is no guard against indices that are out of
bounds.
 Assignment, comparison of arrays, reading data into an
array and printing the contents of an array must be done
component-wise.
cin>>x;
if (x < y) …;
y = x;
//illegal
//illegal
// illegal
 In order to copy one array into another array use a loop.
for(j = 0; j < arraySize; j++)
y[j] = x[j];
Arrays as Parameters to Functions
 By Reference Only: In C++, arrays are passed by reference only.
 Since arrays are passed by reference only, we do not use the symbol
& when declaring an array as a formal parameter.
void initialize(ListType list, int SIZE)
{
int count;
for(count = 0; count < SIZE; count++)
list[count] = 0;
}
Parallel Arrays
Two (or more) Arrays are called parallel if their corresponding
components hold related information.
const int MAXSTUDENTS = 50;
typedef int IdList [MAXSTUDENTS];
typedef char GradesList [MAXSTUDENTS];
IdList studentIds;
GradeList courseGrades;
Two-dimensional Arrays
A two-dimensional array is a collection of a fixed number of
components arranged in two dimensions, and all components are of the
same type.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.
double sales[10][5];
declares a two-dimensional array sales of 10 rows and 5 columns
and every component is of the type float.
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.
The statement
sales[5][3] = 25.75;
stores 25.75 into row numbered 5 and column numbered 3 (that is,
6th row and 4th column) of the array sales. If
int i = 5;
int j = 3;
then the previous statement is equivalent to
sales[i][j] = 25.75;
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.
When processing a particular row or column of a two-dimensional
array we employ algorithms similar to when we process onedimensional arrays.
Suppose that we want to process row number, say 5 of
matrix. The components of row number 5 of matrix
are:
matrix [0][1][2][3][4][5]
[0]
matrix[5][0]
[1]
matrix[5][1]
[2]
matrix[5][2]
[3]
matrix[5][3]
[4]
[5]
[6]
matrix[5][4]
matrix[5][5]
const int ROWS = 7;
const int COLUMNS = 6;
int matrix[ROWS][COLUMNS];
int
int
int
int
int
row;
col;
sum;
largest;
temp;
//In these components the first index is fixed at 5.
row = 5;
//The second index ranges from 0 to 5.
for(col = 0; col < COLUMNS; col++)
process matrix[row][col]
Similarly, suppose that we want to process column number 2 of
matrix, that is, the third column of matrix. The components of
this column are:
matrix [0][1][2][3][4][5]
[0]
matrix[0][2]
[1]
matrix[1][2]
[2]
matrix[2][2]
[3]
matrix[3][2]
[4]
matrix[4][2]
[5]
matrix[5][2]
[6]
matrix[6][2]
• The second index is fixed at 2 and the first index ranges from 0 to 6.
• The following for loop processes column 2 of matrix.
col = 2;
for(row = 0; row < ROWS; row++)
process matrix[row][col]
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;
}
Largest Element in Each Row and Each Column
//Largest element in each row
for(row = 0; row < ROWS; row++)
{
largest = matrix[row][0];
for(col = 1; col < COLUMNS; col++)
if(largest < matrix[row][col])
largest = matrix[row][col];
cout<<"Largest element of row "<<row+1
<<" = "<<largest<<endl;
}
//Largest element in each column
for(col = 0; col < COLUMNS; col++)
{
largest = matrix[0][col];
for(row = 1; row < rows; row++)
if(largest < matrix[row][col])
largest = matrix[row][col];
cout<<"Largest element of col "<<col+1
<<" = "<<largest<<endl;
}
Other forms of Declaring Two-dimensional Arrays
const int ROWS = 20;
const int COLUMNS = 10;
typedef int TableType[ROWS][COLUMNS];
We can declare variables of the type TableType.
tableType matrix;
Passing Two-dimensional Arrays as Parameters to
Functions
• Two-dimensional arrays can be passed as parameters to a function
and they are passed by reference.
• When storing a two-dimensional array in the computer’s memory,
C++ uses the row order form. That is, first the first row is stored,
followed by the second row, which is followed by the third row
and so on.
void initialize(TableType table)
{
int row;
int col;
for(row = 0; row < ROWS; row++)
for(col = 0; col < COLUMNS; col++)
table[row][col] = 0;
}
Multi-dimensional Arrays
• Array: An array is a collection of a fixed number of elements (called
components) arranged in n dimensions (n >= 1), called an ndimensional array.
The general syntax of declaring an n-dimensional array is:
dataType arrayName[intExp1][intExp2]...[intExpn];
where intExp1, intExp2, … , and intExpn are constant
expressions yielding positive integer values.
The syntax of accessing a component of an n-dimensional array is:
arrayName[indexExp1][indexExp2]...[indexExpn]
where indexExp1,indexExp2,..., and indexExpn are
expressions yielding nonnegative integer values. indexExpi gives
the position of the array component in the ith dimension.
The statement
double carDealers[10][5][7];
declares carDealers to be a three-dimensional array.




Size of the first dimension is 10 and it ranges from 0 to 9.
The size of the second dimension is 5 and it ranges from 0 to 4.
The size of the third dimension is 7 and it ranges from 0 to 6.
The base address of the array carDealers is the address of the
first array component, that is the address of
carDealers[0][0][0].
 The total number of components in the array carDealers is
10*5*7 = 350.
carDealers[5][3][2] = 15564.75;
for(i = 0; i < 10; i++)
for(j = 0; j < 5; j++)
for(k = 0; k < 7; k++)
carDealers[i][j][k] = 0.0;
initializes the entire array to 0.0.
Download