Fundamentals Lab #1 Solution

$30.00

Description

  • Objectives

The objectives of this assignment are multifold: to learn basic UNIX commands, to learn how to submit your solutions to the autotester program, and to provide you with practice on the edit-compile-run-debug cycle of C++ programs in a UNIX environment. The C++ programming language is, in many ways, a superset of the C programming language you are already familiar with, so doing this Lab Assignment in C++ should be straightforward. This lab will also provide an introduction to using the NetBeans IDE (Integrated Development Environment), which pro-vides extensive functionality to make source code entry, browsing, building, and debugging more convenient.

  • Problem Statement

In this lab you will rst create a directory for ECE244 assignments in the le system under your UNIX account on ECF. You will then copy a simple C++ program, compile it, check it for correct output, edit the program, recompile and submit to the autotester. Next, you will use the GNU Debugger (GDB) from within NetBeans to debug several programs. The rst few programs give in-depth guidance on how to accomplish the goals, while the latter programs leave you with just hints. To follow the hints, consult the debugging guide which will have more details.

  • Procedure

3.1 Setting up your directory

Create a new directory for this course called ece244, and protect it by setting the permissions so only you can read or write it. See the \ECF Unix Environment” document reading for details (hint: chmod R go-rwx ece244).

It is your responsibility to ensure that others cannot copy your work. If another student is able to copy your work, and we determine that two submitted solutions stem from the same source, then we have no option but to take action against both parties.

Download the lab1 les from the course website. They will be in an archive called lab1.zip. Extract the le into your ece244 folder using the command unzip lab1.zip1 This will create two folders: lab1 and tutorials.

3.2 Preparation: tutorials and background documents

Change your directory to the tutorials directory (hint: cd tutorials). Please read the three background documents listed below and do the exercises contained within.

ECF Unix guide.pdf: The ECF UNIX Environment, commands and lesystem

NetBeans intro.pdf: \Getting Started with NetBeans” guide

Debugging intro.pdf: \Intro to Debugging” guide

1How to use the zip and unzip commands is covered in the ECF Unix Environment document.

Page 1 of 10

ECE244 Programming Fundamentals Fall 2018

