// Line comments start with // and run to the end of the line.
/*
Block comments are enclosed
in between slash-asterisks.
*/
// Includes. These include .h files that declare many
// standard C++ functions which make life easier for us!
//
// iostream.h - contains the declaration of cout, which
// we use for output, and cin, which we use
// for input.
//
// math.h - contains the declaration of sqrt(), which
// is the square root function, and power(),
// which computes a power.
//
// limits.h - contains constants which give the minimum
// and maximum values for various data types.
//
// stdlib.h - contains the declaration of rand(), which
// generates random numbers.
//
#include
#include
#include
#include
// Function prototypes. These declares functions so that
// code may call them. The actual definition of the
// functions happens later.
void report (int);
int compute (int, int);
/*
* If you are using an old compiler
*/
#define bool int
#define false 0
#define true 1
// main(). The main() function is the first function that
// is call when an application runs.
int main (void)
{
// Write some text. cout is the output stream.
// endl is the end of line. The book shows this
// as \n, but I prefer endl because it is a bit
// more descriptive.
cout << "Hello world." << endl;
// Declare and define a variable called length.
int length;
length = 30;
cout << "The length is " << length << endl;
// Declare a variable called width, and get
// the value from cin, which is the input
// stream.
int width;
cout << "Enter the width: ";
cin >> width;
cout << "The width is " << width << endl;
// Declare and define the area. Notice that
// that the declaration and definition can
// be combined into one statement.
int area = width * length;
cout << "The area is " << area << endl;
// Now we will call a standard function to
// compute the square root and to compute
// the 4th power. Notice how the result
// of a function can be used within a calculation
// or written directly to the output stream.
// This is true of any C++ expression.
//
// Notice the double refers to a double precision
// number (we will learn what this means later)
// and that we cannot name a variable "4thPower"
// because identifiers must begin with a letter.
//
// Notice also how we can split lines in many ways.
double d = 3.0;
double fourthPower = pow (d, 4);
cout << d << " to the 4th power is "
<< fourthPower << endl;
cout << "The square root of " << d;
cout << " is " << sqrt (d) << endl;
// Now we are going to call a function that we have
// written ourselves, report(). It is defined below.
// We can pass it any integer value, whether it be
// a constant, variable, or expression.
//
// rand() is a function that takes no parameters
// and returns a random number.
report (25);
report (length);
report (width);
report (5 + 6);
report (rand ());
// Here are some calls to another function that we
// have written ourselves, compute(). This time it
// takes 2 parameters, both integers. It also returns
// an integer, which we can print out in many ways.
cout << "The first computation is " << compute (25, length) << endl;
int c2 = compute (width, -1);
cout << "The second computation is " << c2 << endl;
cout << "Now we will report the third computation:" << endl;
report (compute (5 + 6, rand ()));
// Now we will look at different data types.
// Write the size, minimum value, and maximum value
// for variables of each type. Use the sizeof
// operator to get the size. There are constants
// defined in limits.h which give the minimum and
// maximum values.
int i1 = INT_MIN;
int i2 = INT_MAX;
cout << "int: size = " << sizeof (int) << " bytes." << endl;
cout << "int: minimum = " << i1 << ", maximum = " << i2 << endl;
short s = 1;
cout << "short: size = " << sizeof s << " bytes." << endl;
cout << "short: minimum = " << SHRT_MIN
<< ", maximum = " << SHRT_MAX << endl;
long l = -99;
cout << "long: size = " << sizeof l << " bytes." << endl;
cout << "long: minimum = " << LONG_MIN
<< ", maximum = " << LONG_MAX << endl;
// Now the unsigned types. Notice that the sizes are
// the same as the signed types, but the maximum values
// are different. There are no constants defined for the
// minimum values of unsigned types, since they are 0 by
// definition.
unsigned int ui = 12;
cout << "unsigned int: size = " << sizeof ui << " bytes." << endl;
cout << "unsigned int: maximum = " << UINT_MAX << endl;
unsigned short us = 124;
cout << "unsigned short: size = " << sizeof (unsigned short) << " bytes." << endl;
cout << "unsigned short: maximum = " << USHRT_MAX << endl;
unsigned long ul = 56746L;
cout << "unsigned long: size = " << sizeof (unsigned long) << " bytes." << endl;
cout << "unsigned long: maximum = " << ULONG_MAX << endl;
// The char type is used to declare variables which will
// hold a single character. It is easy (but rarely useful)
// to convert a character to an int, which represents
// the character's ASCII value.
char ch = 'C';
int i = ch;
cout << "The ASCII value for " << ch << " is " << i << "." << endl;
// The bool type holds a boolean value, which is either
// true or false.
bool b = true;
cout << "b = " << b << endl;
b = false;
cout << "Now b = " << b << endl;
// There also types to handle floating point numbers.
// These can be represented as decimal numbers or using
// scientific notation.
float f1 = 43.6543f;
float f2 = 4.53e2f;
cout << "f1 = " << f1 << ", f2 = " << f2 << endl;
double d1 = 43.6543456345;
double d2 = 4.5334535e-14;
cout << "d1 = " << d1 << ", d2 = " << d2 << endl;
long double ld1 = 43.6543456345378437934758934534;
long double ld2 = 4.533453516723672364723674e104;
cout << "ld1 = " << ld1 << ", ld2 = " << ld2 << endl;
// Now we will take a look at the arithmetic operations.
int xx = 123;
int yy = 456;
cout << "xx + yy = " << xx + yy << endl;
cout << "xx - yy = " << xx - yy << endl;
cout << "xx * yy = " << xx * yy << endl;
cout << "xx / yy = " << xx / yy << endl;
cout << "xx % yy = " << xx % yy << endl;
// *,/,% have precedence over + and -, except
// when parentheses are used.
cout << "3 + 4 * 5 = " << 3 + 4 * 5 << endl;
cout << "(3 + 4) * 5 = " << (3 + 4) * 5 << endl;
// main() is declared to return an integer. By convention,
// we return 0 to show that no errors occurred. If an error
// had occurred, we would return a "return code".
return 0;
}
// This function reports a value to the output stream.
// It takes one integer parameter.
void report (int value)
{
cout << "Reporting a value of " << value << endl;
}
// This functions computes the formula x*(y-1), where
// x and y are integer parameters. It then returns
// the result.
int compute (int x, int y)
{
int result;
result = x * (y - 1);
return result;
// This whole thing could have been abbreviated:
// return x * (y - 1);
}