Cmpt 214 programming principles and practice assignment


Programming Principles and Practice Assignment

Lab 7 - General C Programming

Q1. Download the file lab7q1.c from attachment containing source code which comes from the in-class examples for topic 12 (Intermediate C Constructs). You do not need to show a log of the download in your lab7.txt file. Then perform the following step-by-step directions.

(a) Copy lab7q1.c to lab7q1_modified.c.

Add to lab7q1_modified.c a single statement that will add to the table produced by the program the size of a variable that is of type unsigned long *. Make sure the additional output is the last line of the table. The output is to be two columns in width (the type and the size in bytes), and must line up with the existing columns of output produced by the program. Do not show a log of editing in your lab7.txt file. Perform a diff between lab7q1.c and lab7q1_modified.c.

(b) Demonstrate that your lab7q1_modified.c compiles cleanly even when using "-Wall -Wextra". Run your program from step 1a.

Q2. The sample program lab7q2.c compiles with warnings. If one ignores the warnings and executes the resultant executable file anyway, it results in a runtime error (the program "crashes"). Fix the program so that it compiles without warnings and executes without errors. Change only one statement in the program, and make the change as minimal as possible. Do not change any executable statements. Call your modified program lab7q2_fixed.c. You do not need to show a log of editing in your lab7.txt file. Instead, show a diff(1) of the original file and your lab7q2_fixed.c, a clean compilation of lab7q2_fixed.c, and the result of running the resultant binary executable file.

3. Suppose a C program contains the definition of an array of unsigned short integers. The array is defined as follows:

unsigned short us[6] = { 2, 4, 6, 8, 10, 12 };

Suppose that the following C statement

printf( "%zu %p\n", sizeof( unsigned short ), us );

, also in the program, produces

2 0x7fff5d11c866

(a) What would the following subsequent statement print?

printf( "%p\n", &us[3] );

(b) What would the following subsequent statement print?

printf( "%p\n", &us[3] + 2 );

(c) What would the following subsequent statement print?

printf( "%p\n", &us[3] + us[0] );

(d) Suppose that in this program there is also defined a variable ptr of type unsigned short *.

What will be printed as a result of the following subsequent statement combination?

ptr = &us[1]; printf( "%hu\n", *++ptr );

Lab 8 - General UNIX and Intermediate C Programming

Q1. UNIX/LINUX commands typically expect lines to be terminated by newline characters rather than carriage return characters. Lines terminated with carriage return characters can cause unusual and unexpected results.

(a) Download the file L8Q1.with_cr.txt.tar from the moodle pages for this lab. Use the tar(1) command to un-archive the file (to L8Q1.with_cr.txt).

(b) Use the pr(1) command in the same way as you did in question 2 of Lab 6 to output L8Q1.with_cr.txt (from part (a)) with line numbers.

(c) Use a UNIX pipeline involving hexdump -c and grep to show that L8Q1.with_cr.txt contains carriage return characters rather than, or in addition to, newline characters. The logic of the pipeline is that if grep(1) outputs anything (any lines), then L8Q1.with_cr.txt contains carriage returns. Note that hexdump -c represents a carriage return character by "\r" and newline by "\n" Make sure to have grep look for "\r" and not "r".

Q2. Download file lab8q2.c from the moodle pages for this lab. (You do not have to show a log of this in lab8.txt.) Copy it to lab8q2_modified.c. In lab8q2_modified.c complete the malloc() call to allocate an integer array a with the length of ten. Also complete the lines indicated in the file to obtain the lengths of arrays a and b using sizeof(). (You do not have to show a log of your editing in lab8.txt.) Once your editing is complete, run a diff(1) on the two files (lab8q2.c and lab8q2_modified.c) to show your modifications. Compile the code using options -Wall and -Wextra and show there are no warnings. Execute the program, and copy its output to lab8.txt.

In lab8.txt, explain why two different sizes are displayed to the console when your program runs. In other words, why are the lengths of arrays a and b not the same? If you do not understand, please ask for assistance from the lab instructor.

Q3. Download lab8q3_incomplete.c from the moodle pages for this lab. (You do not have to show a log of this in lab8.txt.) It is a revision of example_60.c from class. There is one statement in the program that is incomplete. That statement calculates the address of the (ith, jth) component of an element in a 2D vector. In this question you need to provide the correct expression and complete the statement. Note that you cannot use the unary & operator in your expression. The completed statement should only occupy a single line in the source file.

Copy lab8q3_incomplete.c to lab8q3.c and, with a text editor, complete the partial statement in lab8q3.c as described above. (You do not need to show a log of your editing in lab8.txt.) Compile lab8q3.c using -Wall and -Wextra and run it. Make sure that the results are what they should be. Finally, produce a diff(1) of the two files (lab8q3_incomplete.c and lab8q3.c) to indicate your modification.

Note - Only need the lab7 - Q1, Q2, Q3, and lab8 - Q1, Q2, Q3.

Attachment:- Assignment Files.rar

Request for Solution File

Ask an Expert for Answer!!
Computer Engineering: Cmpt 214 programming principles and practice assignment
Reference No:- TGS02807529

Expected delivery within 24 Hours