File input and memory allocation in C

The purpose of this lab is to learn about reading numbers from a text file, allocating memory, and printing numbers with nicely aligned columns. Below ''the book'' refers to the course book printed in 2020.
  1. The first task is to create a hello-world program. Start an editor and a terminal window.

  2. Write a hello world program, such as on in Section 1.1 (on page 43 in 2020 ed) in the book, and save it in a file called e.g. intopt.c.

  3. Try to locate the file and cd to the same directory in the terminal (see e.g. Appendix A in the book).

  4. Type the following command in the terminal window:

    gcc intopt.c

  5. Run it with:

    ./a.out

  6. Next change your program to read two integer numbers from stdin. See Example 1.4.2 in the book. The numbers should be stored in two variables called m and n. When you run your program, you need to hit the key called "return" or "enter" after the two numbers.

  7. Print their values using the formatting string "m = %d, n = %d\n".

  8. To avoid typing so much, put your input in a file, called i and run with this command:

    ./a.out < i

  9. Now it is time to allocate memory for a linear program. If you don't have the 2020 edition of the course book, you can download pdfs with the pseudo code following this link.

    It is also a good idea to look at the video clips for Lecture 2.

    But for lab 1, you don't have to understand anything about linear programs. It is only about reading the input and printing it.

    If you have the 2020 book, see Appendix B. Use the following format for the input, where m is the number of constraints and n the number of decision variables.

    As you can see in the youtube clips, there is a vector with n c-coefficients, a matrix with m rows and n columns, and a vector with m b-values. The matrix and vectors should have elements of type double.
    m n
    c_0 c_1             ... c_{n-1}
    a_00 a_01           ... a_{0,n-1}
    a_10 a_11           ... a_{1,n-1}
                        ...
    a_{m-1,0} a_{m_1,1} ... a_{m-1,n-1}
    b_0 b_1             ... b_{m-1}
    
    Use the following input (from Appendix B):
    2 2
     1     2
    -0.5   1
     3     1
     4    18
    


  10. Print out the linear program to check that it is what you expect.

  11. Check out Example 13.20.2 on (page 507 in 2020 ed) and print your linear program with nice columns. Also add ''max z = '' and ≤ and + between columns. You can use
    <=
    or Unicode in the C string such as
    "a \u2264 b"
    for a ≤ b.

  12. To make the output even neater, you can experiment with using some additional flags. See page the next page.

  13. Now change your program to allocate too little data. Allocate memory for only one instead of two coefficients in the objective function. Run your program and see if anything strange happens.

  14. Next compile with -g to tell gcc to add debug information to a.out:

    gcc -g intopt.c

    Use Valgrind to let it find the array-index-out-of-bounds error:

    valgrind ./a.out

    What does it say?

  15. Next try the Google sanitizer:

    gcc -g -fsanitize=address intopt.c

    ./a.out

    It will say some obscure information but also some very useful things.

  16. You can download pdfs with the pseudo code and a reference implementation from here. There are two executable files. One for linux and one for macos. The one for linux should work on all 64-bit Linux X86 machines but the one for macos may not work due to missing library files. It was compiled for macOS 10.15.3.

    When you have downloaded the files, you can try it with:
    ./intopt -f simplex.in
    
    and
    ./intopt -p -a intopt.in
    


    The reason I put the files on this unusual location is that the normal location did not seem to work for an unknown reason (and these times, everyone has other things to think about...)

    You don't need to use this program for anything, but it is intended to help you debugging your code by printing how it proceeds in solving problems.

Fri Mar 27 14:59:23 CET 2020