Programming and Algorithms II Degree in Bioinformatics Fall 2018 ***************************************************************** *** Lab 8: *** *** Multi-file c++ programs (and some more single-file ones) *** ***************************************************************** READ THIS FIRST To compile a multi-file program, simply type >> g++ file1 file2 file3 ... fileN where file1 ... fileN are all the files that compose your program and >> indicates your system console. They normally have extensions .cc or .cpp (for C++ source files) or .o (object files, produced by a previous compilation). g++ will do the following: - it will compile every file that has a .cc or .cpp extension. For example, if file1 is my_main.cc, it will produce my_main.o. If there are any errors (called compilation errors), it will stop. Else: - It will link all the .o files, and if all goes well produces an executable file with a bland name such as a.out. All goes well if every name mentioned in the source files is resolved ok (is defined once and only once in the object files), in particular the "main" name. Otherwise you will get errors (called linking errors). You can then execute a.out: >> a.out Depending on system configuration: - you may need to tell the system that a.out is in this directory. Write: >> ./a.out - tell the system that a.out is an executable file. Do (before executing): >> chmod +x a.out which changes the x flag for the file to mark it executable. (Probably not necessary, but just in case.) The g++ compiler has many options that you can use to tell it stuff. Here is a full list: https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html#Option-Summary (disclaimer: the teachers do not know what many of these mean). You can also use "man g++" Most commonly used are: - -c says not to use the linker, just compile and produce .o files. You want to use this to test. Example: "g++ -c my_prog.cc auxiliary.cc" will produce main.o and auxiliary.o. Perhaps you haven't written some other important file but want to check if these two are compiling ok. Some .o files may be deleted if the build succeeds. - -o name: will make "name" the name of the executable file. For example, "g++ -o my_main.exe my_proc.cc auxiliary.cc" sounds reasonable. WATCH OUT: don't make this mistake: >> g++ -o file1.cc file2.cc file3.cc because you will lose file1.cc. Yes, ideally the compiler should be clever enough to detect that this is probably a mistake and not make your day miserable. But it doesn't. - -Wall: -W is a generic flag telling to compile to Warn you of things that, even if not strictly incorrect in C++, look suspicious. Probably a good idea to use it while you are not familiar with C++. And even after you are familiar with C++. For even more warnings, use -Wextra. Get used to writing: >> g++ -Wall -Wextra files ************ TO DO Ex.1. Write a function bool is_palindrome(string s) { ... } that tells whether s is a palindrome. You can access individual characters of s with the usual notation s[i]. The length of string s is s.length(). Place your function in palindrome.cc, with nothing else. Compile it with g++: >> g++ palindrome.cc What error do you get? How should you call g++ to not get an error? Ex.2. Create a file palindrome.h that only contains bool is_palindrome(string s); Then create a file my_main.cc that includes palindrome.h and has a main() that keeps reading words and telling whether they are palindromes or not. Now compile everything (in one g++ line, or in several lines) to get an executable file, and run it. What do you get if you put "cout << is_palindrome(s)" in your program? Ex.3. Write a program that reads a sentence and then prints it backwards word by word, that is, the first word last and the last word first. So on this input: If you really want to hear about it, the first thing you'll probably want to know is where I was born, and what my lousy childhood was like, and how my parents were occupied and all before they had me, and all that David Copperfield kind of crap, but I don't feel like going into it, if you want to know the truth. (the opening line of "The Catcher in the Rye"; you haven't read it? Do!) it should print back: truth. the know to want you if it, into going like feel don't I but crap, of kind Copperfield David that all and me, had they before all and occupied were parents my how and like, was childhood lousy my what and born, was I where is know to want probably you'll thing first the it, about hear to want really you If (it is ok if the output is in one long line, even if you gave the input in many lines with return at the end). 3.1 Do it with an array *assuming* that the sentence has at most 100 words. Make this 100 a constant with #define, so that it can be changed easily. 3.2 Do it with a vector, without any assumption on the length of the sentence (remember to #include ). The size of a vector v is v.size(). vectors, unlike arrays, have the equivalent of an .append operation, it's called push_back. Look for it. 3.3 Do it without vectors or arrays, using recursion. Trick question: How much memory does this solution use, in O(...)? More than solution 3.2, same, or less? Answer: same in O(...); if you don't understand why, ask; it is important. 4. Polynomials. 4.1 Define the following type typedef struct { int degree; vector coeffs; } Polynomial; and write procedures to sum and multiply two polynomials. For example, for p1 = 3x^2+10, p2 = 5x+8, the sum is 3x^2 + 5x + 18, and the product is 15x^2 + 24x^2 + 50x + 80. Think carefully about the headers, what parameters and modes they have, and what the return type is; there is more than one option. Put the definition and the two procedures in files Polynomial.h and Polynomial.cc. Make sure the files compile correctly. 4.2 Create files IOPolynomial.h and IOPolynomial.cc that contains functions that read a polynomial from keyboard and write a polynomial to screen in "nice" way, e.g. 3x^2 + 5x + 18. Make sure the files compile correctly. 4.3 Write a main.cc file with a loop the repeatedly: - lets the user choose + or * - reads two polynomials - applies the chosen operation, + or *, to the two polynomials, - and prints the result again and again.