Sample Programs:

For those who have difficulty to start writing C++ programs, here are 9 sample programs, which progressively introduce various elementary concepts in C++. Go through these sample codes, type them up, compile and run them on the Visual C++ development environment. These exercises will give you enough confidence to proceed with Project 1

Example 1

 

// This is a comment line

// My first C++ program

#include <iostream.h> // Compiler directive

int main() // Function header

{

cout << "Hello World!!" << endl;

cout << "This is my first C++ program output" << endl;

return 0;

}

 

Example 2

 

// This program deals with variable declarations

#include <iostream.h>

int main()

{

int count,employee_id = 1024; // integer variables

double gross_pay = 2000.0, net_pay; // real variables

char employee_code ='P', union_code; // char variables

const float tax_rate =0.25; // named constant

cout << "The employee ID is: " << employee_id << endl;

cout << "The gross pay is: " << gross_pay << endl;

cout << "The employee code is :" << employee_code << endl;

return 0;

}

 

 

Example 3

 

// This program demonstrates the use of the assignment

// operator

#include <iostream.h>

int main()

{

int num1, num2 = 6;

double weight;

char letter;

num1 = 5; // Assigns 5 to num1

weight = 25.7; // Assigns 25.7 to weight

letter = 'A'; // Assigns A to letter

num1 = num1 + 5; // New value of num1 is 10

num2 = num1 + num2; // New value of num2 is 16

weight = 2 * weight + 3.0; // New value of weight

// is 54.4

letter = letter + 1; // New value of letter is B

cout << "The package number is: " << num2 << endl;

cout << "The package type is: " << letter << endl;

cout << "The package weight is: " << weight << endl;

return 0;

}

 

Example 4

 

// Finding the values of the standard named constants

#include <stdlib.h>

#include <float.h>

#include <limits.h>

#include <iostream.h>

int main()

{

cout << "Success code = " << EXIT_SUCCESS << endl;

cout << "Failure code = " << EXIT_FAILURE << endl;

cout << "Smallest integer = " << INT_MIN << endl;

cout << "Largest integer = " << INT_MAX << endl;

cout << "Smallest long integer = " << LONG_MIN << endl;

cout << "Largest long integer = " << LONG_MAX << endl;

cout << "Smallest float type number = " << FLT_MIN << endl;

cout << "Largest float type number = " << FLT_MAX << endl;

cout << "Smallest double type number = " << DBL_MIN << endl;

cout << "Largest double type number = " << DBL_MAX << endl;

return 0;

}

Example 5

 

// Increment and decrement operators

// Verify the results by hand calculation

#include <iostream.h>

int main()

{

int i=3, k=5, j=8, m=2;

 

cout <<" m i j k"<<endl;

cout <<" ----------------"<<endl;

m = ++ i * k --;

cout << " " << m << " "<< i << " "

<< j <<" "<< k << endl;

m = j ++ * k --;

cout << " " << m << " "<< i << " "

<< j <<" "<< k << endl;

m = j ++ * ++ k;

cout << " " << m << " "<< i << " "

<< j <<" "<< k << endl;

m = -- i * -- k;

cout << " " << m << " "<< i << " "

<< j <<" "<< k << endl;

m = -- j * k ++;

cout << " " << m << " "<< i << " "

<< j <<" "<< k << endl;

return 0;

}

Example 6

 

// This program deals with arithmetic expressions

// Check the results by hand calculation

#include <iostream.h>

int main()

{

int i = 5, j = 3, m;

double x = 10.0, y = 7.0;

m = i/j; // integer division

cout << m << endl;

m = i%j; // remainder operation

cout << m << endl;

m = i%j*4 + y/3;

cout << m << endl;

m = i*x/2 + i/3.0;

cout << m << endl;

x = i/j + 2*y;

cout << x << endl;

y = y*x + (j+5)%3;

cout << y << endl;

return 0;

}

Example 7

 

// Input from keyboard

// Use of cin statement

// This program calculates the average of three numbers

#include <iostream.h>

int main()

{

double number1, number2, number3;

double average;

cout << "Enter any three numbers separated by space: ";

cin >> number1 >> number2 >> number3;

average = (number1 + number2 + number3)/3;

cout << "The average is :" << average << endl;

return 0;

}

Example 8

 

// This program shows how to invoke a library function

// Check each result by hand calculation

#include <iostream.h>

#include <math.h>

int main()

{

double x = 25.0;

double result;

result = sqrt(x); // invokes square root function

cout <<"The square root of "<< x << " = " << result <<endl;

result = sqrt(2*x + 5);

cout << "The result of the first expression is "

<< result << endl;

result = 20.0 + sqrt(2*x + sqrt(x));

cout <<"The result of the second expression is "

<< result << endl;

return 0;

}

Example 9

 

// This program demonstrates file I/Os and I/O manipulators

// This program takes data from an input file "salary.dat"

// containing the employee number and the annual salary of each

// employee in an organization and compute the biweekly gross pay,

// the federal tax, the state tax, the social security tax,

// the medicare tax, and the net_pay for each employee. The results

// are saved in a file "pay.dat".

 

#include <iomanip.h> // For manipulating widths and precisions

#include <fstream.h> // For file I/O

// Hypothetical tax rates, defined as constants

const float FED_TAX_RATE = 0.15;

const float STATE_TAX_RATE = 0.05;

const float SS_TAX_RATE = 0.08;

const float MED_TAX_RATE = 0.01;

int main()

{

ofstream pay_file; // declare output file stream pay_file

ifstream salary_file; // declare input file stream salary_file

int employee_number;

double annual_salary;

double gross_pay, fed_tax, state_tax, ss_tax, med_tax, net_pay;

pay_file.setf(ios::fixed, ios::floatfield); // Setup floating point

pay_file.setf(ios::showpoint); // output format

pay_file.open("a:pay.dat"); // open files "a:pay.dat"

salary_file.open("a:salary.dat"); // and "a:salary.dat" and

// link them with the

// coresponding file streams

// Read the first set of data (*** prime read operation)

salary_file >> employee_number >> annual_salary;

// process all statements enclosed in braces (the compound statement)

// again and again while the input file stream salary_file has

// some data to be extracted. That is, process until to the end of

// file "a:salary.dat"

while (salary_file)

{

gross_pay = annual_salary / 24;

fed_tax = FED_TAX_RATE * gross_pay;

state_tax = STATE_TAX_RATE * gross_pay;

ss_tax = SS_TAX_RATE * gross_pay;

med_tax = MED_TAX_RATE * gross_pay;

net_pay = gross_pay -(fed_tax + state_tax + ss_tax + med_tax);

// output results with 10 positions

// output floating point results with two decimals

pay_file << setw(10) << employee_number

<< setw(10) << setprecision(2) << gross_pay

<< setw(10) << setprecision(2) << fed_tax

<< setw(10) << setprecision(2) << state_tax

<< setw(10) << setprecision(2) << ss_tax

<< setw(10) << setprecision(2) << med_tax

<< setw(10) << setprecision(2) << net_pay << endl;

// read next data set

salary_file >> employee_number >> annual_salary;

}

salary_file.close();

pay_file.close();

return 0;

} 


With permission from Dr. Dulal Kar

Email:
kjayan@vt.edu