C++
is a powerful object oriented language, which is known for its performance, efficiency, and flexibility
due to its ability to manipulate low-level memory. The C++ syntax is quite similar to Java syntax so
Java programmers will find it easy to pick up the language.
Resources
There are many detail tutorials online about C++, here are the two popular ones:
If you prefer book-like tutorials, here are the two recommendations:
Here is the useful website to help you compile C++ file online.
Tips for leaning C++
- Always comment so it is easier to update your codes later.
- Learn the syntax carefully. Although C++ syntax is quite similar to Java syntax,
there are certain differences.
- Differentiate between call by value and call by reference,
since this concept is emphasized in C++.
- Data collection in C++ is also difference from in Java.
- Because C++ is not as good as Java in finding code errors, being careful in each small step is necessary.
- Keep practicing everyday by reading books and doing exercises.
Exercises
Here are some quick exercises for you to test your knowledge and skills
-
Write a program to convert temperature from Celsius to Fahrenheit and vice versa.
Toggle answer
#include <iostream>
using namespace std;
// function change Celsius to Fahrenheit
double c_to_f(double c){
return c * 1.8 + 32;
}
// function change Celsius to Fahrenheit
double f_to_c(double f){
return (f-32) * 5 / 9;
}
int main(){
double c;
double f;
cout << "Enter c degree values: ";
// print result
for(int i = 0; i < 5; i++){
cin >> c;
cout << c << " degree C equals " << c_to_f(c) << " degree F" << endl;
}
cout << "Enter f degree values: ";
for(int i = 0; i < 5; i++){
cin >> f;
cout << f << " degree F equals " << f_to_c(f) << " degree C" << endl;
}
}
- Write a program that will terminate if user type 5 otherwise keep running.
Toggle answer
#include <iostream>
using namespace std;
int main(){
int a = 1;
while(a!=5){
cin >> a;
// check the input is not digit
if(cin.fail()){
cout << "Invalid input"<< endl;
cin.clear();
cin.ignore(10000, '\n');
}
}
cout << "It cannot be 5"<< endl;
return 0;
}
- Write a program to calculate area and circumference of circle for given radius.
Toggle answer
#include <iostream>
#include <iomanip>
using namespace std;
// function to calculate area and circumference
void areaAndCircumference(double r, double* a, double *c){
const double pi = 3.1416;
*a = pi * r * r;
*c = 2.0 * pi * r;
r = 0;
}
int main(){
double r, c, a;
cout << "Enter the radius: ";
cin >> r;
// call function by references
areaAndCircumference(r, &a, &c);
cout << "Area: " << a << ". Circumference: " << c << endl;;
cout << r << endl;
return 0;
}
- Write a program to print all permutation of the given string
Toggle answer
#include <iostream>
#include <string>
using namespace std;
// function to swap two element in array
void swap(char *a, char *b){
char temp;
temp = *a;
*a = *b;
*b = temp;
}
// recursive function to print all permutation
void recursive_permutation(char *arr, int len, int position){
// base case
if(position == len - 1){
printf("%s\n", arr);
}
// recursive
else{
for(int i = position; i < len; i++){
swap((arr+position), (arr+i));
recursive_permutation(arr, len, position+1);
swap((arr+position), (arr+i));
}
}
}
int main(){
char array[] = "abcd";
int len = strlen(array);
recursive_permutation(array, len, 0);
}
- Write a program to make a guessing game with the following rules:
Part 1: Computer generate a random number from 1 to 100. You guess until correct.
Part 2: User think a number from 1 to 100. Train computer to guess correctly in less than 8 guesses.
Toggle answer
#include <iostream>
using namespace std;
// Part 1: User guesses
int guessNum(){
int value;
int count = 0;
int v = rand() % 100 + 1;
cout<< "Guess a number: ";
cin>>value;
while (true){
count ++;
if(value < v){
cout << "Too low" << endl;
}
else if(value > v){
cout << "Too high" << endl;
}
else{
cout << "You are correct after " << count << " guesses" << endl;
return 0;
}
cin>>value;
}
return 0;
}
// Part 2: Computer guesses
int guessUserNum(){
char decide;
int count = 0;
int min = 0;
int max = 100;
while (true){
int guess = (min+max)/2;
cout << "Computer guesses " << guess << ". Is it higher(h), lower(l) than your number, or correct(c-default value)" << endl;
count++;
cin >> decide;
if(decide == 'l'){
min = guess + 1;
}
else if(decide == 'h'){
max = guess - 1;
}
else{
cout << "Correct after " << count << " guesses" << endl;
return 0;
}
}
return 0;
}
int main(){
guessNum();
guessUserNum();
}
- Write a program that asks user to enter the number of pancakes eaten by 10 different people.
Part 1: Print the person eat the least and eat the most along with their number of pancakes.
Part 2: Display list of people in order of number of pancakes eaten.
Sample run:
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
........................
Person 5: ate 0 pancakes
Toggle answer
#include <iostream>
using namespace std;
// function to find people eat the most pancake
int findMax(int arr[], int len){
int max;
int maxIndex;
max = arr[0];
for(int i = 1; i < len; i++){
if(arr[i]>max){
max = arr[i];
maxIndex = i;
}
}
return maxIndex;
}
// function to find people eat the least pancake
int findMin(int arr[], int len){
int min;
int minIndex;
min = arr[0];
for(int i = 0; i < len; i++){
if(arr[i] < min){
min = arr[i];
minIndex = i;
}
}
return minIndex;
}
// display people in order
int find_order(int arr[], int len, int indexes[]){
int result[len];
for(int i = 0; i < len -1; i++){
int maxIndex = i;
for(int j = i+1; j < len; j++){
if(arr[maxIndex] < arr[j]){
maxIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
temp = indexes[i];
indexes[i] = indexes[maxIndex];
indexes[maxIndex] = temp;
}
return 0;
}
int main(){
int a, value;
int len = 10;
int arr[len];
for(int i = 0; i < len; i++){
cout << "Enter value: ";
cin >> value;
arr[i] = value;
}
int minIndex = findMin(arr, len);
cout << "Min is " << arr[minIndex] << endl;
cout << "Person " << minIndex + 1 << " ate " << arr[minIndex] << ", the least number of pancake" << endl;
int maxIndex = findMax(arr, len);
cout << "Max is " << arr[maxIndex] << endl;
cout << "Person " << maxIndex + 1 << " ate " << arr[maxIndex] << ", the most number of pancake" << endl;
cout << "Display in order: " << endl;
int sorted_array[len];
int sorted_indexes[len];
for(int i = 0; i < len; i++){
sorted_array[i] = arr[i];
sorted_indexes[i] = i;
}
find_order(sorted_array, len, sorted_indexes);
for(int i = 0; i < len; i++){
cout<< "Person " << sorted_indexes[i] + 1 << " ate: " << sorted_array[i] << " pancakes" << endl;
}
return 0;
}