C++ Snippetts

Junior year of high school, I studied C++ in my AP Computer Science class. I saved all the code I wrote in the class and have placed it on here for those of you who might find some basic C++ examples helpful in learning the language. Some of the code here requires the use of some CollegeBoard classes. If you experience any problems, change classes such as apvector and apstring to use the STL vector and string classes.

Lab01 - First Name

Summary

Basic Input/Output Program

Preview


/* Title     - First Name
 * File name - Lab01
 * Programmer- 415 Erich Musick
 * IPO       - Input person's first name, no processing, output person's first name
*/

#include 
#include 

using namespace std;

int main() {
  
        string firstName;

        cout << "Please enter your first name: ";
        cin >> firstName;

        cout << firstName;
        cout << ", congratulations on your first program!";
        cout << endl;

        return 0;

}

Lab03 - First and Last Name

Summary

Input person's first name and then last name, no processing, output person's first name and last name, with a congrats phrase.

Preview


/* Title     - First and Last Name
 * File name - Lab03
 * Programmer- 415 Erich Musick
 * IPO       - Input person's first name and then last name, no processing, 
 *                         output person's first name and last name, with a congrats phrase
*/

#include 
#include 

using namespace std;

int main() {
        string firstName;
        string lastName;

        cout << "Please enter your first name: ";
        cin >> firstName;
        cin.ignore(80,'\n');

        cout << endl << "Please enter your last name: ";
        cin >> lastName;
        cin.ignore(80,'\n');
        
        cout << endl;
        cout << firstName;
        cout << " ";
        cout <&&

Lab04 - Pythagorean Triple

Summary

User enters m and n - two positive integers, where m > n and both m and n are greater than zero. Using m and n, the lengths of three sides of a right triangle (consisting of integer-length sides) are calculated. The pythagorean triplet calculated in the processing step is printed on the user's screen.

Preview


/* Title     - Pythagorean Triple
 * File name - Lab04
 * Programmer- 415 Erich Musick
 * IPO       - Input - User enters m and n - two positive integers, where m > n
                and both m and n are greater than zero
               Processing - Using m and n, the lengths of three sides of a right
                triangle (consisting of integer-length sides) are calculated.
               Output - The pythagorean triplet calculated in the processing
                step is printed on the user's screen.
*/

#include 

using namespace std;

int main() {
        int a;
        int b;
        int c;
        int m;
        int n;

        cout << "Enter the&

Lab05 - Triangle Formulas

Summary

The user inputs the real number lengths of three sides of a triangle. The program calculates the area and perimeter using the values entered by the user and the formulas for perimeter and for area. The lengths of the three sides, the perimeter, and the area are printed on the user's screen.

Preview


/* Title     - Triangle Formulas
 * File name - Lab05
 * Programmer- 415 Erich Musick
 * IPO       - Input - The user inputs the real number lengths of three sides of
                a triangle.
               Processing - The program calculates the area and perimeter using
                the values entered by the user and the formulas for perimeter
                and for area.
               Output - The lengths of the three sides, the perimeter, and the
                area are printed on the user's screen.
*/

//---------------------Start Header Files------------------------------------

//#include "iostream.h"
#include "waittoclose.h"  // Add to project waittoclose.cpp
#include "apstring.h"     // Add to project apstring.cpp
//#include "randgen&.

Lab05b - Triangle Formulas

Summary

The user inputs the real number lengths of three sides of a triangle. The program calculates the area and perimeter using the values entered by the user and the formulas for perimeter and for area. The lengths of the three sides, the perimeter, and the area are printed on the user's screen.

Preview


/* Title     - Triangle Formulas
 * File name - Lab05
 * Programmer- 415 Erich Musick
 * IPO       - Input - The user inputs the real number lengths of three sides of
                a triangle.
               Processing - The program calculates the area and perimeter using
                the values entered by the user and the formulas for perimeter
                and for area.
               Output - The lengths of the three sides, the perimeter, and the
                area are printed on the user's screen.
*/

//---------------------Start Header Files------------------------------------

//#include "iostream.h"
#include "waittoclose.h"  // Add to project waittoclose.cpp
#include "apstring.h"     // Add to project apstring.cpp
//#include "randgen&.

Lab06 - Inches Conversion

Summary

User inputs an integer-length in inches. Program splits the user input into yards, feet, and inches using division and modulo. Program displays calculated yards, feet, and inches.

Preview


/* Title     - Inches Conversion
 * File name - Lab06
 * Programmer- 415 Erich Musick
 * IPO       - Input - User inputs an integer-length in inches
               Processing - Program splits the user input into yards, feet, and
                 inches using division and modulo
               Output - Program displays calculated yards, feet, and inches
*/

//---------------------Start Header Files------------------------------------

//#include "iostream.h"
#include "waittoclose.h"  // Add to project waittoclose.cpp
#include "apstring.h"     // Add to project apstring.cpp
//#include "randgen.h"      // Add to project randgen.cpp
#pragma hdrstop
//#include "apvector.h"     // DO NOT ADD TO PROJECT apvector.cpp
//#include "apmatrix.h"     // DO NOT ADD TO&

Lab07 - Time Clock

Summary

User inputs the time he/she began and finished work. The program calculates the amount of time the user worked. The program displays the amout of time the user worked

Preview


/* Title     - Time Clock
 * File name - Lab07
 * Programmer- 415 Erich Musick
 * IPO       - Input - The user inputs the time he/she began and finished work
               Processing - The program calculates the amount of time the user
                worked
               Output - The program displays the amout of time the user worked
*/

//---------------------Start Header Files------------------------------------

#include "waittoclose.h"    // Add to project waittoclose.cpp
#pragma hdrstop
//#include "iostream.h"
//#include "apstring.h"     // Add to project apstring.cpp
//#include "randgen.h"      // Add to project randgen.cpp
//#include "apvector.h"     // DO NOT ADD TO PROJECT apvector.cpp
//#include "apmatrix.h&

Lab07b - Time Clock

Summary

User inputs the time he/she began and finished work. The program calculates the amount of time the user worked. The program displays the amout of time the user worked

Preview


/* Title     - Time Clock
 * File name - Lab07
 * Programmer- 415 Erich Musick
 * IPO       - Input - The user inputs the time he/she began and finished work
               Processing - The program calculates the amount of time the user
                worked
               Output - The program displays the amout of time the user worked
*/

//---------------------Start Header Files------------------------------------

#include "waittoclose.h"    // Add to project waittoclose.cpp
#pragma hdrstop
//#include "iostream.h"
//#include "apstring.h"     // Add to project apstring.cpp
//#include "randgen.h"      // Add to project randgen.cpp
//#include "apvector.h"     // DO NOT ADD TO PROJECT apvector.cpp
//#include "apmatrix.h&

Lab08 - Apvector and Functions

Summary

This program repeatedly asks for an array of numbers and then displays those numbers as a comma separated list, a tab 10 separated list, a check type value with leading dollar signs to prevent changing the value list and finally an average of the list values is displayed.

Preview


/* Course         - A P   C o m p u t e r   S c i e n c e ( C + + )
 * Instructor     - M r .  B u r z y n s k i
 * Lab name       - A P V E C T O R   A N D   F U N C T I O N S
 * Lab number     - Lab08.cpp
 * Programmer     - 415 Erich Musick
 * Date Assigned  - 09/29/00
 * Date Due       - 10/07/00
 * Date Turned in -
 * Purpose        - This program repeatedly asks for an array of numbers
 *                - and then displays those numbers as a comma&

Lab09 - Three Means

Summary

User inputs two positive integers. Arithmetic, geometric, and harmonic means are calculated and displayed.

Preview


/* Title      - Three Means
 * File name  - Lab09
 * Programmer - 415 Erich Musick
 * IPO        - Input - User inputs two positive integers
                Processing - Arithmetic, geometric, and harmonic means are
                  calculated
                Output - Arithmetic, geometric, and harmonic means are displayed
*/

//---------------------Start Header Files------------------------------------

//#include "iostream.h"
#include "waittoclose.h"  // Add to project waittoclose.cpp
//#include "apstring.h"     // Add to project apstring.cpp
//#include "randgen.h"      // Add to project randgen.cpp
#pragma hdrstop
//#include "apvector.h"     // DO NOT ADD TO PROJECT apvector.cpp
//#include "apmatrix.h"     // DO NOT ADD TO PROJECT apmatrix.cpp

//----------------------End Header Files-------------------------------------

// Arithmetic&

Lab10 - Multiplication

Summary

User inputs the number of rows and columns he/she wants for the table. Table is resized to satisfy user's input, values of table are filled. Table is displayed, with some formatting.

Preview


/* Title     - Multiplication
 * File name - Lab10
 * Programmer- 415 Erich Musick
 * IPO       - Input - User inputs the number of rows and columns he/she wants
 *               for the table.
 *             Processing - Table is resized to satisfy user's input, values of
 *               table are filled.
 *             Output - Table is displayed, with some formatting.
*/

//---------------------Start Header Files------------------------------------

//#include "iostream.h"
#include "waittoclose.h"  // Add to project waittoclose.cpp
//#include "apstring.h"     // Add to project apstring.cpp
//#include "randgen.h"      // Add to project randgen.cpp
#pragma hdrstop
//#include "apvector.h"     // DO NOT ADD TO PROJECT apvector.cpp
#include "apmatrix.h&

Lab11 - Classifying Triangles

Summary

This program inputs sets of three numbers (0 0 0 to quit) and then prints out if it is a triangle and what type scalene, isosceles, or equalateral.

Preview


/* Title  - Classifying Triangles
File name - Lab11.cpp
Programmer- 415 Erich Musick
Assigned  - 10/28/01
Due       - 11/03/01
IPO       - This program inputs sets of three numbers (0 0 0 to quit) and then
            prints out if it is a triangle and what type scalene, isosceles, or
            equalateral.
*/

/*************************** H E A D E R  F I L E S ***************************/
#include "waittoclose.h"

/******************** F U N C T I O N  P R O T O T Y P E S ********************/
void getSides(double &s1, double &s2, double &s3);
bool goAgain(double s1, double s2, double s3);
bool isTriangle(double s1&

Lab12 - Copying CDs

Summary

User inputs the length of two CD's and a tape. The program determines which, if any, CD(s) will fit on the tape. The program displays the values entered by the user and tells the user which CD(s) (if any) fit on the tape.

Preview


/* Title      - Copying CD's
 * File name  - Lab12.cpp
 * Programmer - 415 Erich Musick
 * Assigned   - 11/05/01
 * IPO        - Input -
 *              User inputs the length of two CD's and a tape
 *            - Process -
 *              The program determines which, if any, CD(s) will fit on the tape
 *            - Output -
 *              The program displays the values entered by the user and tells
 *              the user which CD(s) (if any) fit on the tape
*/

/*************************** H E A D E R  F I L E S ***************************/
#include "waittoclose.h"

/******************** F U N C T I O N  P R O&

Lab13 - Adding Big Integers

Summary

This program tests the Add function written for the workbook chapter 7 problem 16 on page 38.

Preview


/* Name       - Adding Big Integers
 * Lab        - Lab13.cpp
 * Programmer - 415 Erich Musick
 * Assigned   - 11/12/01
 * Due        - 11/??/01
 * Purpose    - This program tests the Add function written for the work
 *              book chapter 7 problem 16 on page 38.
 * Input      - Two "big integers" are entered by the user.
 * Process    - The sum of those integers is calculated.
 * Output     - The two numbers and their sum with leading zeros stripped
 *              off and commas used to make the numbers more readable.
 */

#include 
#include 
#include 

using namespace std;

/******     G L O B A L   C O N S T A N T&

Lab14 - Number Guess Game

Summary

User inputs guesses as to the value of the random number generated by the program. Program determines whether guess is correct. Displays proper message, corresponding to the accuracy of the guess (too high, too low, just right...).

Preview


/* Title      - Number Guess Game
 * File name  - Lab14
 * Programmer - 415 Erich Musick
 * IPO        - Input   - User inputs guesses as to the value of the random
 *                        number generated by the program.
 *              Process - Program determines whether guess is correct.
 *              Output  - Displays proper message, corresponding to the accuracy
 *                        of the guess (too high, too low, just right...)    
*/

/*************************** H E A D E R  F I L E S ***************************/
//#include "iostream.h"
#include "waittoclose.h"    // Add to project waittoclose.cpp
#include "apstring.h"       // Add to project apstring.cpp
#include "randgen.h"       // Add to project randgen&.

Lab15 - ln(2)

Summary

User inputs an integer.Approximate value of ln(2) is calculated using: 1-1/2+1/3-1/4+...+(+/-)1/n+... where n is the inputed integer. Approximate and actual values (chopped at 6 decimal places) are displayed.

Preview


/* Title      - ln(2)
 * File name  - Lab15
 * Programmer - 415 Erich Musick
 * IPO        - Input   - User inputs an integer.
 *              Process - Approximate value of ln(2) is calculated using:
 *                                1-1/2+1/3-1/4+...+(+/-)1/n+...
 *                        where n is the inputed integer.
 *              Output  - Approximate and actual values (chopped at 6 decimal
 *                        places) are displayed.
*/

/*************************** H E A D E R  F I L E S ***************************/

#include 
#include 
#include 

using namespace std;

/******************** F U N C T I O N  P R O T O T Y P E S ********************/

/****************************** F U N C T I O N S *****************************/
int&

Lab16 - Calculating Powers

Summary

User inputs the base and exponent of a power. Value of base raised to the power is calculated. Result, and values inputed by user are displayed.

Preview


/* Title      - Calculating Powers
 * File name  - Lab16
 * Programmer - 415 Erich Musick
 * IPO        - Input   - User inputs the base and exponent of a power
 *              Process - Value of base raised to the power is calculated
 *              Output  - Result, and values inputed by user are displayed
*/

/*************************** H E A D E R  F I L E S ***************************/
//#include "iostream.h"
#include "waittoclose.h"    // Add to project waittoclose.cpp
#include "apstring.h"       // Add to project apstring.cpp
//#include "randgen.h"       // Add to project randgen.cpp
#pragma hdrstop
//#include "apvector.h"     // DO NOT ADD TO PROJECT apvector&.

Lab17 - Square Root Function

Summary

User inputs a positive real number. Approximate square root value of user inputed number is calculated, within .01 of the acutal value. Approx square root value and actual value (calculated using the sqrt() function) are displayed.

Preview


/* Title      - Square Root Function
 * File name  - Lab17
 * Programmer - 415 Erich Musick
 * IPO        - Input   - User inputs a positive real number
 *              Process - Approximate square root value of user inputed number
 *                is calculated, within .01 of the acutal value
 *              Output  - Approx square root value and actual value (calculated
 *                using the sqrt() function) are displayed
*/

/*************************** H E A D E R  F I L E S ***************************/
//#include "iostream.h"
#include "waittoclose.h"    // Add to project waittoclose.cpp
#include "apstring.h"       // Add to project apstring.cpp
//#include "randgen.h"       // Add to project randgen.cpp
#pragma&

Lab18 - Credit Card Type

Summary

User inputs a credit card number. The program determines, based on the first few numbers of the credit card, what type of credit card the number identifies. The type of credit card is displayed.

Preview


/* Title      - Credit Card Type
 * File name  - Lab18
 * Programmer - 415 Erich Musick
 * IPO        - Input   - User inputs a credit card number
 *              Process - The program determines, based on the first few numbers
 *                of the credit card, what type of credit card the number
 *                identifies
 *              Output  - The type of credit card is displayed
*/

/*************************** H E A D E R  F I L E S ***************************/
//#include "iostream.h"
#include "waittoclose.h"      // Add to project waittoclose.cpp
//#include "apstring.h"       // Add to project apstring.cpp
//#include "randgen.h"        // Add to project randgen.cpp
#pragma hdrstop&#

Lab19 - Rock, Paper, Scissors

Summary

User selects their weapon - rock, paper, or scissors. Computer selects random weapon, winner is determined. Selected weapons, winner, and win-loss-tie record are displayed. User can continue to play for as long as he/she would like.

Preview


/* Title      - Rock, Paper, Scissors
 * File name  - Lab19
 * Programmer - 415 Erich Musick
 * IPO        - Input   - User selects their weapon - rock, paper, or scissors
 *              Process - Computer selects random weapon, winner is determined
 *              Output  - Selected weapons, winner, and win-loss-tie record are
 *                displayed. User can continue to play for as long as he/she
 *                would like.
*/

/*************************** H E A D E R  F I L E S ***************************/
//#include "iostream.h"
#include "waittoclose.h"      // Add to project waittoclose.cpp
//#include "apstring.h"         // Add to project apstring.cpp
#include "randgen.h"          // Add to project randgen&.

Lab20 - Diamond

Summary

User inputs a max width and symbol. Diamond is displayed with proper spacing.

Preview


/* Title      - Diamond
 * File name  - Lab20
 * Programmer - 415 Erich Musick
 * IPO        - Input   - User inputs a max width and symbol
 *              Process - None.
 *              Output  - Diamond is displayed with proper spacing
*/

/*************************** H E A D E R  F I L E S ***************************/
//#include "iostream.h"
#include "waittoclose.h"      // Add to project waittoclose.cpp
//#include "apstring.h"       // Add to project apstring.cpp
//#include "randgen.h"        // Add to project randgen.cpp
#pragma hdrstop
//#include "apvector.h"       // DO NOT ADD TO PROJECT apvector.cpp
//#include "apmatrix.h"       // DO NOT ADD TO PROJECT apmatrix.cpp

/******************** F U&

Lab21 - Prime Factorization

Summary

User inputs the first and last numbers in a range. Prime factorization for each number is calculated and displayed.

Preview


/* Title      - Prime Factorization
 * File name  - Lab21
 * Programmer - 415 Erich Musick
 * IPO        - Input   - User inputs the first and last numbers in a range
 *              Process - Prime factorization for each number is calculated
 *              Output  - After being calculated, each prime factorization is
 *                displayed
*/

/*************************** H E A D E R  F I L E S ***************************/
//#include "iostream.h"
#include "waittoclose.h"      // Add to project waittoclose.cpp
//#include "apstring.h"       // Add to project apstring.cpp
//#include "randgen.h"        // Add to project randgen.cpp
#pragma hdrstop
#include "apvector.h"         // DO NOT ADD TO PROJECT apvector.cpp&#

Return to Resources