Sample Programs for CMIS 102

These sample programs each illustrate one or more of the features of the C++ programming language. To study the program, read the source program, then compile and run the program. Relate the behavior of the program to the statements in the source code.

To get set up for studying these samples:

  1. Create a folder named SamplePrograms (or some such)
  2. Open the associated .zip file
  3. Extract the contents of the .zip file to the folder you created.

In the folder, you will find source code and a Dev-C++ project file (.dev) for each sample. To run a sample program, double-click on the corresponding .dev file. Dev-C++ will open. In the Dev-C++ menu, select Execute/Compile & Run. A .exe file will be created and run. To rerun the program, just double-click the .exe file.

Index of the Sample Programs

Sample Number What it does What is illustrates
1 Some computations, with no outputs. Variable declarations and assignment statements
2 Tells you whether an integer you entered is negative, zero, or positive. if...then... else control structure
3 Tells you whether what you entered is a valid int, a valid float, or neither. If your entry is a valid int or float, it tells you the value. Use of the CMString class; also use of a while loop to continue program execution until the user closes the window.
4 Similar to Sample 2, but uses CMString to do input validation. Input validation
5 Asks the user if she wants to quit; loops until user enters "Yes". Post-test "while" loop
6 Determines the smallest power of 2 greater than a given number. Post-test "while" loop
7 Requires the user to enter an integer. A useful approach to input data validation
8 "for" loop demo: you enter a start index, a final index, and a step size. The program sets up two different "for" loops and tells you how many times the bodies of the loops will be executed. "for" loops
9 Computes N factorial (that is, 1*2*...*N). A function
10 A recursive function to compute N factorial. A function
11 Computes the largest of three numbers. A function
12 Count how many times a character occurs in a String. A function; post-test while loop.
13 Constructs a string containing a specified number of a specified character. A function can return any type of value
14 Computes x to the power y. Use of a library function
15 Counts the total number of characters is up to ten strings. Arrays; "for" loop