CSC32610/12/2018
A)One-Dimensional Arrays
Forms:
ElementType arrayName[NumOfElements];
ElementType arrayName[NumOfElements]={initialize_list};
An array as an ADT is:
- fixed size: NumOfElements specifies the capacity of the array;
- ordered: indices are numbered 0, 1, 2, …, NumOfElements-1;
- same type elements: ElementType is the type of the elements;
- direct access: subscript operator [].
CONST numOfValues=50;
float values[numofValues];
SumValues (const float values[ ], int numOfValues )
//Precondition:values[ 0] through values[numOfValues-1]have been assigned
//Returns the sum of values[0] throughvalues[numOfValues-1]
{
float sum = 0;
for ( int index = 0; index < numOfValues; index++ )
{
sum += values [ index ] ;
}
return sum;
}
B)Two dimensional arrays
Forms:
ElementType arrayName[NUM_ROWS][NUM_COLUMNS];
const int NUM_STATES = 50 ;
const int NUM_MONTHS = 12 ;
int stateHighs [ NUM_STATES ] [ NUM_MONTHS ] ;
int stateAverages [NUM_STATES ];
int total = 0 ;
int month ;
int average ;
//find the total and average temperatures of 12 months for the 3rd state:
for ( month = 0 ; month < NUM_MONTHS ; month ++ )
total = total + stateHighs [ 2 ] [ month ] ;
average = int ( total / 12.0 + 0.5 ) ;
//find the average temperatures of 12 months for each state respectively:
void findAverages ( const int stateHighs [ ] [ NUM_MONTHS] ,
int stateAverages [ ] )
// Precondition:stateHighs[0..NUM_STATES-1][0..NUM_MONTHS-1] assigned
// Postcondition:stateAverages[0..NUM_STATES-1] contains rounded
// average high temperature for each state
{
int state;
int month;
int total;
for ( state = 0 ; state < NUM_STATES; state++ )
{
total = 0 ;
for ( month = 0 ; month < NUM_MONTHS ; month++ )
total += stateHighs [ state ] [ month ] ;
stateAverages [ state ] = int ( total / 12.0 + 0.5 ) ;
}
}
To avoid type mismatches, a typedef statement can be used:
typedef int StateHighsType [ NUM_STATES ] [ NUM_MONTHS ] ;
typedef int StateAveragesType [ NUM_STATES ] ;
void findAverages( const StateHighsType stateHighs , StateAveragesType stateAverages )
{
.
.
.
}
C)STRUCTS
Declaration of a struct:
struct Typename
{
Declarations of members // of any types
};
Forms of declarations of struct objects:
Typename structname;
Typename structname={initialize_list};
a)an example of a struct declaration
struct CarType
{
int year ;
char maker[10];
float price ;
} ;
CarType thisCar;//CarType variables
CarType myCar;
b)function passing by reference
void AdjustForInflation(CarType& car, float perCent)
// Increases price by the amount specified in perCent
{
car.price = car.price * perCent + car.price;
} ;
AdjustForInflation(myCar, 0.03);
c)function passing by value
bool LateModel(CarType car, int date)
// Returns true if the car’s model year is later than or
// equal to date; returns false otherwise.
{
return ( car.year >= date ) ;
} ;
if ( LateModel(myCar, 1995) )
std::cout < myCar.price < std::endl ;
d)array of struct:
struct Date
{
char month[10]; //name of month
int day, //day number
year; //year number
}
const int MAX_SCORES=4;
struct StudentClassRecord
{ //student’s
char name[20]; //complete name
Date birth; //birthday
int score[MAX_SCORES];//test scroes
}
CONST int COURSE_LIMIT=30;
StudentClassRecord csc126[COURSE_LIMIT];