C++ Course at ISSC
Exercises
// quicky1.cpp
// give the output
//
// What to learn:
// 1) Using the ASCII table
// 2) Type converion: char -> int, int -> char
// 3) Type conversion operator: (type)
// 4) Integer division
#include < iostream.h >
int main()
{
int i = 'x' - 'X';
cout << "i = " << i << endl; // =>
int j = 'c'/' ';
cout << "j = " << j << endl; // =>
char c1 = 73;
char c2 = 32;
char c3 = (char) 3;
char c4 = 32;
char c5 = 67;
char c6 = 43;
char c7 = 43;
cout << c1 << c2 << c3 << c4 << c5 << c6 << c7;
// =>
cout << endl;
int s = 7/8;
cout << "s = " << s << endl; // =>
double t = 7/8;
cout << "t = " << t << endl; // =>
int y = 5/4 + ' ' + '0';
cout << "y = " << y << endl; // =>
int z = '0' + '\0' + 0 + 'O';
cout << "z = " << z << endl; // =>
return 0;
}
// quicky2.cpp
// give the output
//
// What to learn:
// 1) Pointers and Addresses
// 2) References
//
// for the addresses tell us whether they are the same
#include < iostream.h >
int main()
{
float a = 2.1;
float* pa = NULL;
pa = &a;
cout << "pa = " << pa << endl; // =>
cout << "&a = " << &a << endl; // =>
float& b = a;
cout << "b = " << b << endl; // =>
float* c = NULL;
c = &b;
*c = 17.8;
cout << "c = " << c << endl; // =>
cout << "a = " << a << endl; // =>
unsigned long x = 921;
unsigned long* px = NULL;
px = &x;
cout << "px = " << px << endl; // =>
cout << "&x = " << &x << endl; // =>
unsigned long y = x;
cout << "y = " << y << endl; // =>
unsigned long* z = NULL;
z = &y;
*z = 4324567;
cout << "z = " << z << endl; // =>
cout << "x = " << x << endl; // =>
return 0;
}
// quicky3.cpp
// give the output
//
// What to learn:
// 1) Precedence table
//
// ++ is the increment operator, it increments by 1
// Attention: There is a
// prefix increment
// and a
// postfix increment
// They are at different places at the precedence table
//
// % is the remainder operator
// for example 23%7 = 2 since 23/7 = 3 remainder 2
#include < iostream.h >
int main()
{
int a = 8;
a++;
++a;
cout << "a = " << a << endl; // =>
int b = 8, c = 7;
int r1 = c*b++;
cout << "r1 = " << r1 << endl; // =>
cout << "b = " << b << endl; // =>
int d = 8, e = 7;
int r2 = d*(e++);
cout << "r2 = " << r2 << endl; // =>
cout << "e = " << e << endl; // =>
int f = 8, g = 7;
int r3 = f*++g;
cout << "r3 = " << r3 << endl; // =>
int n = 31;
int m = 5;
int s1 = n/m;
cout << "s1 = " << s1 << endl; // =>
int s2 = n%m;
cout << "s2 = " << s2 << endl; // =>
int p = 5;
int s3 = n/m%p;
cout << "s3 = " << s3 << endl; // =>
int s4 = n%m/p;
cout << "s4 = " << s4 << endl; // =>
return 0;
}
// quicky4.cpp
// give the output
//
// What to learn:
// 1) Out of scope, block begin block end
//
// -- is the decrement operator it decrements by 1
// If a decrement is placed in front of a variable
// then it is called predecrement
// if it is placed behind the variable it is
// called postdecrement
//
// The curly bracket { indicates block begin
// The curly bracket } indicates block end
//
// The scope of an identifier is the portion
// of the program in which the identifier can be
// seen (referenced). When we declare a local variable
// in a block, it can be referenced only in that
// block or in blocks nested within that block.
#include < iostream.h >
int main()
{
int a = -15;
a--;
cout << "a = " << a << endl; // =>
{
int b = 7;
b = 2*a*b;
}
int result = a + b;
cout << "result = " << result << endl; // =>
return 0;
}
// quicky5.cpp
// convert the character ch = 'Z' into a string "Z"
//
// What to learn:
// 1) Null character '\0'
// 2) Memory allocation using new
// 3) Freeing memory using delete
#include < iostream.h >
int main()
{
char ch = 'Z';
char *string = NULL;
string = new char[2]; // here I allocated memory for you, why 2 ?
/* insert here the string construction */
cout << string << endl;
/* apply here the delete operator */
return 0;
}
// quicky6.cpp
//
// What to learn:
// 1) Pass by Value
// 2) Pass by Reference
// 3) Forward Declaration
#include < iostream.h >
void swap1(char,char); // prototype
void swap2(char&,char&); // prototype
void swap3(char*,char*); // prototype
int main()
{
char c1 = 'X';
char c2 = 'Y';
swap1(c1,c2);
cout << "c1 = " << c1 << endl; // =>
cout << "c2 = " << c2 << endl; // =>
char d1 = '2';
char d2 = '3';
swap2(d1,d2);
cout << "d1 = " << d1 << endl; // =>
cout << "d2 = " << d2 << endl; // =>
char e1 = '+';
char e2 = '-';
swap3(&e1,&e2);
cout << "e1 = " << e1 << endl; // =>
cout << "e2 = " << e2 << endl; // =>
return 0;
}
void swap1(char x1,char x2)
{
char temp;
temp = x1; x1 = x2; x2 =temp;
}
void swap2(char& y1,char& y2)
{
char temp;
temp = y1; y1 = y2; y2 = temp;
}
void swap3(char* x1,char* x2)
{
char temp;
temp = *x1; *x1 = *x2; *x2 = temp;
}
// quicky7.cpp
//
// What to learn:
// 1) Initialization
//
// the program compiles and runs
// without any problem
// still there is someting wrong
// the task should be to find the product
// of all the elements of an array
// thus the output should be: 2 x (-7) x 5 x 14 = -980
// Explain and describe why we do not find this result
// except when we are very lucky
#include < iostream.h >
long product(long* array,long n)
{
long result;
int j;
for(j=0;j < n;j++)
{
result *= array[j]; // this is the shortcut for
} // result = result*array[j];
return result;
}
int main()
{
long size = 4;
long* a = NULL;
a = new long[size];
a[0] = 2; a[1] = -7; a[2] = 5; a[3] = 14;
long result = product(a,size);
cout << "product of the array elements is: " << result;
delete [] a;
return 0;
}
// quicky8.cpp
//
// What to learn:
// 1) Memory allocation
// 2) Using strcpy (string copy)
// 3) Type conversion
// 4) Array of pointers
#include < iostream.h >
#include < string.h > // why do we need this header file ?
int main()
{
char* s = NULL;
s = new char[4]; // why 4 ?
strcpy(s,"ABC");
int* a = NULL;
a = new int[3];
for(int j=0;j<3;j++)
{
a[j] = s[j]; // => can this line be written also as: *(a+j) = *(s+j);
}
int result = 0;
for(j=0; j<3; j++)
{
result += a[j];
}
cout << result << endl; // =>
delete []a;
delete s;
int* x[2] = { NULL } ;
int u = 3, v = -1;
x[0] = &u;
x[1] = &v;
int y;
y = *x[0] - *x[1];
cout << y << endl; // =>
return 0;
}
// quicky9.cpp
// give the output
//
// What to learn:
// 1) passing a function to another function
// 2) mathematical functions pow, sin, cos, exp
//
#include < iostream.h >
#include < math.h > // why do we need this header file ?
double func1(double x)
{
return x*x;
}
double func2(double x)
{
return pow(x,3.0);
}
double func3(double x)
{
return (sin(x) + cos(x) + exp(x));
}
// f a pointer to the function
double summation(double (*f)(double),double* array,int size)
{
int i;
double sum = 0.0;
for(i=0; i < size; i++)
sum += f(array[i]);
return sum;
}
int main()
{
int size = 3;
double* array = NULL;
array = new double[size];
array[0] = 1.0; array[1] = 2.0; array[2] = 3.0;
double sum1 = summation(func1,array,size);
cout << "sum1 = " << sum1 << endl; // =>
double sum2 = summation(func2,array,size);
cout << "sum2 = " << sum2 << endl; // =>
double sum3 = summation(func3,array,size);
cout << "sum3 = " << sum3 << endl; // =>
return 0;
}
// quicky10.cpp
//
// What do learn:
// Bitwise representation of integer numbers.
//
// The operator & is the bitwise AND operator.
// The operator | is the bitwise inclusive OR operator.
// The operator ^ is the bitwise exclusive OR operator.
// The operator << is the left shift bitwise operator.
// The operator >> is the right shift bitwise operator.
#include < iostream.h >
void main()
{
unsigned u = 0xFFFF;
cout << "u = " << u << endl; // =>
int x = 9;
int y = 13;
int r1 = x & y;
cout << "r1 = " << r1 << endl; // =>
int r2 = x | y;
cout << "r2 = " << r2 << endl; // =>
int r3 = x ^ y;
cout << "r3 = " << r3 << endl; // =>
int r4 = 3 << 4;
cout << "r4 = " << r4 << endl; // =>
int r5 = 15 >> 2;
cout << "r5 = " << r5 << endl; // =>
}
// bitwise AND &
// 1 1 => 1
// 1 0 => 0
// 0 1 => 0
// 0 0 => 0
// bitwise inclusive OR |
// 1 1 => 1
// 1 0 => 1
// 0 1 => 1
// 0 0 => 0
// bitwise exclusive XOR ^
// 1 1 => 0
// 1 0 => 1
// 0 1 => 1
// 0 0 => 0
// quicky11.cpp
//
// What to learn:
// arrays of strings
//
// What is the input and output of the function validMonth()
// in the following program ? What is the output of the program ?
// Why is the header file string.h necessary ?
// Read the comment at the end of the program.
#include < iostream.h >
#include < string.h >
char* validMonth(int month)
{
char *months[12] = {
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
if(month >= 1 && month <= 12) return months[month-1];
else return "value not allowed";
}
int main()
{
char str1[20];
strcpy(str1,validMonth(6));
cout << "str1 = " << str1 << endl; // =>
char str2[20];
strcpy(str2,validMonth(15));
cout << "str2 = " << str2 << endl; // =>
return 0;
}
// Remember again that the line:
// strcpy(str1,validMonth(6));
// cannot be replaced by:
// str1 = validMonth(6); // a typical beginner error
// This would lead to the error message:
// Lvalue required
// quicky12.cpp
//
// What to learn:
// 1) Pointers and Addresses
// 2) Dereference Operator
//
// What is the output ?
#include < iostream.h >
#include < string.h >
int main()
{
cout << endl << endl;
char s1[5] = { 'l', 'i', 's', 't', '\0' };
char s2[5] = "list";
cout << s1 << endl; // =>
cout << s2 << endl; // =>
cout << *s1 << endl; // =>
cout << *s2 << endl; // =>
cout << &s1 << endl; // =>
cout << &s2 << endl; // =>
cout << "(void*) s1 = " << (void*) s1 << endl; // =>
cout << "(void*) s2 = " << (void*) s2 << endl; // =>
cout << "&(s1[1]) " << &(s1[1]) << endl; // =>
cout << "&(s2[1]) " << &(s2[1]) << endl; // =>
if(s1 == s2) cout << "TRUE" << endl;
else cout << "FALSE" << endl; // =>
if(*s1 == *s2) cout << "TRUE" << endl;
else cout << "FALSE" << endl; // =>
if(&s1 == &s2) cout << "TRUE" << endl;
else cout << "FALSE" << endl; // =>
int r = strcmp(s1,s2);
if(r == 0) cout << "Strings are equal" << endl;
else cout << "Strings are different" << endl; // =>
char* s3 = NULL;
s3 = new char[5]; // Why 5 ? =>
strcpy(s3,"list");
if(s1 == s3) cout << "TRUE" << endl;
else cout << "FALSE" << endl; // =>
if(strcmp(s1,s3) == 0) cout << "TRUE" << endl;
else cout << "FALSE" << endl; // =>
cout << "&s3 = " << &s3 << endl; // =>
cout << "(void*) s3 = " << (void*) s3 << endl; // =>
delete [] s3;
return 0;
}
// quicky13.cpp
//
// What do learn:
// 1) Pass by value, pass by reference
// 2) for loop
// 3) integer division
// Write a C++ program which calculates the sum
//
// 1/1 + 1/2 + 1/3 + 1/4 + ... + 1/N
//
// where N is a positive integer.
// The program must contain two functions
// a function
//
// void sum1(double& sum,unsigned N)
//
// and a function
//
// double sum2(unsigned N)
//
// which both calculate the sum.
// The unsigned integer number is provided in main.
#include < iostream.h >
void sum1(double& sum,unsigned N)
{
}
double sum2(unsigned N)
{
}
int main()
{
return 0;
}
// quicky14.cpp
//
// What to learn:
// Pass by reference, pass by value
//
// Describe what the program does.
// Give the output.
// The program compiles and runs,
// but half way through running it gives
// the error message: access violation.
// Explain this error message and fix it.
#include < iostream.h >
#include < fstream.h >
#include < stdlib.h >
void readFile(double* array,int& length)
{
ifstream inFile("input.dat");
inFile >> length;
if(length <= 0)
exit(0);
cout << "length of array = " << length << endl;
array = new double[length];
int n;
for(n=0; n < length; n++)
{
inFile >> array[n]; cout << array[n] << " ";
}
cout << endl;
}
double arithMean(double* array,const int length)
{
double sum = 0.0;
for(int n=0; n < length; n++)
{
sum += array[n];
}
return sum/length;
}
int main()
{
double* array = NULL;
int length = 0;
readFile(array,length);
cout << "arithmetic mean = " << arithMean(array,length);
delete [] array;
}
// The input file with the name
// input.dat
// contains the data
// 5 1.1 1.3 1.5 2.3 0.7
// quicky15.cpp
// What is the following program doing ?
// Does it compile and run ?
// If not, why ?
// If so, what is the output ?
#include < iostream.h >
int rowsums(int* array,int length)
{
int sum = 0;
for(int i=0; i < length; i++)
{
sum += array[i];
}
return sum;
}
int main()
{
int rows = 3; int columns = 2;
int** W = NULL; W = new int*[rows]; // allocating memory
for(int j=0; j < rows; j++)
{
W[j] = new int[columns];
}
W[0][0] = 5; W[0][1] = 14;
W[1][0] = 12; W[1][1] = 78;
W[2][0] = -6; W[2][1] = 43;
int result = rowsums(W[1],2);
cout << "result1 = " << result << endl;
result = rowsums(W[0],2);
cout << "result0 = " << result << endl;
result = rowsums(W[2],2);
cout << "result2 = " << result << endl;
for(j=0; j < rows; j++)
{
delete [] W[j];
}
delete [] W;
return 0;
}
// quicky16.cpp
// What is the ouput ?
//
// What do learn:
// returning pointers.
#include < iostream.h >
int* myfunction()
{
int x = 7;
int* px = &x;
cout << "&x = " << &x << endl; // =>
cout << "px = " << px << endl; // =>
return px;
}
int main()
{
int* pi;
pi = myfunction();
cout << "pi = " << pi << endl; // =>
cout << "*pi = " << *pi << endl; // =>
return 0;
}
// quicky17.cpp
//
// What to learn:
// Which constructor is called ?
#include < iostream.h >
#include < string.h > // for strcpy, strcat
// Declaration of class Message
class Message {
private:
char secret_message[64];
char* string;
public:
Message(); // constructor
~Message(); // destructor
Message(char*); // constructor
Message(char*,char*); // constructor
void display();
void print();
};
Message::Message()
{
string = NULL; strcpy(secret_message, "Much ado about nothing");
}
Message::~Message() // how often is the destructor called: =>
{
delete [] string; cout << "destructor called" << endl;
}
Message::Message(char* user_message)
{
string = NULL; strcpy(secret_message, user_message);
}
Message::Message(char* s1, char* s2)
{
string = new char[40];
strcpy(string,s1); strcat(string,s2);
}
void Message::display()
{
cout << "The message is: " << secret_message << endl;
}
void Message::print() { cout << "The string is: " << string << endl; }
int main()
{
Message poet;
Message book("Chaos and Fractals");
poet.display(); // =>
book.display(); // =>
char *s1 = "to quote";
char *s2 = " or not to quote";
Message Shakespeare(s1,s2);
Shakespeare.print(); // =>
}
// quicky18.cpp
//
// What do learn:
// Which constructor is called ?
#include < iostream.h >
class A
{
private:
int i;
double x;
public:
A();
A(int);
A(double);
int square();
double area();
};
A::A() { }
A::A(int j) { i = j; }
A::A(double y) { x = y; }
int A::square() { return i*i; }
double A::area() { return 3.14159*x*x; }
class B
{
private:
int b;
public:
int r;
B(int);
};
B::B(int y) { b = y; r = b + b; }
int main()
{
A oba(4);
B obb(7);
int result = oba.square()*obb.r;
cout << "result = " << result << endl; // =>
A obc(2.3);
double z = obc.area();
cout << "z = " << z << endl; // =>
return 0;
}
// quicky19.cpp
//
// What to learn:
// 1) Which construtor is called ?
// 2) Type conversion
//
// The compiler gives output:
// 1.18335e-038
// Explain the problem and fix it.
#include < iostream.h >
class Problem
{
private:
float a, b;
double c, d;
public:
Problem();
Problem(float,float);
Problem(double,double);
float add();
};
Problem::Problem() { }
Problem::Problem(float x,float y)
{
a = x;
b = y;
}
Problem::Problem(double u,double v)
{
c = u;
d = v;
}
float Problem::add()
{
return a + b;
}
int main()
{
Problem object(3.14,-2.1);
float result = object.add();
cout << "result = " << result << endl;
return 0;
}
// quicky20.cpp
//
// What do learn:
// 1) Copy constructor
// 2) Assignment operator
// 3) this pointer
//
// To write to output to a file named myoutput.dat
// at the command line enter after you generated the execute file:
// quicky20 > myoutput.dat
#include < iostream.h >
#include < string.h >
class ThisPointer
{
private:
char c;
int i;
double x;
int length1;
int length2;
char* str1;
char* str2;
public:
ThisPointer();
ThisPointer(char,int,double,char*,char*);
ThisPointer(const ThisPointer&);
~ThisPointer();
ThisPointer& operator = (const ThisPointer&);
ThisPointer& change();
void display();
};
ThisPointer::ThisPointer()
{
str1 = NULL; str2 = NULL;
cout << "Default constructor called" << endl;
}
ThisPointer::ThisPointer(char c1,int i1, double x1, char* s1, char* s2)
{
c = c1; i = i1; x = x1;
length1 = strlen(s1); length2 = strlen(s2);
str1 = NULL; str2 = NULL;
str1 = new char[length1+1]; str2 = new char[length1+length2+1];
strcpy(str1,s1); strcpy(str2,s2);
}
ThisPointer::ThisPointer(const ThisPointer& toCopy)
{
c = toCopy.c; i = toCopy.i; x = toCopy.x;
length1 = toCopy.length1;
str1 = new char[length1+1];
strcpy(str1,toCopy.str1);
length2 = toCopy.length2;
str2 = new char[length1+length2+1];
strcpy(str2,toCopy.str2);
cout << "Copy Constructor called" << endl;
}
ThisPointer::~ThisPointer()
{
if(str1 != NULL) delete[] str1;
if(str2 != NULL) delete[] str2;
cout << "destructor called" << endl;
}
ThisPointer& ThisPointer::operator = (const ThisPointer& toSet)
{
if(this != &toSet)
{
c = toSet.c; i = toSet.i; x = toSet.x;
if(length1 != toSet.length1)
{
delete [] str1;
length1 = toSet.length1;
str1 = new char[length1+1];
}
strcpy(str1,toSet.str1);
if(length2 != toSet.length2)
{
delete [] str2;
length2 = toSet.length2;
str2 = new char[length1+length2+1];
}
strcpy(str2,toSet.str2);
}
cout << "assignment operator = called" << endl;
return *this;
}
ThisPointer& ThisPointer::change()
{
c = c + 2; i = 2*i; x = 3.7*x;
strcat(str2,str1);
return *this;
}
void ThisPointer::display()
{
cout << endl;
cout << c << endl; cout << i << endl; cout << x << endl;
cout << str1 << endl; cout << str2 << endl;
}
int main()
{
ThisPointer oba('R',7,4.1,"-Carl","Otto");
oba.change();
oba.display(); // =>
ThisPointer obb('+',-5,7.89,"Egoli","Gauteng");
ThisPointer obc;
obc = obb;
obb.display(); // =>
obc.change();
obc.display(); // =>
obb.display(); // =>
obb.change();
obb.display(); // =>
ThisPointer obd(obb);
obd.display(); // =>
return 0;
}