CMPT 102   Lab 1

This lab is designed primarily to give you practice using the basic C tools introduced in lab1 and the components of the C language discussed in class last week.  Detailed instructions on how to complete the lab problem are followed by the statement of the lab problem to be solved.  An additional practice problem is also provided.

1.     Make a directory to hold all your files for this lab

a)     Log in to a Linux machine (or a Cygwin window if you are working at home)

b)     The red hat on the bar at the bottom of the screen is the icon for your start menu.

c)     Select system tools from your start menu

d)     Select terminal from the system tools menu

e)     A terminal window like the one below will appear

f)      In the terminal window type kate, followed by enter

g)     A kate text editor window should open for you.

 

2.     Write your program

a)     You have two choices, either

                                               i.          Type your program into the text editor window

                                             ii.          Load the sample add.c program from lab 1 and edit it to become your new program

b)     When you wish to save your program select ‘save as’ from the file menu of the kate editor.

                                               i.          Save the lab problem in a file called expansion.c, you may choose any name for the c file containing the solution of the practice problem

                                             ii.          Reload the lab program into the kate editor by selecting ‘reload’ from the file menu of the kate editor

c)     Each time you make changes to your file you will need to save them (by selecting ‘save’ in the file menu) and reload the file before trying to compile or run the program.

d)     If you leave the lab after you save your file, next time you return follow the steps in 1 but when you  start kate give the name of your .c file as an argument

kate  expansion.c.               (To edit the file containing c code named expansion.c)

3.     Compile your program

a)     Make sure your most recent changes have been saved, and that the saved file has been reloaded into kate.

b)     Press the terminal icon at the bottom of the kate window. This will produce a split screen, the upper window will contain you code in the kate editor, the lower half is a terminal window in which you can type in the commands discussed below.

c)     To compile the program in the text file expansion.c execute the following command by typing it into the kate terminal window (remember to type the enter key after the end of the command)

 

gcc     –Wall –c     –o expansion.o      expansion.c

 

gcc is the name of the c compiler application we are using (GNU C Compiler or GCC).  There are many options available when using this compiler, we will cover only a few of these options in this course, You can see explanations of these and other options at any time by typing      man  gcc.

-Wall  is an option that provides more extensive compiler messages warning you of potential problems in your code. These extra warning messages will help you find the problems in your code.

 -c  and option that tells the c compiler to compile the input text file only,  the linker will not be executed.

-o expansion.o   specifies that the name of the object file which will contain your compiled code. In the case of the example above this name would be expansion.o. Regardless of the name you use for your object file it should end in .o to tell the compiler it is an object file resulting from compiling a c text file. 

expansion.c  The test file containing the C code you wish to compile. The name of this file must end with .c.  Ending the file with .c tells the compiler that the text file contains C code, rather than objective C, C++, or other flavors of C code that this compiler also supports.

d)     When you compile your code using a variant of the command above (filenames will change) you will observe one of two outcomes

                                               i.          You will see a prompt just like the one at which you typed in the command above.  This means that your C code has been successfully compiled, and an object file containing the compiled code has been created

                                             ii.          You will see a list of error messages that tell you there are still syntax and semantic errors within your C code.  In this case no object file is created,  (Be careful, if you have an object file from a previous version of your code, linking the object file after a failed compile will mean you are recreating the previous version not the present version with your most recent changes). Your next task is to correct those compile time errors and recompile the text file.  Remember to save and reload your file after each group of changes you make before you recompile.

4.     When you can compile your code successfully try linking your code

a)     To link the object file expansion.o execute the following command by typing it into the kate terminal window (remember to type the enter key after the end of the command)

 

gcc     expansion.o           -lm     -o expansion

expansion.o   specifies that the name of the object file which contains your compiled code. In the case of the example above this name would be expansion.o. Regardless of the name you use for your object file it should end in .o to tell the compiler it is an object file resulting from compiling a c text file.

