CPSC 230 Exercise/Home Work

Review 3 – Stream 1, 2 and 3; Structure ; Class 1, and 2 .

Department of Physics, Computer Science and Engineering

Christopher Newport University

Question 1

For each of the following, write a single statement that performs the indicated task.

1.1

Print 1.92, 1.925 and 1.9258 with 3 digits of precision using manipulator.

1.2

Read characters into character array line until the character ‘z’ is encountered up to a limit of 20 characters ( including a terminating null character). Do not extract the delimiter character from the stream.

1.3

Print the current precision setting using a member function.

1.4

Print integer 100 in decimal, octal, and hexadecimal using a single stream manipulator to change the base.

Question 2

Assume that the following statement applies to a program. write a single statement that performs the indicated task.

2.1

Write a statement that opens file “trans.dat” for input; use ifstream object inTransaction.

2.2

Write a statement that reads a record from the file “oldmast.dat”. The record consists of integer accountNum, string name and floating-point currentBalance; use ifstream object inOldMaster. .

2.3

Assume that the following statement applies to a program.

Write a statement that writes a record to the file “newmast.dat”. The record consists of integer accountNum, string name, and floating-point currentBalance; use ofstream object outNewMaster.

Question 3

State whether the following are true or false

3.1

One structure can not have two fields with the same type of data.

3.2

A field in a structure can not be a structure.

Question 4

Create a class called Rational for performing arithmetic with fractions.

(a) Use integer variables to represent the private data of the class - numerator and denominator.

(b) Provide a constructor function that enables an object of this class to be initialized when it is declare- 0 for numerator and 1 for denominator.

(c) Provide public member functions of addition, subtraction, multiplication, division and printing for fractions.

Fractions have the form

numerator/denominator

numerator and denominator are integers.

Question 5

Write a program that uses the sizeof operator to determine the sizes in bytes of the various data types on your computer systems. Write the results to the file “datasize.dat” so you may print the results later. The format for the results in the file should be

Date type Size

char 1

unsigned char 1

short int 2

unsigned short int 2

int 4

unsigned int 4

long int 4

unsigned long int 4

float 4

double 8

long double 16

Question 6

Create a class called Complex for performing

arithmetic with complex numbers. Write a driver program

to test you class. Complex numbers have the form

realPart + imaginaryPart*i

where i is square root of (-1)

Use double variables to represent the private data of the class - real part and imaginary part. The constructor should contain default values in case no initializers are provided. Provide public member functions for each of the following:

(a) Addition of two Complex numbers: The real parts are added together and the imaginary parts are added together.

(b) Subtraction of two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.

(c) Printing Complex numbers in the form ( a, b) where a is the real part and b is the imaginary part.

(d) Setting Complex number.