It is critical that you read the entirety of these three documents { taking shortcuts now will lead to you having a poor understanding of the tools we will use in this course, and make later labs very di cult to complete. Make sure in particular that you understand the process to check and submit your work. Many students receive a grade of 0 on a lab, not for failing to do the work, but because they did not submit the les correctly.

3.3 Importing and building the \Hello, World” program

The classic “Hello, World!” program is shown below. It can be found in the hello folder under lab1 which was extracted from the archive. Use the cd command to change your current directory to the texttthello directory.

  • // hello.cpp — “Hello, world” C++ program

2

  • #include <iostream>

  • using namespace std;

5

  • int main () {

7

  • cout << “Hello ” << endl;

  • cout << “world!” << endl;

  1. return (0);

  1. }

12

  1. Open NetBeans (from the command line, you can type netbeans to start it).

  1. Create a new project (File ! New Project) from existing sources

    1. Choose C/C++ from existing sources since the les are already there, then click next

    1. Click the Browse button and select the folder you just created (<…>/ece244/lab1/hello)

    1. Leave all other settings at default

  1. In the project navigator at left, open hello.cpp by double-clicking. Note the C++ keywords are highlighted.

  1. NetBeans will by default have built (compiled and linked) the program after importing. If you want to build again after making source changes, you can do so by clicking the hammer,

or through the menu Run ! Build Main Project. The hammer-and-broom icon is for clean and build (ie. delete all non-source les rst)

  1. Run the program by clicking the green right arrow button in the toolbar or from the menu

Run ! Run Main Project. You should see the expected output in the window pane below the source editor.

3.4 Starting with the debugger: using breakpoints

Before going on, be sure you have read the \Intro to Debugging” document, and worked through the examples within it.

One of the most fundamental functions of the debugger is to \pause” a program either at a speci ed source line, or when a certain condition is true to let you inspect what is going on. The most basic way to do this is through setting a breakpoint, which will be illustrated below:

Figure 1: NetBeans basic toolbar for building and running projects

  1. Set a breakpoint on the line where \world” is printed

  1. Run the program normally, noting that it runs just like before: the breakpoint does nothing unless we start the debugger.

  1. Now run with the debugger. Either click the button that shows a breakpoint with a green triangle at lower right (see Fig 2), or menu Debug ! Debug Main Project2.

  1. Note that the program prints \Hello,” and stops. The source line is also highlighted in green. The program execution has paused, waiting for your action. In this case, simply continue. The program should print the rest and nish.

Figure 2: The debugging toolbar

3.5 Using exercise to test your program

One utility provided to help you is the exercise script3. It will run your program and provide it with test input, then compare the output expected against what is actually provided. You will be noti ed whether or not the two di er. When you run the hello program, you should see:

Now let’s test the program before submitting it to see if that works:

  1. Run the program and verify that you get the output shown in Fig 3

  1. Use the exercise script to test your hello program

Hello

world!

Figure 3: Output produced

Hello, world!

Figure 4: Expected output

  1. You should nd that it fails – it was expecting something more like Fig 4

  1. Edit the code so that it produces the expected output

  1. Test again, hopefully it works

You should run through this process for every lab that you submit to ensure you are producing correct output. Remember that output produced must be exactly what is speci ed in the handout

  • no di erence is permitted in spaces, capitalization, spelling, etc. The functionality marks are produced by a test script which checks for exact equivalence. Note also that (where user input controls the program) the exercise script will only run one or a few test cases. In the future, you will be evaluated on more cases than are run in exercise to ensure your program can handle a range of input.

3.6 Finding a run-time bug with harmonic.cpp

The next program, found in the harmonic folder, is supposed to ask the user for a number and then print the corresponding harmonic number. The N-th harmonic number is de ned as

N 1

1

1

1

X1

= 1 +

+

+ : : : +

(1)

i

2

3

N

In this program, it has been implemented as a for loop with a variable used to accumulate the sum as it goes. However, you will nd that although the program compiles correctly and runs it does not produce the expected output.

  1. Create a project and compile the le

  1. Run for a few test cases to see if correct output is produced

  1. Find the bug. Suggested procedure:

    1. Set a breakpoint within the loop body

    1. Run the program with a reasonable (small) input number, say 3

    1. Check the value of the variables (Variables window) when the breakpoint is encountered

    1. Hit continue to go through the loop step-by-step and watch the values

  1. Change the code and recompile

  1. Re-test to ensure correct functioning

When you believe you have xed the bug, you should run ~ece244i/public/exercise 1 harmonic

to con rm that the program generates the expected output.

3.7 Another run-time bug: temperature conversion with convert.cpp

Our third program takes a temperature in degrees Fahrenheit, Celsius, or Kelvin, and prints out its equivalent in the other scales. Where TF , TC, and TK are the temperatures in each of the scales, the conversion between them is de ned by the equations below:

TF =

9

TC + 32:0

(2)

5

TK = TC + 273:0

(3)

For your convenience in testing, several equivalent values with physical signi cance are given below.

De nition

o C

o K

o F

Boiling point (liquid nitrogen)

-196.0

77.0

-320.8

Freezing point (pure water)

0.0

273.0

32.0

Boiling point (pure water)

100.0

373.0

212.0

Normal body temperature

37.0

310.0

98.6

Table 1: Selected temperature values

  1. Create a NetBeans project

  1. Compile the program, xing errors as needed4. NetBeans will help you navigate the error messages, and ag syntax errors. You can run

~ece244i/public/exercise 1 convert

to automatically run some test cases, but you should also try some test cases of your own.

  1. If you nd that it does not, try to nd and x the error(s). As often happens with software, be aware there may be more than one bug to be xed.

HINT Sometimes when debugging a process involving many steps, the single-step function in the debugger can be very useful to follow along. This can help you con rm that program control is following the path you think (or not!). Often, this is a far more e cient process than inserting random print statements or staring at code.

Important Make sure you do not change the output formatting. The exact same spacing and number of decimal places must be given to receive credit.

3.8 Debugging a large program

Part of the code distribution is early version of VPR (Versatile Place and Route) for FPGA, the software part of Vaughn Betz’s PhD thesis. It is a fairly large piece of software, consisting of over 20k lines of source. To illustrate the use of the debugger, an error has been deliberately inserted

into VPR to cause a run-time error. Trying to locate a fault by inserting print statements would be next to impossible in a program of this scope. Instead, you will use the debugger available in NetBeans to locate the fault quickly and precisely.

The steps below will walk you through compiling and running the program, which should cause it to die with a segmentation fault. Recall from the debugging tutorial that a segmentation fault occurs when the program tries to read memory that it’s not allowed to. This may be because of an invalid pointer, or exceeding the bounds of a valid pointer. When programming, it is good practice to initialize all pointers to NULL (value 0x0) because that value is never a valid memory address and so is easily recognized as an uninitialized pointer. Otherwise it is hard to tell by inspection what values are valid or not.

  1. Create a NetBeans project for the vpr folder using the existing source and Make le

  1. Compile the project (there should be no compile-time errors)

  1. Run the program – it should just print a usage message explaining the (complicated) command-line options

  1. Run the program again with the command-line options below (see \Getting Started With NetBeans” to see how you can set command-line options) – it should get partway through and then die reporting \Segmentation fault”

vpr e64-4lut.net 4lut_sanitized.arch placed.out routed.out -nodisp

  1. Run again using the debugger to localize the exception

    1. When the segfault happens, the Operating System will send the program a “signal” (i.e. a message) which is intercepted by GDB/NetBeans – it will pop up a dialog box asking you what to do.

    1. Choose to \discard and pause”, i.e. do not pass the signal to the program, but pause it and let you work with the debugger.

    1. Look at the sequence of functions the got you to this point starting at main() (inspect the call stack5) to see where you are and how you got there. The function names won’t have signi cance to you, but that’s OK; just poke around.

    1. Inspect the local variables to see their values.

    1. The fault should happen on an illegal memory access. The debugger will show you the line on which the bad memory access occurs. Look at the variables on that line, especially pointers (and remember that an array is really a pointer to a block of memory). Which pointer/array variable has an invalid value?

  1. Scroll around within the function a bit to see where the pointer’s value is (or should be) de ned – in this case, it should be clear and easy to x.

  1. Demonstrate how you located the fault to your lab TA (details below in Sec 4.1).

  • See debugger tutorial for details – look in the Call Stack pane at the bottom

.

The command line to run the example le is also saved in test.sh6 for your convenience.

A few helpful hints as you try debugging:

You can inspect the value of a variable while debugging either by using the Variables window, or by hovering the mouse cursor over the variable name.

One hidden \feature”: to inspect the base pointer in an array expression such as p[i][j] you have to highlight the base pointer name (p) rst and then hover the cursor over it.

3.9 Setting up your environment to use C++ 2011

The C++ 2011 standard will be assumed in both developing and marking assignments. Thus, it is essential that your ECF environment be set up to use the appropriate C++ compiler, i.e., one that suppots the 2011 standard. This can be done with a simple command. Login into your account and start a terminal window (i.e., an xterm). If you do not know how to start terminal, ask for help from one of the TAs.

Once the terminal starts, type the following command at the prompt (usually a % or a $):

ecf-sw-env -a ece244

The command produces no output. However, to take e ect, you must logout from your ECF account and then login again. To make sure that you have the correct environment set up, start a terminal and type the following command at the prompt:

g++ -v

The command will print a few lines of output that should end with the following line if the environment is set up correctly to use C++ 2011 compilers:

gcc version 5.3.1 20160406 (Red Hat 5.3.1-6) (GCC)

If the gcc version is not 5.3.1 and you have typed the ecf-sw-env command correctly, please ask one of the TAs for help.

3.10 Setting up NetBeans to use C++ 2011

It is also essential to make sure that Netbeans is using the appropriate C++ compiler. Normally, this would be the case. However, if you have used NetBeans before with an older version of the C++ compiler (e.g., in APS105). Thus, Netbeans needs to be told to update the compiler version to the one you setup with the ecf-sw-env -a ece244 command above. This can be done in a few simple steps. These steps need to be done only once.

  1. Start NetBeans (if not already started).

  1. Click on Tools menu and select Options from the dropdown menu.

  1. Select C/C++ to display the locations of the C/C++ compiler and associated tools (see Figure 5 below). Observe the location of the C++ compiler.

  1. Click on Restore Defaults button at the bottom (see Figure 5 below) and click OK in the resulting dialog box. You may observe that the locations of the C/C++ tools has changed.

Figure 5: Window for restoring C/C++ Default Compiler

You can verify that NetBeans is setup correctly using the test11.cpp code shown below. You do not need to understand what the code is doing at this stage. The code can be found in the test11 folder under lab1, which was extracted from the archive. Use the cd command to change your current directory to the texttttest11 directory.

  • #include <iostream>

  • using namespace std;

3

  • class Test11 {

  • public:

  • int x=9;

  • };

8

  • int main() {

  1. Test11 a;

11

  1. cout << a.x;

13

  1. return 0;

  1. }

16

17

  1. Create a new project (File ! New Project) from existing sources

  1. Choose C/C++ from existing sources since the les are already there, then click next

  1. Click the Browse button and select the folder you just created (<…>/ece244/lab1/test11)

  1. Leave all other settings at default

Build and run the code and if NetBeans is setup correctly, you will get no build or runtime errors.

Should you create a new project from scratch in NetBeans, make sure that you select the C++11 option as shown in Figure 6.

Figure 6: Make sure a new project is created for C++11

  • Deliverables

4.1 Demonstration

Demonstrate the following to one of your lab TAs:

Find the source le and line number where the vpr program is crashing. Figure out how the program arrived at this point (inspect the call stack). Identify the type of crash and why it is occurring.

Find the (deliberately commented out) line causing the error and uncomment it.

18

Recompile and demonstrate correct function of the program.

Compile and run the test11.cpp program with no errors or warnings.

4.2 Autotester submission

Please submit the following les to the autotester, after you have xed the code in each so they produce correct output and pass exercise.

hello.cpp

harmonic.cpp convert.cpp

Remember to use exercise to test each program. When you are satis ed both programs are correct and perfectly match the expected output, copy all three les into one directory and use

~ece244i/public/submit 1

to submit the assignment for marking. Carefully examine the output from submit to ensure your program was submitted correctly and passed basic checks, or you may receive 0 for this part of the lab. You can submit your work as many times as you like; only the last submission will be graded. Also check that you have set your le permissions correctly to prevent others from copying your work.

Page 10 of 10