-l specifies that a library should be linked with your input object code to produce your output executable code. In the example above –lm indicates that the standard math library should be linked with you own object code.  You will need the standard math library is you use any functions like sin, cos, exp (exponential) etc. 

-o expansion specifies that the name of the executable file which will contain the compiled and linked code. In the case of the example above this name would be expansion. It is not necessary to give your executable file any particular extension (like .exe) to identify them.

b)     When you link your code using a variant of the command above (filenames will change, you may or may not need to link the math library) you will observe one of two outcomes

                                               i.          You will see a prompt just like the one at which you typed in the command above.  This means that your C code has been successfully linked, and an executable file containing the compiled code has been created

                                             ii.          You will see a list of error messages that tell you there are still semantic errors within your C code.  In this case no executable file is created. (Be careful, if you have an executable file from a previous version of your code, running the executable file after a failed link will mean you are running the previous version not the present version with your most recent changes). Your next task is to correct the link errors and recompile and relink the text file.  Remember to save and reload your file after each group of changes you make before you recompile and relink..

5.     You can also compile and link your code with a single command (shown below). In this case no object code will be created, you will create an executable file directly from your C text file.

gcc     -Wall           -lm     -o expansion          expansion.c

6.     When you can link successfully try running your code

a)     To run your executable code from the terminal window execute the command below by typing it and then pressing the enter key

./expansion

b)    One of three outcomes will happen when you run your program

                                              i.          Your program may work correctly and print the expected results   OR

                                             ii.          Your program may terminate with and error  OR

                                           iii.          You program may complete and produce an incorrect answer

c)     In the second two cases you will need to correct your logic errors. After correcting your errors you will need to save your changes, reload the file into kate, recompile and relink before you can retest your new executable file.

7.     When you have a completed executable file try following the instructions on the submission server page.  These instructions will help you submit the C text file (filename.c) for your completed program electronically to the submission server.  Before you submit your file you will need to zip it. Zipping a file will compress that file. If you have more than one file to submit you must combine them with tar before you zip your submission for the submission server.  To use tar and zip visit the instructions for using tar and zip.

8.     Print a copy of the C text file for your program and a copy of the lab exercise cover sheet from the class website.  Fill out and sign the cover sheet.  Be sure that all members in the group must sign the cover sheet.  Hand in the hard copy of your lab to the CMPT 102 drop box.  The hard copy of this lab problem includes a listing of your C text file and a signed cover sheet.

 

LAB PROBLEM:   (submit only this problem for marking, your C code should be saved in a file named expansion.c)

You are designing a highway bridge. In particular you are trying to determine how to put the road surface on the bridge. You live in an area where the maximum summertime temperature is 40 degrees centigrade and the minimum winter temperature is -20 degrees centigrade.  The coefficient of expansion, α, measures the fractional change in length per unit change in temperature. Consider a section of road surface with length L at temperature T.  If the temperature T increases to T2 the length of the section of road surface will increase to L+∆L. In the temperature decreases to T3 the length of the section of road surface will decrease to L-∆L. The amount the length changes by is  ∆L = ∆Tα  where  ∆T=T2-T  or  ∆T=T3-T.  You are to write a program to determine how much space to leave between the sections of road surface on the day they are installed. You must leave enough room for the road surface to expand to the length it would have at the maximum temperature of 40 degrees. The user will provide the temperature on the day the installation takes place, the length of each section of road surface to be installed and the coefficient of expansion of the material being used in the road surface.

 

PRACTICE PROBLEM:

Write a C program that determines the circumference (2 Π R) and area of a circle (Π R 2.),  You will need to start a second project.  Make sure to use the constant Π.  The program should prompt your for the radius R of the circle, then print out 2 messages. Each message should appear on a single line with no other text on the same line. The first message should say “The circumference of the circle with radius  …  is … “.  The second message should say “The area of the same circle is …”. In these messages the … should be replace with the radius, circumference and area of the circle respectively. 

 

If you want more practice try some of the end of chapter problems in your textbook.