UNIVERSITY COLLEGE OF THE FRASER VALLEY
COMP 150, Fall 1999 - Section MI1 (Mission)
Introduction to Programming with C++

Example C++ Programs


[ COMP150 Main Page ]


The following are example files that should be reviewed for practice and understanding.
(Taken from "Basic Programming Principles in C++ - Workbook", InThought Systems Ltd.)
They can be used to help understand any gray areas as well as help complete assignments.
All examples are designed for Borland C++ but should be compatible with Visual C++.

EXAMPLE NUMBER

DESCRIPTION

221.cpp

Enter two integers and display the sum.

231.cpp

A program that computes the GST on a retail price.

Ex231.cpp

Converts kilograms to pounds.

Ex232.cpp

Converts dollars to Canadian dollars.

Ex233.cpp

Converts inches to centimeters.

241.cpp

Illustrates how using literal spaces are not an acceptable way to format.

242.cpp

Illustrates how to RHJ (Right Hand Justify) two integers.

243.cpp

Illustrates how to add headings to your output.

244.cpp

Illustrates how to format decimal numbers.

Ex241.cpp

Allows the user to enter the average temperature, and the average rainfall.

251.cpp

Confirming that two methods can produce the same output.

252.cpp

Illustrates the result of integer division.

253.cpp

Illustrates the use of integer division and the modulo operator. Converts hours/minutes to total minutes.

Ex251.cpp

Calculates the earned run average for a pitcher.

261.cpp

Illustrates how a variable value can be overwritten.

262.cpp

Illustrates how the assignment of a float value to an "INT" variable causes truncation.

263.cpp

Illustrates that numeric variables of mixed types can be used in an arithmetic expression.

264.cpp

Test your understanding of type conversion. Can you predict the output of this program?

265.cpp

Allows the user to enter 3 exam scores and finds their average. Forces float division.

Ex261.cpp

Converts degrees Celsius to Fahrenheit, displaying the result to the nearest degree.

Ex262.cpp

Calculates the balance after one year of earning interest compounded twice yearly.

271.cpp

Illustrates how to format strings.

272.cpp

Illustrates how to position a string value after a numeric value.

273.cpp

Illustrates how to position a numeric value after a string value.

Ex271.cpp

Allows the user to enter a name, street address, city, postal code, # of hours worked, and hourly pay rate. Calculate gross and net pay along with taxes.

311.cpp

Converts pounds to kilograms or kilograms to pounds.

321.cpp

Illustrates how a non-zero condition makes the "IF" block of code execute while a zero condition makes the "ELSE" block of code execute.

322.cpp

Write a program that allows the user to enter the rainfall total for July. Part 1 of 2.

323.cpp

Checks to see if the entered rainfall amount is within the normal range of 24mm and 11mm. Part 2 of 2.

324.cpp

Compute the tax, according to table based on the user's taxable income.

Ex321.cpp

Finds the larger of two numbers entered by the user.

Ex322.cpp

Converts military time into standard time.

331.cpp

Write a program that allows the user to enter in the date in the form of year, month, day. Converts numeric date (i.e. 1998 11 23) to combination alpha-numeric (Nov 23, 1998).

332.cpp

Determines a maximum load value depending on the purchase price and credit rating.

Ex331.cpp

Converts a given month into a season.

341.cpp

Illustrates how you cannot trust a test for equality that involves a float or double.

342.cpp

 Illustrates how you cannot trust a test for equality that involves a float or double.

Ex411.cpp

Calculates the balance after one year of earning interest compounded a certain number of times.

Ex412.cpp

Modifies Ex411.cpp so that the user enter the term of the interest calculation as well as the number of compounds per year.

411.cpp

Calculates the balance after one year of earning interest compounded 12 times annually.

412.cpp

Calculates the balance after one year of earning interest compounded 12 time annually. User enters the initial balance and yearly interest as a percent.

413.cpp

