Lab #1



MECH 415 Exam Review Questions #2

Question #1

a) Develop a class called Timer that is used to represent a count down timer. The member variables are as follows

int hour ; // hour changes from 0 to 23

int min ; // minute changes from 0 to 59

int sec; // second changes from 0 to 59

int *time;

// time is a dynamic 1D array that stores a copy of hour, min, and sec in time[0], time[1],

// and time[2], respectively.

Develop the following member functions (print out error messages and return where appropriate):

b) A constructor function that takes the arguments h, m, and s. The arguments are used in the constructor to initialize the hour, min, and sec member variables, respectively. The constructor should also allocate the time array and initialize it.

c) A destructor.

d) A member function called update_timer() that reduces the count down timer by one second. When the countdown timer reaches zero the timer should stop and print out the message “This is the end my friend”.

e) A member function called save(file_name) that saves the hour, minute, and second into a text file. The file name is stored in the character array (string variable) file_name.

f) A member function print_time() that prints the current time to the screen in the format “hour:minute:second”.

Question #2

Develop a function called strsearch that searches for a sub string s2 within a larger string s1. The function prototype will be as follows:

int string_search(char s1[], char s2[]);

The function should return the index of the first character in the larger string s1. If the string is not found it should return –1. For example, string_search(“appleorangepeach”,”orange”) will return 5 (because “orange” occurs in s1 beginning at character 5) and string_search(“appleorangepeach”,”pear”) will return –1. Don’t use string library functions to solve this question.

Question #3

Develop a function called file_compare(file1,file2) that compares two text files (file names stored in the string variables file1 and file2) one character at a time. The function returns 0 if each character in the two files are equal. Otherwise, it returns the number of characters that are not equal (if two files are of unequal length the remaining characters of the longer file would also be considered not equal).

Question #4

Develop a function called multiply(A,B,C) that multiplies two 10x10 matrices (2D arrays starting at [1][1]) A and B together, storing the result in C. The function will also return the number of zero elements in C plus the number of elements equal to 1.0 (Hint: C[i][j] = dot product of the ith row of A with the jth column of B).

Question #5

a) Develop a class called Container that is used to represent a dynamic 1D array of doubles. The elements of the array can be manipulated in various ways by the member functions described below. The member variables are as follows:

int Nmax; // the maximum size of the dynamic 1D array A

int N; // the present size of array A (i.e. the number of elements currently used in A)

double *A; // a dynamic 1D array of doubles

Note: It is assumed that N ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download