Docsity

Exam II Review: Arrays and 2d Arrays in C++ - Prof. Keith E. Hellman, Study notes of Computer Science

Partial preview of the text

Download Exam II Review: Arrays and 2d Arrays in C++ - Prof. Keith E. Hellman and more Study notes Computer Science in PDF only on Docsity!

EXAM II REVIEW

August 5, 2009

Exam II Review Arrays

Write a C++ statement that declares an array of 4 Boolean values.

Exam II Review Arrays

Write a C++ statement that declares an array of 4 Boolean values.

Now do it another way...

Write a C++ statement that declares an array of 14 doubles initialized to zero.

Exam II Review Arrays

Write a C++ statement that declares an array of 4 Boolean values.

Now do it another way...

Write a C++ statement that declares an array of 14 doubles initialized to zero.

Can you write a C++ statement of 1,000,000 Boolean values initialized to

true?

Exam II Review Arrays

True or False? Arrays are always passed by value in function parameters.

Exam II Review Arrays

True or False? Arrays are always passed by value in function parameters.

Suppose that

double findMaxValue( const double data[], const int elements );

is available to your main routine that has an array of doubles declared as:

double myData[SIZE];

How do you find the maximum value of myData?

Exam II Review 2d Arrays

2d Arrays

Two rows of a valid sudoku grid

may be swaped, resulting in

another valid sudoku grid, as long

as the two rows are in the first,

middle, or last row of quads.

You can swap rows

4,8,9,5,3,... and

but you cannot swap

4,8,9,5,3,... and

Sudoku Puzzle No. 0-

Exam II Review 2d Arrays

2d Arrays

Write a function definition for

rowSwap that accepts:

1. A 2d array of integers named

puzzle with 9 rows and 9

columns.

2. Row offsets a and b,

rowSwap should be a void

function and (you guessed it),

swap rows a and b.

Sudoku Puzzle No. 0-

Exam II Review Functions

Consider the C++ source to the

right.

A What are the values of x, y, and z after line 36 has finished executing? B What are the values of x, y, and z after line 37 has finished executing? C What are the values of x, y, and z after line 38 has finished executing? D What are the values of x, y, and z after line 39 has finished executing? 11 v o i d r i g h t _ t o _ l e f t ( i n t l e f t , i n t & r i g h t ) 12 { 13 l e f t = r i g h t ; 14 } 15 16 v o i d l e f t _ t o _ r i g h t ( i n t l e f t , i n t & r i g h t ) 17 { 18 r i g h t = l e f t ; 19 } 20 21 v o i d i n c r e m e n t ( i n t & l e f t , i n t r i g h t ) 22 { 23 l e f t += 1 ; 24 r i g h t += l e f t ; 25 } 26 27 v o i d d e c r e m e n t ( i n t & l e f t , i n t r i g h t ) 28 { 29 r i g h t −= 1 ; 30 l e f t −= r i g h t ; 31 } 32 33 v o i d h e l t e r _ s k e l t e r ( v o i d ) 34 { 35 i n t x ( 0 ) , y ( 1 ) , z ( 2 ) ; 36 i n c r e m e n t ( x , y ) ; 37 r i g h t _ t o _ l e f t ( x , z ) ; 38 l e f t _ t o _ r i g h t ( x , z ) ; 39 d e c r e m e n t ( y , 0 ) ; 40 }

Exam II Review Classes

Consider the declaration for Data:

1 # include < iostream > 2 using namespace std ; 3 4 class Data { 5 public : 6 ostream & output ( ostream & os ) const ; 7 istream & input ( istream & is ); 8 void setB ( double newb ); 9 int setY ( int newy ); 10 double getA () const ; 11 double getB (); 12 double getX () const ; 13 Data process_data ( const Data & datum ) const ; 14 private : 15 double setAY ( double newa , int newy , int & oldy ); 16 Data process_data ( void ) const ; 17 Data process_data ( int newx , int newy ); 18 double a , b , c ; 19 int x , y ; 20 };

◮ How many public data

members does this class

declare?

Exam II Review Classes

Consider the declaration for Data:

1 # include < iostream > 2 using namespace std ; 3 4 class Data { 5 public : 6 ostream & output ( ostream & os ) const ; 7 istream & input ( istream & is ); 8 void setB ( double newb ); 9 int setY ( int newy ); 10 double getA () const ; 11 double getB (); 12 double getX () const ; 13 Data process_data ( const Data & datum ) const ; 14 private : 15 double setAY ( double newa , int newy , int & oldy ); 16 Data process_data ( void ) const ; 17 Data process_data ( int newx , int newy ); 18 double a , b , c ; 19 int x , y ; 20 };

◮ Which member functions

would be considered

accessor functions?

Exam II Review Classes

Consider the declaration for Data:

1 # include < iostream > 2 using namespace std ; 3 4 class Data { 5 public : 6 ostream & output ( ostream & os ) const ; 7 istream & input ( istream & is ); 8 void setB ( double newb ); 9 int setY ( int newy ); 10 double getA () const ; 11 double getB (); 12 double getX () const ; 13 Data process_data ( const Data & datum ) const ; 14 private : 15 double setAY ( double newa , int newy , int & oldy ); 16 Data process_data ( void ) const ; 17 Data process_data ( int newx , int newy ); 18 double a , b , c ; 19 int x , y ; 20 };

◮ What is unconventional

with the prototype on

line 11?

Exam II Review Classes

Consider the declaration for Data:

1 # include < iostream > 2 using namespace std ; 3 4 class Data { 5 public : 6 ostream & output ( ostream & os ) const ; 7 istream & input ( istream & is ); 8 void setB ( double newb ); 9 int setY ( int newy ); 10 double getA () const ; 11 double getB (); 12 double getX () const ; 13 Data process_data ( const Data & datum ) const ; 14 private : 15 double setAY ( double newa , int newy , int & oldy ); 16 Data process_data ( void ) const ; 17 Data process_data ( int newx , int newy ); 18 double a , b , c ; 19 int x , y ; 20 };

◮ Write the prototype for

Data’s default

constructor.

Exam II Review Classes

Consider the declaration for Data:

1 # include < iostream > 2 using namespace std ; 3 4 class Data { 5 public : 6 ostream & output ( ostream & os ) const ; 7 istream & input ( istream & is ); 8 void setB ( double newb ); 9 int setY ( int newy ); 10 double getA () const ; 11 double getB (); 12 double getX () const ; 13 Data process_data ( const Data & datum ) const ; 14 private : 15 double setAY ( double newa , int newy , int & oldy ); 16 Data process_data ( void ) const ; 17 Data process_data ( int newx , int newy ); 18 double a , b , c ; 19 int x , y ; 20 };

◮ Write the definition

(what would be written

in Data.cpp) of the

default constructor.

All data members should

be initialized to a value

of 0. Can you do this

without C++ statements

in the function body?