Ask Computer Engineering Expert

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

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92850713

Have any Question?


Related Questions in Computer Engineering

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As