C++ Course @ GRS 2014 Wolfgang Eckhardt, Tobias Neckel Object-oriented Programming in C++ Worksheet 1: Tips on programming and compilers: • It is recommended to work with an IDE like Eclipse / your favorite editor (VI, Emacs etc.) on a GCC compiler in Ubuntu. For further details see the links below. • Use the -Wall flag during compilation. Warnings at their best could prevent better code optimization by the compiler; and at their worst could be errors in disguise. • Printing statements is one way of debugging. However, smarter ways exist (GDB, Eclipse debugger etc.). • Good reference websites for C++: – http://www.cplusplus.com/ – http://www.cppreference.com Links Compilers: • GCC with at least C++0x (better: C++11) support: http://gcc.gnu.org – 4.6.x (partial C++0x), included in Ubuntu 12.04 (sufficient) – 4.7.x (partial C++11), included in Ubuntu 12.10 and Ubuntu 13.04 – 4.8.1 or greater (full C++11), included in Ubuntu 13.10 • Intel C++ Composer XE 2013: http://software.intel.com/en-us/non-commercial-software-development (free for non commercial use, but requires registration) • MinGW: port of GCC and basic build tools for Windows http://www.mingw.org Eclipse IDE with CDT: • Available in Ubuntu repository ”universe” since 12.04: package eclipse-cdt • or download latest version of Eclipse IDE for C/C++ Developers: http://www.eclipse.org/downloads/ • Set up the IDE: http://www3.ntu.edu.sg/home/ehchua/programming/ howto/EclipseCpp_HowTo.html 1 Assignment 1: Hello World Hello world is one of the most traditional introductions to almost every programming language. Therefore we may not miss it in our tutorial! 1 2 # include < iostream > 3 4 5 6 7 8 int main ( int argc , char * argv []) { std :: cout << " Hello World ! " << std :: endl ; return 0; } Compile & execute a hello world program. Assignment 2: Call-By-Reference vs. Call-By-Value Rewrite the code below without a return statement, using call-by-reference! 2 # include " foo . h " 3 4 5 6 int foo ( int a ) { return a * a ; } code: http://www5.in.tum.de/lehre/vorlesungen/progcourse/c++/GRS_2014/tutorial1/solution/call- by- reference/foo.cpp 2 3 # include " foo . h " # include < iostream > 4 5 6 7 8 9 10 int main ( int argc , char ** argsv ) { int a = 20; int b = foo ( a ) ; std :: cout << " foo ( " << a <<" ) = " << b << std :: endl ; return 0; } code: http://www5.in.tum.de/lehre/vorlesungen/progcourse/c++/GRS_2014/tutorial1/solution/call- by- reference/main.cpp Assignment 3: Simple Calculator We want to implement the functionality of a simple calculator. Create a file called simpleCalculator.h and provide functions for the 2 basic arithmetic operations for the floating point type double: • sum(a, b), which returns a + b; • subtract(a, b), which returns a − b; Additionally, declare a function pow(base, exponent), which calls the function pow in cmath. Implement the functions in a file called simpleCalculator.cpp, which includes simpleCalculator.h. Next, write a main function main.cpp. Prompt the user in a loop to specify an operation (+, −, p) followed by two values, or accept q to quit the loop. If the user chooses an operation, call the respective function implemented before. Therefore, the header file simpleCalculator.h has to be included in main.cpp. 2 Use formatted i/o to read and print text and variables from resp. to the terminal via std::cin and std::cout. Compile and link the two source files and create an executable calculator. Assignment 4: Sorting Numbers – Bubble Sort One of the simplest sorting algorithms is Bubble Sort. In order to sort n numbers, n sorting iterations are performed. In one iteration, each number at position i is compared with its neighbour at i + 1. If that neighbour is smaller, both elements are swapped. After the first iteration, the largest element has ”bubbled” to the end. In the next iteration, this procedure is repeated for the remaining first n − 1 elements. • Implement the function bubbleSort()! • Use the main-function available from http://www5.in.tum.de/lehre/ vorlesungen/progcourse/c++/GRS_2014/tutorial1/solution/sort/ as the programme frame! Note: The support for random numbers random and timing chrono are features of C++11, which needs to be switched on for compilation: gcc 4.6: g++ -std=c++0x -o main main.cpp bubbleSort.cpp gcc 4.7 and later: g++ -std=c++11 -o main main.cpp bubbleSort.cpp Assignment 5 (Optional): Sorting Numbers – Quicksort A more sophisticated sorting algorithm is Quicksort. It is a divide-and-conquer algorithm based on recursion: Select a so-called pivot element (usually the element in the middle of the array of numbers), and sort the numbers in two sets, one smaller and one larger than that pivot element. That process is continued recursively for each of the two sets, until each set is small enough (e.g. n <= 3) so that it can be efficiently sorted using Bubble Sort. Implement the Quicksort algorithm, using the main function for the Bubble Sort. Note: • Each number may occur multiple times in the set being sorted. • You can use an auxiliary array to sort the numbers in two sets. Fill the middle elements with the pivot elements. 3
© Copyright 2024