Introduction to Programming and C++

Contents Previous Topic Next Topic

All but the most trivial programs store and manipulate information on a computer. Information is often referred to as data while a single piece of information is referred to as a datum (the singular of data). The course ECE 250 Data Structures and Algorithms is an entire course meant to teach you how to store information on a computer (using data structures) and how to manipulate it (using algorithms).

0s and 1s

In reality, a computer can only store bits, each being either 0 or 1. If all you could do is set various bits to either 0 or 1, this would be turn programming into a nightmare. (In fact, this is how original programs were written.) All modern programming languages have a number of built-in data types which are understood by the compiler (meaning, the compiler can store, manipulate, and display them).

In C++, there are a number of built-in data types available. Table 1 lists a few of the more useful ones.

Data TypeDescription
intintegers from -2147483648 to 2147483647
doublefloating-point (real) numbers such as 3.141592653589793
boola Boolean value: either true or false
chara single character (any character found on a keyboard plus a few others)

The compiler is aware of these types, and is able to perform operations with them without you having to do much extra work. Each of these data types is described it its own section below.

The int Built-In Data Type

The int built-in data type uses1 32 bits of memory (4 bytes) to store a signed integer in the range −2147483648 to 2147483647. It uses a system called 2s complement to store negative integers — you will learn about 2s complement in ECE 222. Printing objects such as cout print an int using base 10. If you add 1 to the largest integer, it wraps around to the smallest integer.

Some of the properties of the data structure int are shown in Program 1.

Program 1. The int built-in data type.

#include <iostream>

using namespace std;

int main() {
	cout << 235 << endl << endl;

	// we can use addition (+), subtraction (-), 
	// multiplication (*) and division (/)
	cout << "   235 + 1 = " <<      235 + 1   << endl;
	cout << "   235 - 1 = " <<      235 - 1   << endl;
	cout << "      -235 = " <<         -235   << endl;
	cout << "235 * 5932 = " <<   235 * 5932   << endl << endl;

	// division returns the integer part
	// and discards any remainder
	cout << "       3/3 = " <<          3/3   << endl;
	cout << "       4/3 = " <<          4/3   << endl;
	cout << "       5/3 = " <<          5/3   << endl;
	cout << "       6/3 = " <<          6/3   << endl << endl;

	// the largest integer
	cout << "The largest integer:          " << 2147483647 << endl;

	// adding 1 to the largest integer wraps to
	// the largest negative integer
	cout << "The largest negative integer: " << 2147483647 + 1 << endl;

	return 0;
}

The double Built-In Data Type

The double built-in data type stores approximations of real numbers using the double-precision floating-point representation documented in IEEE 754. You will/have learned about this in ECE 104 or ECE 204. The representation is close but not exact, and therefore some errors in your computations. Like int, a double can only store real numbers on a finite range and can only store these using a finite amount of precision.

Two comments:

  1. There are two ways to represent doubles: one using a conventional decimal representation, e.g., 3.14, another using scientific notation, e.g., 3.86e15 represents 3.83 × 1015 and 7.09e-24 represents 7.09 × 10-24. You can use e or E.
  2. If you intend to store amounts of dollars and cents in a computer, store it as an integer number of cents, e.g., store $1.02 as the integer 102 instead of the double 1.02.

Some of the properties of the data structure double are shown in Program 2.

Program 2. The double built-in data type.

#include <iostream>

using namespace std;

int main() {
	cout << 3.141592653589793 << endl << endl;

	// we can use addition (+), subtraction (-), 
	// multiplication (*) and division (/)
	cout << "   32.25 + 1.2 = " <<     32.25 + 1.2   << endl;
	cout << "   32.25 - 1.2 = " <<     32.25 - 1.2   << endl;
	cout << "        -32.25 = " <<          -32.25   << endl;
	cout << "32.25 * 5.2352 = " <<  32.25 * 5.2352   << endl;
	cout << "32.25 / 5.2352 = " <<  32.25 / 5.2352   << endl << endl;

	// the largest doubles
	cout << "A large double:        " << 1.235e201 << endl;
	cout << "The largest double:    " << 1.79769313486231570e+308 << endl;
	cout << "An even larger double: " << 1e1000 << endl;

	return 0;
}

The bool Built-In Data Type

The bool built-in data type stores one of two values true or false. Unfortunately, it uses 8 bits (or one byte) to store this value, so it can be rather expensive.

A value of true is printed as 1 while a value of false is printed as 0. There are three operators which work on boolean values, not (!), and (&&) and or (||). These are demonstrated in Program 3.

Program 3. The bool built-in data type.

#include <iostream>

using namespace std;

int main() {
	cout << "true =  " <<  true << endl;
	cout << "false = " << false << endl << endl;

	// logical 'not' of a Boolean value
	cout << "not true =  " <<  !true << endl;
	cout << "not false = " << !false << endl << endl;

	// logical 'and' of two Boolean values
	cout << " true and true  =  " << (  true && true  ) << endl;
	cout << " true and false =  " << (  true && false ) << endl;
	cout << "false and false =  " << ( false && false ) << endl;
	cout << "false and true  =  " << ( false && true  ) << endl << endl;

	// logical 'or' of two Boolean values
	cout << " true or true  =   " << (  true || true  ) << endl;
	cout << " true or false =   " << (  true || false ) << endl;
	cout << "false or false =   " << ( false || false ) << endl;
	cout << "false or true  =   " << ( false || true  ) << endl;

	return 0;
}

The char Built-In Data Type

The char built-in data type occupies 8 bits or 1 byte. This means that it can store 28 = 256 different values from 000000002 to 111111112. These 256 are each interpreted by the compiler and various functions to represent different characters, including the letters, both upper case and lower case, numbers, other various symbols found on the keyboard, and a few special characters such as tab, and character to represent the end-of-line, etc. To represent a literal character, you must put that symbol between single quotes, e.g., 'a'. Because not all symbols can be represented by a single character, there are a few special pairs of characters which may be used to represent a single character, e.g., tab ('\t'), new line ('\n'), and backslash ('\\'). Program 4 demonstrates how characters may be used.

Program 4. The bool built-in data type.

#include <iostream>

using namespace std;

int main() {
	cout << 'a' << 'b' << 'c' << endl;
	cout << 'A' << 'B' << 'C' << endl;
	cout << '1' << '2' << '3' << endl;
	// the pound sign, a tab, a backslash, a new line, and a *
	cout << '#' << '\t' << '\\' << '\n' << '*' << endl;

	return 0;
}

Two observations about characters:

  1. A character represents a single symbol. Strings are a sequence of characters surrounded by double-quotes (") which make life a little easier than may be suggested in Program 4. Note that a string "a" is different from the character 'a'.
  2. Today, with all the languages in the world, 256 different symbols is insufficient to represent all possible characters in all possible languages. Programming languages like Java and C# use a 16 bit (or two byte) character which can store 216 = 65536 different values. This type of encoding is called unicode.

Questions

1. Write a C++ program which prints the integer 1, the double 1.0, the character '1', the string "1", and the Boolean value true, all on separate lines.


Contents Previous Topic Top Next Topic