EECS 211
Control Flow Exercises

Below are exercises suitable for submission to the Code Critic after reading Chapters 1 through 6 of the textbook. Don't forget the rules of the queue.

The exercises are:


Exercise: Class Average

Chapter 4 of the textbook adds a new member function, determineClassAverage(), to the GradeBook class. Unfortunately, as defined, you have to manually enter grades at the terminal to test it. This is a very bad design. Every time you make changes to the code, you'll either have to run those manual tests, or, more likely, you'll skip that step and hope you didn't break anything.

A better approach is test-driven development (TDD), where you write testing code first. This naturally leads you to design programs for fast automatic testing.

Here are the basic steps. First, start with your GradeBook test file from this exercise.

Add a new test function called testClassAverage() that creates one or more gradebooks and repeatedly

When there are no grades, GradeBook::determineClassAverage( ) should return 0. Think about what would be a good sequence of grades to test the average calculation.

Compile and run your project. It shouldn't even compile, much less do the right thing.

Now, using or following the code from Figures 4.12 and 4.13 implement the member functions. Changes you need to make to the Deitel code include:

Neither addGrade( ) nor determineClassAverage( ) should read or print anything in your new code.

Don't forget to update both GradeBook.h and GradeBook.cpp with the new member function information.

Recompile and run. If any tests fail, fix and re-run.

Submit to Code Critic:

Remember to put a header comment at the start of the two code files that identifies which file they are.

Wrap Up

When the above has been approved via the Code Critic, run make handin.

Email the Zip archive produced by your Makefile with the Subject EECS 211: Class Average.

Exercise: Integer Powers

Use TDD to develop the following global functions integers:

There should be no reading or printing in any of the above functions.

Put the tests in PowerTests.cpp, and the solution code in Power.h and Power.cpp.

Use TDD and write your tests for these functions first. Think about both easy cases, like the ones above, and possible the tricky cases. Submit your tests first to the Code Critic. You can work on the implementation while waiting for feedback but do not submit your solution until your tests have been approved.

Do not use any standard math library functions for these, just the basic arithmetic operators and control structures. This rule is for this exercise -- in general, using library functions is the correct thing to do.

Test your tests. Try trivial definitions like always returning m, and make sure the right tests fail, before doing the real code.

Submit to Code Critic:

Remember to put a header comment at the start of these files that identifies what file they are.

Wrap Up

When the above has been approved via the Code Critic, run make handin.

Email the Zip archive produced by your Makefile with the Subject EECS 211: Integer Powers.

Exercise: Number Theory

Use TDD to develop the following global functions from simple number theory:

There should be no reading or printing in any of the above functions.

Put the tests in MathTests.cpp, and the solution code in Math.h and Math.cpp.

GCD() and LCM() should accept zero and negative values, but always return non-negative results. For example, the GCD of 49 and -91 should be 7 and the LCM of those numbers should be 637. The GCD of 0 and any number is the number itself, and the LCM of 0 and any number is 0.

Use TDD and develop these one function at a time. Focusing on one function will help you get familiar with the concepts involved.

Don't repeat yourself (DRY). There are some common operations needed to implement these functions that should be refactored into subfunctions.

Don't reinvent the wheel. E.g., don't reinvent what's in standard libraries like cstdlib or cmath.

Submit to Code Critic:

Remember to put a header comment at the start of these files that identifies what file they are.

Wrap Up

When the above has been approved via the Code Critic, run make handin.

Email the Zip archive produced by your Makefile with the Subject EECS 211: Number Theory.

Exercise: Generic Math

Use TDD to develop the global generic template functions (Section 6.18) tmax() and tmin(). ("T" is for template.)

These should be defined to handle 2 or 3 parameters. tmax() should return the largest of the arguments passed, tmin() the smallest. The only restriction when calling these functions is that the three parameters have the same type, and that type can be compared using <.

To handle 2 versus 3 parameters, use overloaded function definitions (Section 6.17).

There should be no reading or printing in any of the above functions. Do not use any standard C++ libraries in your definitions.

Put the tests in GenericMathTests.cpp, and the solution code in GenericMath.h.

Put all template code in header files. Otherwise, the compiler won't know about them when translating your code.

Make different test functions for each overloaded function, i.e., one for tmax() with 2 arguments, another for tmax() with 3 arguments, etc. Each test function should at least test with integers, doubles, characters, and C++ strings.

Avoid repeated code. Make good use of the conditional operator. (Warning: The book has an answer that's way too verbose.)

Submit to Code Critic:

Remember to put a header comment at the start of these files that identifies what file they are.

Wrap Up

When the above has been approved via the Code Critic, run make handin.

Email the Zip archive produced by your Makefile with the Subject EECS 211: Generic Math.


Comments? comment icon Contact the Prof!

Valid HTML 4.01 Transitional