Week 3: Data Structure


Basic Variable in C++

Use a "good" name

Tell where the information is stored

Tell what value is kept there

Tell what kind of information is stored


int transactionCount;
transactionCount = 5;



Only alphabetic characters, digits and _ are allowed

The first character is a name cannot be a digit

Uppercase and lowercase are different

Cannot use a C++ keyword (e.g. return, main, int, ...)

No length limit


int sum;

int sum_of_records;

int sumOfRecords;


Short, int, long Integer types


A short integer is at least 2 bytes

A int integer is at least as big as short

A long integer is at least 4 bytes and at least as big as int



Unsigned Type


Unsigned variety cannot hold negative values


unsigned short change;

unsigned int rovert;

All the number values in C++ are dependent on computer systems

For portability, use long for big integers



The char type: Characters and Small Integers


#include <iostream.h>
int main(void)
	char c = 'M';		// assign ASCII code for M to c
	int i = c;		// store same code in an int
	cout << "The ASCII code for " << c << " is " << i << "\n";
	cout << "Add one to the character code:\n";
	c = c + 1;          
	i = c;
	cout << "The ASCII code for " << c << " is " << i << '\n';

	// using the cout.put() member function to display a char
	cout << "Displaying char c using cout.put(c): ";

	// using cout.put() to display a char constant

	cout << "\nDone\n";
	return 0;
// The ASCII code for M is 77
// Add one to the character code:
// The ASCII code for N is 78
// Displaying char c using cout.put(c): N!
// Done



The New bool Type


bool done = true;

if (done)



The const Qualifier


const int Months = 12; // value set once, cannot be changed later, works like a constant


Floating-Point Numbers


We have float and double


float a = 2.34E+22;

Represent a greater range of values (than integer)

Might be slower in computation

Might lose precision (consider 0.0000000000000000001 - 0.00000000000000000000000095)





A data from that can hold several values of all of one type

An array declaration should indicate three things

The type of value to be stored in each element

The name of the array

The number of elements in the array


int months[12]; // Create an array of 12 int

int yams[3];   // creates array with three elements
yams[0] = 7;   // assign value to first element
yams[1] = 8;
yams[2] = 6;
cout << "Total yams = ";
cout << yams[0] + yams[1] + yams[2] << "\n";




A string is a series of characters stored in consecutive bytes of memory. All string ends with \0

char name[5]= {'s', 'i', 'n', 'n', 's'}; 	// not a string
char name[5]= {'s', 'i', 'n', 'n', '\0'};	// a string
// More ways of using it
char bird[10] = "Mr. Chunk";	// the \0 is implied
char fish[] = "HelloFish";	// The C++ compile will internally know what the length is


More on String


#include <iostream.h>
int main(void)
	const int ArSize = 20;
	char name[ArSize];
	char dessert[ArSize];

	cout << "Enter your name:\n";
	cin.get(name, ArSize);       // reads to newline
	cout << "Enter your favorite dessert:\n";
	cin.get(dessert, ArSize);
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << ".\n";
	return 0;


// Enter your name:
// Richard Sinn
// Enter your favorite dessert:
// I have some delicious for you, Richard Sinn



Structures (The traditional way)


Array can only store elements of one type

Structure can store multiple of them


// structur.cpp -- a simple structure
#include <iostream.h>
struct inflatable   // structure template
	char name[20];
	float volume;
	double price;

int main(void)
	inflatable guest =
		"Glorious Gloria", // name value
		1.88,        // volume value
		29.99        // price value
	};   // guest is a structure variable of type inflatable
		  // It's initialized to the indicated values
	inflatable pal =
		"Audacious Arthur",
	};  // pal is a second variable of type inflatable
// NOTE: some implementations require using 
// static inflatable guest =

	cout << "Expand your guest list with " << guest.name;
	cout << " and " << pal.name << "!\n";
     // pal.name is the name member of the pal variable
	cout << "You can have both for $";
	cout << guest.price + pal.price << "!\n";
	return 0;


Demo and Lab