Programming and Algorithms II Degree in Bioinformatics Fall 2018 ***************************************************************** *** Lab 7: *** *** Writing single-file C++ programs *** ***************************************************************** ********** READ THIS FIRT. Compiling a single-file C++ program. Assuming you are using the g++ compiler and that your program is file.cc, type (>> indicates your system console): >> g++ file.cc and you will get an executable file, whose exact default name depends on the system. If you want to call your executable file with some particular name such as file type >> g++ file.cc -o file or >> g++ -o file file.cc DO NOT do >> g++ -o file.cc because the compiler will first delete file.cc, and you lose your source file. ******** TO DO TODAY (AND IN YOUR FREE TIME) Write C++ functions for all the following. One important part of the problems is defining the headers appropriately: - the return type of the function (void, or something else), - the parameters, their type, and their passing mode (value, reference, or constant reference). Use vector> for matrices. In addition, create main programs that read the appropriate parameters from the keyboard, call the function, and prints the results, repeating while there is an input. Advice for problems that require complicated inputs: - put your input in a file input.txt - execute the program to read from the input. In the console, type >> file < input.txt That saves you from typing the long input again and again. (>> still stands for your console mark, do not type it). ************************ THE EXERCISES - power(x,y) that returns x raised to the y power, where y is a non-negative integer. Use a loop (no calls to library functions). - max(v) that the returns the max of v, where v is vector and v.size() > 0. - stretch(v), that returns the longest stretch of non-zero values, where v is vector. For example, for v = [1,2,0,3,4,5,0,6,7] it returns 3, and for v = [1,2,3,0,4,5,0,6,7] it returns 3 as well. - palindrome(s), where s is a string, that returns True or False depending on whether s is a string or not. - concat_all(v), where v is vector, that returns the concatenation of all strings in v. (Google up how to concatenate strings, if needed). - readmatrix() that reads values n and m, then nxm doubles interpreted to be the n rows of an nxm matrix, puts them in a newly created matrix, and returns it. Note that you can use cin >> n >> m to read two values at a time. cin returns also True if the value or values could be read, and False if the value could not be read. So it is common to use while (cin >> n) { do something with n } to read values until there is no more input. cin does not care about line breaks, at our level. It just treats blanks, tabs, line breaks, etc. as equivalent, just to separate a number from the next. - writematrix(m) that receives a matrix of doubles and prints it out to screen, one row per line. - product(a,b), where a and b are matrices of compatible sizes for product, and returns the product. Note: you will have to create a new matrix in the function, initialize it, and return it. - product(a,b,c), where a, and b are of compatible sizes for multiplication and c has the right size to be the product of a and b. The three matrices are supposed to be created already. Watch out particularly with the parameter passing modes in the last exercise. You normally would use it this way: vector> mat1, mat2, mat3; ... fill in mat1; ... fill in mat2; product(mat1,mat2,mat3); // compute mat3 = mat1*mat2 what if the user then does product(mat1,mat2,mat1); // I want to compute mat1*mat2 and put it in mat1 or product(mat1,mat1,mat1); // I want to compute the square of mat1 and put it in mat1 Does your program still work ok? How do you fix it?