Cmpt 214 programming principles and practice labs lab -


Programming Principles and Practice Labs

Lab - Stack Frames and Bash Shell Scripting

Questions -

Q1. Build an executable factorial program from factorial.c, making sure that debugging information is included by the compiler. Run gdb on factorial. Set appropriate breakpoints, and then run factorial within gdb. Continue executing factorial until execution gets to the point pictured by factorial_stack.pdf. At that point, perform a backtrace command to show all the stack frames.

Then without advancing factorial, use appropriate gdb commands to output the values of variables f and i of function main() of factorial. You are NOT permitted to use "backtrace full" for this portion because that would be too easy!

Once you have outputted the desired values, you can exit from gdb.

Q2. Perform steps (a), (b), (c), (d) in succession using bash, and then answer questions (e) and (f). There is one mark for the combination of steps (a), (b), (c), (d).

(a) Execute the command "/bin/ls -l 'which sh' 'which bash'".

(b) Execute the command "set -x". Recall this turns on execution tracing.

(c) For a second time, execute the command "/bin/ls -l 'which sh' 'which bash'".

(d) Execute the command "set -".

(e) Based on the output in step 2a, explain the relationship between sh and bash on tuxworld. Write your answer in lab11.txt.

(f) Explain the operation of the command in part 2c. For example, what commands are being executed, and in what order? Your answer should focus on the output generated because of the -x option being turned on. Write your answer in lab11.txt

Lab - awk and Program Linking

Questions -

Q1. A file called country_data.txt is provided as supplementary data for this question. It consists of four columns: country name, population (in thousands), annual births (in thousands), and literacy rate (%). Before you start the tasks below, familiarize yourself with the contents of the file by examining its content with a program like more (1) or less (1). You do not need to show a log of this in lab12.txt.

(a) Like some data files you may encounter, country_data.txt is not formatted consistently-while most rows contain columns delimited by tabs, a few have one or more columns delimited by spaces. To verify this, use cut(1) to output only the third column (field), and redirect the output to cutout.txt. When you do this, do not change the default delimiter, which is a tab character. Then use awk(1) to output only the third column (field), redirecting the output to awkout.txt. Again, do not change the default delimiter, which in this case is one or more whitespace characters. Finally, use diff(1) to compare cutout.txt and awkout.txt. Submit a log of these steps. You do not need to submit awkout.txt or cutout.txt.

(b) Write an awk(1) command to "clean up" country_data.txt. In other words, make it consistent that a single tab character, and only that, is used to delimit columns (fields) in the file. You will need to read in the file using awk(1), then re-output all of the fields such that they are delimited only by single tabs. Place the output in a file called country_data_cleaned.txt. Your awk(1) script must be specified on the command line, rather than in a separate file. Then repeat question 1a with country_data_cleaned.txt to verify that the output of cut(1) and awk(1) are now identical when outputting the third column.

Use country_data_cleaned.txt to answer questions 1c, 1d, and 1e. You do not need to submit country_data_cleaned.txt.

(c) Write an awk(1) command that lists all the countries having a literacy rate less than 50%. Your command must output only the countries, not the other information in the table. Countries having an unknown literacy rate (represented by the "-" character) must not be included in your output. Your awk(1) code must be specified on the command line, rather than in a separate script.

Remember to use country_data_cleaned.txt as your input file.

(d) Write an awk(1) command that prints the total population (in thousands) of all of the countries whose names begin with "A" and end with "a". Your awk(1) code must be placed in a script called population.awk, and then invoked using "awk -f" on the command line. Upload population.awk as part of your lab submission.

Note that this question does not ask for the population (total or otherwise) for each country whose name matches the pattern. Rather the awk(1) command is to determine the sum (the total) across all the countries whose names match the pattern.

Remember to use country_data_cleaned.txt as your input file.

Hint: the anchors '^' and '$', if used in a regular expression matching against a field, mean "beginning of the field" and "end of the field", respectively.

(e) Write an awk(1) command that reads country_data_cleaned.txt, and outputs the same information except with an additional column. This fifth column contains the country's birth rate (number of children per woman per year). Assume the population of each country is 50% male and 50% female. You may use the default output format for decimal numbers (7 decimal places). Like the other columns, the additional column must be delimited by a tab character.

Your awk(1) code must be specified on the command line, rather than in a separate file, and the output of the command must be redirected to a file country_data_birthrate.txt. Upload your country_data_birthrate.txt file as part of your lab submission.

Q2. Use a pipeline involving date(1) and awk(1) to print out the current month and year in the following format:

Month: Nov

Year: 2011

Your awk(1) code must be specified on the command line, rather than in a separate script.

Attachment:- Assignment Files.rar

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Cmpt 214 programming principles and practice labs lab -
Reference No:- TGS02820564

Expected delivery within 24 Hours