Program 1 (search.cpp) The search program takes two arguments: a string and a le
ID: 639635 • Letter: P
Question
Program 1 (search.cpp)
The search program takes two arguments: a string and a letter. The program determines if the letter is found in the string. For example, running "search cat a" should print out that "a is found in cat" while "search cat z" should print that "z is not found in cat". However, the code is buggy and will always say that the letter is not found in the string.
1 First, compile the code by typing in "make search".
2 Start the debugger by typing in "gdb ./search".
3 Then, run the search program by typing in "run cat a". This will cause the entire program to run to the end. This is not useful if you are trying to debug it. You will need to add breakpoints to have it stop at strategic places.
4 The simplest breakpoint to add is at the beginning of the main function. Type in "break main" to do this. Now type "run cat a".
5 You will now use the inspect command to print out the values of variables. Question 1: What are the values of the following parameters to the main() function? (1 point)
1 argc
2 argv[0]
3 argv[1]
4 argv[2]
6 You can print them out by, for instance, typing in "inspect argc". These arguments to main are called command line arguments.
7 Then type in "step" and hit return to advance the debugger to the next line. Keep doing this until you reach the line with the for loop. You can hit the return key to repeat the last command. Another option if you get tired of stepping through the code, is to set a breakpoint at line 21 by typing in "break 21" and then using the "continue" command.
8 Question 2: How do you fix the search program? Hint: use the debugger to inspect the value of the variable found at each iteration of the for loop. (1 point)
Program 2 (average.cpp)
Compile average.cpp by calling "make average" and then running with "./average". The average should be 2.5 but this is definitely not what the program prints out. You will also debug this program.
1 Start the debugger by typing in "gdb ./average".
2 Run the program by just typing in "run". Note that there are no command line arguments.
3 Question 3: How do you fix the average program? If you are not sure what the bug is, use the debugger to inspect variables such as average and result. (2 points)
Program 3 (mystery.cpp)
Compile mystery.cpp by calling "make mystery" and then running with "./mystery". The mystery function does not have bugs. It should be fairly obvious what it does. However, it is a recursive function and works in a different way than what you are probably used to. The purpose of this part of the lab is to practice working with call stacks and to show you how the mystery function actually works.
1 Start the debugger by typing in "gdb ./mystery".
2 Put a breakpoint at main by typing in "break main". Then, put a second breakpoint at line 24 by typing in "break 24" (this is the line that returns result). Run the program by just typing in "run".
3 You will watch what happens by using "step" to execute things one line at a time. Step into the mystery function, then step into the mystery_helper function. Keep using "step" until you hit the breakpoint. Note: At some point, the debugger goes into the string library functions itself (you will see output like below). If this happens, keep hitting return until you get back to the C++ code in mystery.cpp.
4 Once you hit line 24, type in "bt" for backtrace.
5 Question 4: How many times was mystery_helper called? What were the arguments passed to it each time? (2 points)
6 Question 5: What does the mystery function do? Describe the process of how it does what it does? (4 points)
average.cpp
mystery.cpp
makefile
Explanation / Answer
Miscellaneous Options
-D <string>, --debug=<string>
set debug options to <string> (see USAGE whatfor <string> is).
-O <string>, --diff=<string>
if <string> is a file then print diffs between <string> and input file otherwise use as a prefix and print diffs. See USAGE for more information on usage.
-t <string>, --language=<string>
treat input as language. Use --help=language for list.
-U <string>, --username=<string>
set username for coversheet
-H, --help[=<string>]
Type help information and given the <string> option it lists options for the following topics:
all-options
- all options
misc-options - miscellaneous options
page-furniture-options - page furniture options
text-format-options - text formatting options
print-options - options that select what to print
page-format-options - page format options
output-options - options affecting output
language - languages
prompt - format for --print-pages string
debug - format for --debug string
header - format for header & footer strings
report - file format for --language=report input
environment - environment vars used
-V, --version
Type version information
-N, --use-environment
use environment variables
--N, --ignore-environment
don't use values from environment, such as time, $USER, etc. This is for test purposes, to make test results more reproducible
-R 1, --ps-level-one
Use level one postscript.
-R 2, --ps-level-two
Use level two postscript.
Text Formatting
-b, --no-page-break-after-function
don't print page breaks at the end of functions
--b, --page-break-after-function
print page breaks at the end of functions
-W, --no-intelligent-line-wrap
Wrap lines at exactly the line-wrap column
--W, --intelligent-line-wrap
Wrap lines intelligently at significant characters, such as a space
-L <number>, --minimum-line-length=<number>
minimum line length permitted by intelligent line wrap (default 10)
-T <number>, --tabsize=<number>
set tabsize (default 8)
-E, --ignore-form-feeds
don't expand form feed characters to new page
--E, --form-feeds
expand form feed characters to new page
-p <number>, --point-size=<number>
specify point size (default 10)
-g <number>, --leading=<number>
specify interline gap in points (default 1)
-w <number>, --line-wrap=<number>
specify the line-wrap column.
--no-line-wrap
turn off line-wrap
-l <number>, --page-length=<number>
specify number of lines on a page, point size is calculated appropriately
Page Formatting
-I, --no-holepunch
don't leave space for holepunch at the side of each page
--I, --holepunch
leave space for holepunch at the side of each page
-J, --no-top-holepunch
don't leave space for holepunch at the top of each page
--J, --top-holepunch
leave space for holepunch at the top of each page
-o p, --portrait
print using portrait orientation
-o l, --landscape
print using landscape orientation
-S 1, --single-sided
print single-sided
-S 2, --double-sided
print double-sided
-1, --one-up
print 1-on-1 (default)
-2, --two-up
print 2-on-1
-3, --two-tall-up
print 2-on-1 at 4-on-1 pointsize
-4, --four-up
print 4-on-1
Page Furniture
-X <string>, --left-header=<string>
specify string for left side of header
-x <string>, --left-footer=<string>
specify string for left side of footer
-Y <string>, --center-header=<string>
specify string for center of header
-y <string>, --center-footer=<string>
specify string for center of footer
-Z <string>, --right-header=<string>
specify string for right side of header
-z <string>, --right-footer=<string>
specify string for right side of footer
-m <string>, --message=<string>
message to be printed over page. Will be printed in a very large, light gray font. There are a few escapes as a replacement for <string>; see USAGE for more information.
-G <number>, --gray-bands=<number>
Emulate the old lineprinter paper with gray bands across each page. The value of <number> gives the width of the bands and the gaps between them
-K, --headers
include the header on each page
--K, --no-headers
suppress the header on each page
-k, --footers
include the footer on each page
--k, --no-footers
suppress the footer on each page
-u, --page-furniture-lines
Draw lines around header & footer boxes and down left side of page.
--u, --no-page-furniture-lines
Don't draw lines around header & footer boxes or down left side of page.
-i, --no-braces-depth
exclude the braces depth count
--i, --braces-depth
include the braces depth count
-n, --no-line-numbers
exclude the line number count
--n, --line-numbers
include the line number count
Output Options
Per default trueprint will send the postscript output to 'lp' or 'lpr' and it will explicitly set the destination to the value of the environment variable '$PRINTER'.
-d <string>, --printer=<string>
use printer <string>
-P <string>, --printer=<string>
use printer <string>
-s <string>, --output=<string>
send output to filename <string>; use - for stdout
-r, --redirect-output
redirect output to .ps file named after first filename
--r, --no-redirect-output
don't redirect output
-c <number>, --copies=<number>
specify number of copies to be printed
Print Selection
-C, --no-cover-sheet
don't print cover sheet
--C, --cover-sheet
print cover sheet
-A <selection>, --print-pages=<selection>
specify list of pages to be printed (see USAGE whatfor <selection> is).
-a, --no-prompt
don't prompt for each page, whether it should be printed or not
--a, --prompt
prompt for each page, whether it should be printed or not
-F, --no-file-index
don't print file index
--F, --file-index
print file index
-f, --no-function-index
don't print function index
--f, --function-index
print function index
-B, --no-print-body
don't print body of text
--B, --print-body
print body of text
Usage
The usage of the '-D <string>', '--debug=<string>' is as follows:
Each letter turns on a set of debugging messages, and the corresponding digit indicates the level of messages, with 1 indicating the least number of messags and 9 turning on all messages. The letter '@' can be used to turn on all areas, so '--debug=@9' will turn on all messages. Use '--help=debug' for details of what letters turn on what areas.
Note that this feature is not uniformly implemented - messages were only typically added where they were needed. Also note that messages will not be generated in code prior to the code that sets the debug string!
The usage of the '-O <string>', '--diff=<string>' option looks as follows:
You specify the old version of the file or files with '--diff=<pathname>'. If <pathname> suffixed with the current filename is a valid file then the differences between <pathname>/<filename> are displayed. Alternatively, if <pathname> is a file and you're printing one file, then the differences between <pathname> and the current file are displayed. Examples how to make it are shown below:
trueprint --diff=../old- this.c that.c
will print the differences between ../old-this.c and this.c, and ../old-that.c and that.c.
trueprint --diff=../old/ this.c that.c
will print the differences between ../old/this.c and this.c, and ../old/that.c and that.c. Note that the trailing / is important.
trueprint --diff=this.c that.c
will print the differences between this.c and that.c.
The usage of the '-A <selection>', '--print-pages=<selection>' option looks as follows:
<selection> consists of a comma-seperated list of page selections:
'<number>'
-- print the specific page
'<number>-<number>' -- Print all pages between, and including, the specified pages
'<function-name>' -- Print all pages that include function-name.
'd' -- Print all pages that have changed (this is only useful with the '--diff' (see above) option.
'D' -- Print all pages containing functions that have changed (this is only useful with the '--diff' (see above) option.
'f' -- Print the function index
'F' -- Print the file index.
For example,
--print-pages=1-5,main,f
will print the function index, pages 1 to 5, and all the pages for the function 'main'.
If you specify '--prompt'then trueprint will prompt you for each page, whether or not it should print that page. It will give you information such as the current filename, current functionname, page number, and so on. The possible responses are:
'y'
-- Print this page.
'y<number>' -- Print <number> pages.
'y*' -- Print all the remaining pages.
'n' -- Skip this page
'n<number>' -- Skip <number> pages.
'n*' -- Skip all the remaining pages.
'p<selection>' -- Print all remaining pages that match <selection>, where <selection> is in the format defined above.
'?' -- Print a help message.
The '--message=<string>' option takes a few % escapes for <string> which are listed below:
'%%'
-- Print a simple %.
'%m' -- Print the current month of the year as a number, e.g. 05.
'%d' -- Print the current day of the month, e.g. 01.
'%y' -- Print the current year, e.g. 1999
'%D' -- Print the date in the format mm/dd/yy.
'%L' -- Print the date and time in long format, e.g. Fri Oct 8 11:49:51 1999
'%c' -- Print the last modified date of the current file in the format mm/dd/yy.
'%C' -- Print the last modified date and time of the current file in long format, e.g. Fri Oct 8 11:49:51 1999
'%H' -- Print the current hour.
'%M' -- Print the current minute.
'%S' -- Print the current second.
'%T' -- Print the time in the format HH:MM:SS.
'%j' -- Print the day of the year, e.g. 095.
'%w' -- Print the day of the week, e.g. Sunday.
'%a' -- Print the abbreviated day of the week, e.g. Sun.
'%h' -- Print the abbreviated month name, e.g. Mar.
'%r' -- Print the time in am/pm notation, e.g. 10:45pm.
'%p' -- Print the page number in the current file.
'%P' -- Print the overall page number.
'%f' -- Print the total number of pages of the current file.
'%F' -- Print the total number of pages.
'%n' -- Print the current filename.
'%N' -- Print the current function name.
'%l' -- Print the username of the current user.
Default Values are:
'left-header' '%L'
'center-header' '%N'
'right-header' 'Page %P of %F'
'left-footer' '%L'
'center-footer' '%n %p'
'right-footer' 'Page %P of %F'
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.