Scientific Programming 2015

Home Work Set I

Advanced problems are marked in bold


Part I: Compiling, linking, executing

    1.   Write a "Hello World" program. Using your favorite programming language write a program that will print "Hello World", compile, link and execute it.

    2.   Write the main program and the subroutine dictionary in a single file. The subroutine should contain an array of words: "new", "old", "student", "doktorand", "teacher", "professor", "programming", "skip", "fail", "succeed", "complete", "course", "difficult", "easy", "hopeless", "great" and more, if you like to. This subroutine should take an array of integers as parameter and return a sequence of space-separated words in a single string according to the numbers in this array. The main program should call subroutine dictionary several times with different (random) array length and content. The main program should also print the results. Compile, link and run it.

    3.   The same as in 2 but put the subroutine in a separate file. Compile, link and run. Use optimization flag(s).


Part II: I/O

    4.   Generate a calendar for 2016 and write it into a text file in the following format

   January 2016

Week  1  2  3  4  5
Mon      4 11 18 25         
Tue      5 12 19 26
Wed      6 13 20 27
Thu      7 14 21 28
Fri   1  8 15 22 29
Sat   2  9 18 23 30
Sun   3 10 17 24 31

   February 2016

    5.   Create a program that will read the calendar from the file and check if in 2016 we will have any Friday the 13th.

    6.   Download this binary file consisting of 1024 floating point 8-byte long numbers (double precision) grouped in 16 records of identical length. The file was produced by a FORTRAN code. Open and read it. Print the numbers.

    7.   Download this binary file consisting of 1024 4-byte integers. The file was produced by a C code. Open and read it. Print the numbers. Remember that the platform used to generate these data files may have different byte order from your computer.

    8.   Use the file in the exercise 6. Subtract 1000000 from all the numbers in record 11 and write it back into the same file. Close the file.

    9.   Repeat the exercise 6 on the modified file but this time read record 11 only. Print its content.

 10.   Read and print the list of files in the current directory.

 11.   Create a program that will return the length of a given file.


Part III: Text processing

12.   Take the content of this web page and save it in a text file. Write a program that will read this file, compute the occurrence of different words and print those words sorted alphabetically followed by their numbers.

13.   Take the description text for this problem. Sort all the symbols (letters, spaces, numbers, points etc.) in increasing order (space, 1-9, A-Z, a-z) and decreasing order (z-a, Z-A, 9-1, space) and print the unique characters.

14.   Write a personalized spam-distribution system. It should take a message from a file and a table of names – emails from another and create (but not send) a number of emails with polite intro e.g. Dear Sherlock Holmes, ... and associated commands to send these mails.

 15.   Take the description text for this problem. Convert all the uppercase characters to low case and print. Compress the text (remove all spaces and tabs) and print.


Part IV: Math

16.   Solve a system of linear equation using Gauss elimination:
 2.0  1.0  1.0   x1    3.0
 4.0 -6.0  0.0 X x2 = 10.0
-2.0  7.0  2.0   x3   -5.0

17.   Write a subroutine for Gauss elimination using an arbitrary-size matrix. Apply it to the previous problem.

 18.   Write a subroutine for finding real roots of quadratic equation: a*x2+b*x+c=0. Keep track of special cases (e.g. when a=0).

 19.   In the main program create 100000 (or smaller) element arrays of different a, b and c. For all possible combinations of a, b and c, call the subroutine from 18 and find the minimum and the maximum root value. Try different sequence of nested loops (a-loop is fastest, b-loop is fastest, c-loop is fastest) and note the execution time difference (Linux/Mac OS X users can use time command). Do you see execution time difference, why? Remove the evaluation of the maximum/minimum and the printing part. Use the fasted optimization and note the execution time. Can you think of ways to speed up this exercise dramatically?

 20.   Write a random number generator that will take a seed number (floating point) and the length of the random sequence and return uniformly distributed random numbers between 0 and 1. Use the web to find simple algorithms for generating random numbers.

 21.   Compute analytical and numerical 2nd derivatives of a cubic polynomial 0.001*x3-0.3*x2+2.0*x-20.0 at x=107. How different are the results and why? Can you improve the situation?

 22.   Write a subroutine for a box-car filtering in 2D. The parameters should be: a 2D array of type integer, its dimensions, the size of the fragment to be filtered (smaller or equal than full size) and the size of the filter in X and Y in pixels. Use this subroutine to filter a photo of yourself. Prepare the original, the filtered result (say for filter box 10x10) and the difference between the two.