Same as 411 and 412, but the balance at the end of the month is displayed and the interest is accumulated and displayed as well.

414.cpp

Calculates the interest earned and balance for a monthly deposit.

421.cpp

Shows how to use an exit controlled loop to validate an integer that must be between 1 and 10.

Ex421.cpp

Shows how to use cursor control to create a simple data entry screen with 2 numeric validations.

422.cpp

Does the same as 421 but uses cursor controls to enhance the input presentation.

Ex422.cpp

Modifies 424.cpp so that the integers entered are validated to be positive and less than 1,000.

423.cpp

Illustrates how to use an exit controlled loop to give the user the execution control over the program.

Ex423.cpp

 

424.cpp

Allows the user to enter any number of integers until a sentinel value of -999 is entered. Calculates the sum and average of those numbers entered.

431.cpp

This program contains an infinite loop. **** WARNING: DO NOT RUN THIS PROGRAM FROM THE BORLAND IDE. INSTEAD, COMPILE AND LINK THE CODE, THEN RUN THE EXE FILE FROM THE EXPLORER.

432.cpp

This program calculates and displays how many years of earning interest it will take in order to increase a balance from the starting value to an ending value.

511.cpp

Stores integers input from the keyboard in an array, then prints out the integers 3 per line.

Ex511.cpp

 Allows the user to enter up to 20 names. Displays the names in reverse order.

Ex512.cpp

The user enters a set of data for each employee. One employee has a name

Together with an hourly rate and hours worked. After all the data is entered, the program calculates and displays the gross pay for each employee.

Ex521.cpp

Uses a bubble sort to sort an array of strings.

522.cpp

Uses a bubble sort to sort an array of integers.

531.cpp

Replaces all occurrences of "+" with a blank in a string.

Ex531.cpp

 Modifies 532.cpp so that the original array is edited. No second array is used.

532.cpp

Creates a new array that contains only the positive integers from an array Assume that the array is not larger than 20 integers.

Ex532.cpp

Merges two sorted arrays into one sorted array.

Ex533.cpp

Extracts sub-strings delimited by a "*" out of a large string. The sub-strings are copied to an array of a string.

541.cpp

Creates a data entry screen for inventory. Each inventory item has an inventory number, quantity, and description.

Ex541.cpp

 Modifies 541.cpp so that the display of the inventory is sorted by inventory number.

Ex542.cpp

 

552.cpp

 Same as Ex521.cpp except that we use an array of char instead of the string class in order to represent strings.

561.cpp

 This program is a modification of 511.cpp so that the user cannot exceed the capacity of the array.

611.cpp

 Allows the user to enter any positive number and displays its square root.

Ex611.cpp

 Calculates the monthly mortgage payment using the calculations: Principal*MonthlyRate*Calc1/(Calc1-1).

621.cpp

 This program has a function that accepts two integers and returns their sums.

Ex621.cpp

 Uses a function to validate the range if an integer. This program only performs validated input. No output is attempted.

Ex622.cpp

 A function is used to calculate the standard deviation of an array of integers. The array is initialized instead of input from the user.

Ex631.cpp

 

632.cpp

 Illustrates how formal parameters are paired with actual parameters and that the formal parameters take on the values of their corresponding actual parameters.

633.cpp

 Illustrates the effects of changing the values of formal parameters.

634.cpp

 Illustrates how the pass by value mechanism is an important design feature when we create a function.

635.cpp

 This program illustrates that arrays are not passed by value.

636.cpp

 This program shows how to pass an array to a function and restrict the function's access to read only.

637.cpp

 This program shows how to pass a parameter by reference. We start with an array of integers. The function AppendData appends integers to the array and updates the size of the array. Note the second parameter in the function AppendData.

655.cpp

 Rewrite 541.cpp so that the data entry and display processes are housed in functions.

656.cpp

 This program is used in the project 656.ide. It allows a larger array to be used.