[{"content":"Chapter 2 1. Create and run Kernighan and Ritchie’s famous “hello, world” program:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { printf(\u0026#34;hello, world\\n\u0026#34;); } Do you get a warning message from the compiler? If so, what’s needed to make it go away?\nSolution:\nTo reproduce this exercise you need create a new file and name it with .c extension. In the same directory the file is, you compile the program with gcc (if using GNU linux, or find the compiler available for your system.) open the terminal emulator and run the following line to compile the program: gcc hello.c, to execute run: ./a.out. Author\u0026rsquo;s asks about if we gat some warning message from the compiler, this is because in some older compilers, left the main function without a return statement make throw a warning. As using modern compilers such Clang, there\u0026rsquo;s no warning message, as long as main has iint as its return type.\nAccording to the C standard (since C99):\nIf control reaches the end of main() without a return statement, the effect is as if return 0; had been executed.\nIn some older compilers there\u0026rsquo;s a warning that appears when running the program without the return statement.\n2. Consider the following program:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { printf(\u0026#34;Parkinson\u0026#39;s Law:\\nWork expands so as to \u0026#34;); printf(\u0026#34;fill the time\\n\u0026#34;); printf(\u0026#34;available for its completion.\\n\u0026#34;); return 0; } (a) Identify the directives and statements in this program. Solution:\nDirectives: Instructions to the preprocessor, usually starting with #. In this program the only directive is #include \u0026lt;stdio.h\u0026gt; Statement: Instructions that the program executes, typically ending with a semicolon. printf(\u0026#34;Parkinson\u0026#39;s Law:\\nWork expands so as to \u0026#34;); printf(\u0026#34;fill the time\\n\u0026#34;); printf(\u0026#34;available for its completion.\\n\u0026#34;); return 0; (b) What out put does the program produce? Solution: The program will output the following message:\nParkinson\u0026rsquo;s Law: Work expands so as to fill the time available for its completion.\n3. Condense the dweight.c program by (1) replacing the assignments to height, length, and width with initializers and (2) removing the weight variable, instead calculating (volume + 165) / 166 within the last printf.\ndweight.c:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { int height, length, width, volume, weight; height = 8; length = 12; width = 10; volume = height * length * width; weight = (volume + 165) / 166; printf(\u0026#34;Dimensions: %dx%dx%d\\n\u0026#34;, length, width, height); printf(\u0026#34;Volume (cubic inches): %d\\n\u0026#34;, volume); printf(\u0026#34;Dimensional weight (pounds): %d\\n\u0026#34;, weight); return 0; } Solution:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { int height = 8, length = 12, width = 10; int volume = height * length * width; printf(\u0026#34;Dimensions: %dx%dx%d\\n\u0026#34;, length, width, height); printf(\u0026#34;Volume (cubic inches): %d\\n\u0026#34;, volume); printf(\u0026#34;Dimensional weight (pounds): %d\\n\u0026#34;, (volume + 165) / 166); return 0; } 4. Write a program that declares several int and float variables—without initializing them— and then prints their values. Is there any pattern to the values? (Usually there isn’t.)\nSolution:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { float a, b, c; int x, y, z; printf(\u0026#34;Uninitialized float values: %f %f %f\\n\u0026#34;,a, b, c); printf(\u0026#34;Uninitialized int values: %d %d %d\\n\u0026#34;, x, y , z); return 0; } There shouldn\u0026rsquo;t be any predictable pattern to these values because they depend on the state of the memory at runtime. That means that the values are garbage values previously in the memory location addressed by these variables.\n5. Which of the following are not legal C identifiers? (a) 100_bottles (b) _100_bottles (c) one__hundred__bottles (d) bottles_by_the_hundred_\nSolution:\nIn C, identifiers (names for variables, functions, etc.) must follow these rules:\nThey can only contain letters (A-Z or a-z), digits (0-9), and underscores (_). They must not begin with a digit. They cannot be a reserved keyword, though that’s not relevant here. Evaluating the options we have that: (a) is a illegal identifier. 6. Why is it not a good idea for an identifier to contain more than one adjacent underscore (as in current___balance, for example)?\nSolution:\nThat\u0026rsquo;s because the C standard (ISO C) reserves identifiers with double underscores (__) for the compiler and standard library.\n7. Which of the following are keywords in C? 1. (a) for 2. (b) If 3. (c) main 4. (d) printf 5. (e) while\nSolution: Options: (a), (c) and (e)\n8. How many tokens are there in the following statement? answer=(3*q-p*p)/3;\nSolution:\nanswer = ( 3 * q - p * p ) / 3 ; 14 tokens in total.\n9. Insert spaces between the tokens in Exercise 8 to make the statement easier to read.\nSolution: anwser = ( 3 * q - p * p ) / 3;\n10. In the dweight.c program (Section 2.4), which spaces are essential?\nSolution: The only space needed is for the directive #include that needs a new line.\nProgramming Projects 1. Write a program that uses printf to display the following pattern on the screen:\n* * * * * * * * Solution:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { printf(\u0026#34; *\\n *\\n *\\n *\\n* *\\n * *\\n *\\n\u0026#34;); return 0; } 2. Write a program that computes the volume of a sphere with a 10-meter radius, using the formula $v = \\frac{4}{3}πr3$. Write the fraction $\\frac{4}{3}$ as 4.0f/3.0f. (Try writing it as 4/3. What happens?) Hint: C doesn’t have an exponentiation operator, so you’ll need to multiply r by itself twice to compute r3.\nSolution:\n#include \u0026lt;stdio.h\u0026gt; #define PI 3.14159265364 #define SPHERE_VOLUME(v) ( (4.0f/3.0f) * PI * (v * v * v)) int main(void) { printf(\u0026#34;Sphere volume = %.2f\\n m³\u0026#34;, SPHERE_VOLUME(10.00)); return 0; } When 4/3 instead of 4.0/3.0f what happens is a integer division which evaluates to 1. That means the fractional part is discarded), not 1.3333\u0026hellip;. Making at least one operand as float, forces the compiler to generate the expression as floating-point division.\n3. Modify the program of Programming Project 2 so that it prompts the user to enter the radius of the sphere.\nSolution:\n#include \u0026lt;stdio.h\u0026gt; #define PI 3.14159265364 #define SPHERE_VOLUME(v) ((4.0f/3.0f)*PI*(v*v*v)) int main(void) { float radius; scanf(\u0026#34;%f\u0026#34;,\u0026amp;radius); printf(\u0026#34;sphere volume = %.2f\\n\u0026#34;, SPHERE_VOLUME(radius)); return 0; } 4. Write a program that asks the user to enter a dollars-and-cents amount, then displays the amount with 5% tax added:\nEnter an amount: 100.00 With tax added: $105.00 Solution:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { float amount; printf(\u0026#34;Enter an amount: \u0026#34;); scanf(\u0026#34;%f\u0026#34;,\u0026amp;amount); amount += amount*0.05f; printf(\u0026#34;With tax added: %.2f\\n\u0026#34;, amount); return 0; } 5. Write a program that asks the user to enter a value for x and then displays the value of the following polynomial: $3x^5 + 2x^4 -5x^3 -x^2 + 7x - 6$ Hint: C doesn\u0026rsquo;t have an exponentiation operator, so you\u0026rsquo;ll need to multiply $x$ by itself repeatedly in order to compute the power of $x$. (For example, x*x*x is x cubed.)\nSolution:\n#include \u0026lt;stdio.h\u0026gt; #define f(x) ((3*(x*x*x*x*x)) + (2*(x*x*x*x)) - (5*(x*x*x)) - (x*x) + (7*x) - 6) int main(void) { int x; printf(\u0026#34;Enter a value for x: \u0026#34;); scanf(\u0026#34;%d\u0026#34;,\u0026amp;x); printf(\u0026#34;f(%d) = %d\\n\u0026#34;, x, f(x)); return 0; } 6. Modify the program of Programming Project 5 so that the polynomial is evaluated using the following formula: $((((3x + 2)x – 5)x – 1)x + 7)x – 6$ Note that the modified program performs fewer multiplications. This technique for evaluating polynomials is known as [[Horner\u0026rsquo;s method]].\nSolution:\n#include \u0026lt;stdio.h\u0026gt; #define f(x) (((((3*x+2)*x - 5)*x -1)*x + 7)*x - 6) int main(void) { int x; printf(\u0026#34;Enter a value for x: \u0026#34;); scanf(\u0026#34;%d\u0026#34;, \u0026amp;x); printf(\u0026#34;f(%d) = %d\\n\u0026#34;, x,f(x)); return 0; } 7. Write a program that asks the user to enter a U.S. dollar amount and then shows how to pay that amount using the smallest number of $20, $10, $5, and $1 bills:\nEnter a dollar amount: 93 $20 bills: 4 $10 bills: 1 $5 bills: 0 $1 bills: 3 Hint: Divide the amount by 20 to determine the number of $20 bills needed, and then reduce the amount by the total value of the $20 bills. Repeat for the other bill sizes. Be sure to use integer values throughout, not floating-point numbers.\nSolution:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { int amount, remaining; int twenties, tens, fives, ones; printf(\u0026#34;Enter a dollar amount: \u0026#34;); scanf(\u0026#34;%d\u0026#34;,\u0026amp;amount); twenties = amount / 20; remaining = amount - 20*twenties; tens = remaining / 10; remaining = remaining - 10*tens; fives = remaining / 5; remaining = remaining - 5*fives; ones = remaining / 1; printf(\u0026#34;$20 bills: %d\\n$10 bills: %d\\n$5 bills: %d\\n$1 bills: %d\\n\u0026#34;,notes_of_20,notes_of_10,notes_of_5,notes_of_1); return 0; } 8. Write a program that calculates the remaining balance on a loan after the first, second, and third monthly payments:\nEnter amount of loan: 20000.00 Enter interest rate: 6.0 Enter monthly payment: 386.66 Balance remaining after first payment: $19713.34 Balance remaining after second payment: $19425.25 Balance remaining after third payment: $19135.71 Display each balance with two digits after the decimal point. Hint: Each month, the balance is decreased by the amount of the payment, but increased by the balance times the monthly interest rate. To find the monthly interest rate, convert the interest rate entered by the user to a percentage and divide it by 12.\nSolution:\n#include \u0026lt;stdio.h\u0026gt; #define tax(v) ((v/100.00f) / 12.00f) int main(void) { double loan, rate, payment; double balance; printf(\u0026#34;Enter amount of loan: \u0026#34;); scanf(\u0026#34;%lf\u0026#34;,\u0026amp;loan); printf(\u0026#34;Enter interest rate: \u0026#34;); scanf(\u0026#34;%lf\u0026#34;, \u0026amp;rate); printf(\u0026#34;Enter monthly payment: \u0026#34;); scanf(\u0026#34;%lf\u0026#34;,\u0026amp;payment); balance = loan; balance = balance + (loan * tax(rate) ) - payment; printf(\u0026#34;Balance remaining after first payment: %.2lf\\n\u0026#34;, balance); balance = balance + (loan * tax(rate) ) - payment; printf(\u0026#34;Balance remaining after first payment: %.2lf\\n\u0026#34;, balance); balance = balance + (loan * tax(rate) ) - payment; printf(\u0026#34;Balance remaining after third payment: %.2lf\\n\u0026#34;, balance); return 0; } ","permalink":"https://joaonevessoares.github.io/c-modern-approach-solutions/posts/chapter2/","summary":"\u003ch1 id=\"chapter-2\"\u003eChapter 2\u003c/h1\u003e\n\u003cp\u003e\u003cstrong\u003e1\u003c/strong\u003e. Create and run Kernighan and Ritchie’s famous “hello, world” program:\u003c/p\u003e\n\u003cdiv class=\"highlight\"\u003e\u003cpre tabindex=\"0\" class=\"chroma\"\u003e\u003ccode class=\"language-c\" data-lang=\"c\"\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"cp\"\u003e#include\u003c/span\u003e \u003cspan class=\"cpf\"\u003e\u0026lt;stdio.h\u0026gt;\u003c/span\u003e\u003cspan class=\"cp\"\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"cp\"\u003e\u003c/span\u003e\u003cspan class=\"kt\"\u003eint\u003c/span\u003e \u003cspan class=\"nf\"\u003emain\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"kt\"\u003evoid\u003c/span\u003e\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"p\"\u003e{\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\t\u003cspan class=\"nf\"\u003eprintf\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\u003cspan class=\"s\"\u003e\u0026#34;hello, world\u003c/span\u003e\u003cspan class=\"se\"\u003e\\n\u003c/span\u003e\u003cspan class=\"s\"\u003e\u0026#34;\u003c/span\u003e\u003cspan class=\"p\"\u003e);\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003cspan class=\"line\"\u003e\u003cspan class=\"cl\"\u003e\u003cspan class=\"p\"\u003e}\u003c/span\u003e\n\u003c/span\u003e\u003c/span\u003e\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eDo you get a warning message from the compiler? If so, what’s needed to make it go away?\u003c/p\u003e\n\u003chr\u003e\n\u003cp\u003e\u003cstrong\u003eSolution\u003c/strong\u003e:\u003c/p\u003e\n\u003cp\u003eTo reproduce this exercise you need create a new file and name it with \u003ccode\u003e.c\u003c/code\u003e extension. In the same directory the file is, you compile the program with gcc (if using GNU linux, or find the compiler available for your system.) open the terminal emulator and run the following line to compile the program: \u003ccode\u003egcc hello.c\u003c/code\u003e, to execute run: \u003ccode\u003e./a.out\u003c/code\u003e. Author\u0026rsquo;s asks about if we gat some warning message from the compiler, this is because in some older compilers, left the main function without a return statement make throw a warning. As using modern compilers such Clang, there\u0026rsquo;s no warning message, as long as main has iint as its return type.\u003c/p\u003e","title":"Chapter 2 Exercises"},{"content":"Chapter 3 1. What output do the following calls of printf produce?\n(a) printf(\u0026quot;%6d,%4d\u0026quot;, 86, 1040); (b) printf(\u0026quot;%12.5e\u0026quot;, 30.253); (c) printf(\u0026quot;%.4f\u0026quot;, 83.162); (d) printf(\u0026quot;%-6.2g\u0026quot;, .0000009979);\nSolution: (a) The first one will output 86 with 4 trailing spaces the second will display only 1040. (b) 3.02530e+01 (c) 83.1620 (d) 1.0e-06\n2. Write calls of printf that display a float variable x in the following formats.\n(a) Exponential notation; left-justified in a field of size 8; one digit after the decimal point. (b) Exponential notation; right-justified in a field of size 10; six digits after the decimal point. (c) Fixed decimal notation; left-justified in a field of size 8; three digits after the decimal point. (d) Fixed decimal notation; right-justified in a field of size 6; no digits after the decimal point.\nSolution:\nUsing PI as float constant for exemplification (a)\nprintf(\u0026#34;%-8.1e\\n\u0026#34;, M_PI); (b)\nprintf(\u0026#34;%10.6e\\n\u0026#34;, M_PI); (c)\nprintf(\u0026#34;%-8.3f\\n\u0026#34;, M_PI); (d)\nprintf(\u0026#34;%6.0f\\n\u0026#34;,M_PI); 3. For each of the following pairs of scanf format strings, indicate whether or not the two strings are equivalent. If they’re not, show how they can be distinguished. (a) \u0026quot;%d\u0026quot; versus \u0026quot; %d\u0026quot; (b) \u0026quot;%d-%d-%d\u0026quot; versus \u0026quot;%d -%d -%d\u0026quot; (c) \u0026quot;%f\u0026quot; versus \u0026quot;%f \u0026quot; (d) \u0026quot;%f,%f\u0026quot; versus \u0026quot;%f, %f\u0026quot;\nSolution: (a) both are equivalent (b) both are equivalent (c) both are equivalent (d) they not equivalent\n4. Suppose that we call scanf as follows: scanf(\u0026quot;%d%f%d\u0026quot;, \u0026amp;i, \u0026amp;x, \u0026amp;j); If the user enters 10.3 5 6 what will be the values of i, x, and j after the call? (Assume that i and j are int variables and x is a float variable.)\nSolution: 10, 0.3, 5\nReason: the first %d reads 10.3 and drops the fractional part, the upcoming %f reads the .3 value, and the last %d reads 5 and the value 6 is ignored by the scanf.\n5. Suppose that we call scanf as follows: scanf(\u0026quot;%f%d%f\u0026quot;, \u0026amp;x, \u0026amp;i, \u0026amp;y); If the user enters 12.3 45.6 789 what will be the values of x, i, and y after the call? (Assume that x and y are float variables and i is an int variable.)\nSolution: 12.3, 45, 0.6\n6. Show how to modify the addfrac.c program of Section 3.2 so that the user is allowed to enter fractions that contain spaces before and after each / character.\naddfrac.c:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { int num1, denom1, num2, denom2, result_num, result_denom; printf(\u0026#34;Enter first fraction: \u0026#34;); scanf(\u0026#34;%d/%d\u0026#34;, \u0026amp;num1, \u0026amp;denom1); printf(\u0026#34;Enter second fraction: \u0026#34;); scanf(\u0026#34;%d/%d\u0026#34;, \u0026amp;num2, \u0026amp;denom2); result_num = num1 * denom2 + num2 * denom1; result_denom = denom1 * denom2; printf(\u0026#34;The sum is %d/%d\\n\u0026#34;, result_num, result_denom); return 0; } Solution:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { int num1, denom1, num2, denom2, result_num, result_denom; printf(\u0026#34;Enter first fraction: \u0026#34;); scanf(\u0026#34;%d / %d\u0026#34;, \u0026amp;num1, \u0026amp;denom1); printf(\u0026#34;Enter second fraction: \u0026#34;); scanf(\u0026#34;%d / %d\u0026#34;, \u0026amp;num2, \u0026amp;denom2); result_num = num1 * denom2 + num2 * denom1; result_denom = denom1 * denom2; printf(\u0026#34;The sum is %d/%d\\n\u0026#34;, result_num, result_denom); return 0; } Programming Projects 1. Write a program that accepts a date from the user in the form mm/dd/yyyy and then displays it in the form yyyymmdd: Enter a date (mm/dd/yyyy): 2/17/2011 You entered the date 20110217\nSolution:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { int dd,yyy,mm; scanf(\u0026#34;%d/%d/%d\u0026#34;,\u0026amp;mm,\u0026amp;dd,\u0026amp;yyy); printf(\u0026#34;You entered the date %04d%02d%02d\\n\u0026#34;,yyy,mm,dd); return 0; } 2. Write a program that formats product information entered by the user. A session with the program should look like this:\nEnter item number: 583 Enter unit price: 13.5 Enter purchase date (mm/dd/yyyy): 10/24/2010 output:\nItem Unit Price Purchase Date 583 $ 13.50 10/24/2010 Solution:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { int it, mm, dd, yyyy; float price; printf(\u0026#34;Enter a item number: \u0026#34;); scanf(\u0026#34;%d\u0026#34;, \u0026amp;it); printf(\u0026#34;Enter unit price: \u0026#34;); scanf(\u0026#34;%f\u0026#34;, \u0026amp;price); printf(\u0026#34;Enter purchase date (mm/dd/yyyy): \u0026#34;); scanf(\u0026#34;%d/%d/%d\u0026#34;,\u0026amp;mm,\u0026amp;dd,\u0026amp;yyyy); printf(\u0026#34;Item\\tUnit Price\\tPurchase Date\\n%d\\t$ %.2f \\t\\t %d/%d/%d\\n\u0026#34;,it,price, mm,dd,yyyy); return 0; } 3. Books are identified by an International Standard Book Number (ISBN). ISBNs assigned after January 1, 2007 contain 13 digits, arranged in five groups, such as 978-0-393-97950-3. (Older ISBNs use 10 digits.) The first group (the GS1 prefix) is currently either 978 or 979. The group identifier specifies the language or country of origin (for example, 0 and 1 are used in English-speaking countries). The publisher code identifies the publisher (393 is the code for W. W. Norton). The item number is assigned by the publisher to identify a specific book (97950 is the code for this book). An ISBN ends with a check digit that’s used to verify the accuracy of the preceding digits. Write a program that breaks down an ISBN entered by the user:\nEnter ISBN: 978-0-393-97950-3 GS1 prefix: 978 Group identifier: 0 Publisher code: 393 Item number: 97950 Check digit: 3 Note: The number of digits in each group may vary; you can’t assume that groups have the lengths shown in this example. Test your program with actual ISBN values (usually found on the back cover of a book and on the copyright page).\nSolution:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { int gs1, gi, pc, in, cd; printf(\u0026#34;Enter ISBN: \u0026#34;); scanf(\u0026#34;%d-%d-%d-%d-%d\u0026#34;, \u0026amp;gs1, \u0026amp;gi,\u0026amp;pc, \u0026amp;in, \u0026amp;cd); printf(\u0026#34;GS1 prefix: %d\\n\u0026#34;, gs1); printf(\u0026#34;Group identifier: %d\\n\u0026#34;, gi); printf(\u0026#34;Publisher code: %d\\n\u0026#34;, pc); printf(\u0026#34;Item number: %d\\n\u0026#34;,in); printf(\u0026#34;Check digit: %d\\n\u0026#34;,cd); return 0; } 4. Write a program that prompts the user to enter a telephone number in the form (xxx) xxx- xxxx and then displays the number in the form xxx.xxx.xxx:\nEnter phone number [(xxx) xxx-xxxx]: (404) 817-6900 You entered 404.817.6900 Solution:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { int prefix, num, posfix; printf(\u0026#34;Enter a phone number [(xxx) xxx-xxxx]: \u0026#34;); scanf(\u0026#34;(%d) %d-%d\u0026#34;,\u0026amp;prefix, \u0026amp;num,\u0026amp;posfix); printf(\u0026#34;%d.%d.%d\\n\u0026#34;, prefix,num,posfix); return 0; } 5. Write a program that asks the user to enter the numbers from 1 to 16 (in any order) and then displays the numbers in a 4 by 4 arrangement, followed by the sums of the rows, columns, and diagonals:\nEnter the numbers from 1 to 16 in any order 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 Rows sums: 34 34 34 34 Column sums: 34 34 34 34 Diagonal sums: 34 34 If the row, column, and diagonal sums are all the same (as they are in this example), the numbers are said to form a magic square. The magic square shown here appears in a 1514 engraving by artist and mathematician [[Albrecht Dürer]]. (Note that the middle numbers in the last row give the date of the engraving.)\nSolutions:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { int n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16; printf(\u0026#34;Enter the numbers from 1 to 16 in any order: \u0026#34;); scanf(\u0026#34;%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\u0026#34;, \u0026amp;n1, \u0026amp;n2, \u0026amp;n3, \u0026amp;n4, \u0026amp;n5, \u0026amp;n6, \u0026amp;n7, \u0026amp;n8, \u0026amp;n9, \u0026amp;n10, \u0026amp;n11, \u0026amp;n12, \u0026amp;n13, \u0026amp;n14, \u0026amp;n15, \u0026amp;n16); printf(\u0026#34;%2d %2d %2d %2d\\n\u0026#34;,n1,n2,n3,n4); printf(\u0026#34;%2d %2d %2d %2d\\n\u0026#34;,n5,n6,n7,n8); printf(\u0026#34;%2d %2d %2d %2d\\n\u0026#34;,n9,n10,n11,n12); printf(\u0026#34;%2d %2d %2d %2d\\n\u0026#34;,n13,n14,n15,n16); printf(\u0026#34;Rows sums: %d %d %d %d\\n\u0026#34;,n1+n2+n3+n4,n5+n6+n7+n8,n9+n10+n11+n12,n13+n14+n15+n16); printf(\u0026#34;Column sums: %d %d %d %d\\n\u0026#34;,n1+n5+n9+n13,n2+n6+n10+n14,n3+n7+n11+n15,n4+n8+n12+n16); printf(\u0026#34;Diagonal sums: %d %d\\n\u0026#34;,n1+n6+n11+n16,n4+n7+n10+n13); return 0; } 6. Modify the addfrac.c program of Section 3.2 so that the user enters both fractions at the same time, separated by a plus sign:\nEnter two fractions separated by a plus sign: 5/6+3/4 The sum is 38/24 addfrac.c:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { int num1, denom1, num2, denom2, result_num, result_denom; printf(\u0026#34;Enter first fraction: \u0026#34;); scanf(\u0026#34;%d/%d\u0026#34;, \u0026amp;num1, \u0026amp;denom1); printf(\u0026#34;Enter second fraction: \u0026#34;); scanf(\u0026#34;%d/%d\u0026#34;, \u0026amp;num2, \u0026amp;denom2); result_num = num1 * denom2 + num2 * denom1; result_denom = denom1 * denom2; printf(\u0026#34;The sum is %d/%d\\n\u0026#34;, result_num, result_denom); return 0; } Solution:\n#include \u0026lt;stdio.h\u0026gt; int main(void) { int num1, denom1, num2, denom2, result_num, result_denom; printf(\u0026#34;Enter two fractions separated by a plus sign: \u0026#34;); scanf(\u0026#34;%d/%d+%d/%d\u0026#34;, \u0026amp;num1, \u0026amp;denom1, \u0026amp;num2, \u0026amp;denom2); result_num = num1 * denom2 + num2 * denom1; result_denom = denom1 * denom2; printf(\u0026#34;The sum is %d/%d\\n\u0026#34;, result_num, result_denom); return 0; } ","permalink":"https://joaonevessoares.github.io/c-modern-approach-solutions/posts/chapter3/","summary":"\u003ch1 id=\"chapter-3\"\u003eChapter 3\u003c/h1\u003e\n\u003cp\u003e\u003cstrong\u003e1\u003c/strong\u003e. What output do the following calls of printf produce?\u003c/p\u003e\n\u003cp\u003e(a) printf(\u0026quot;%6d,%4d\u0026quot;, 86, 1040);\n(b) printf(\u0026quot;%12.5e\u0026quot;, 30.253);\n(c) printf(\u0026quot;%.4f\u0026quot;, 83.162);\n(d) printf(\u0026quot;%-6.2g\u0026quot;, .0000009979);\u003c/p\u003e\n\u003cp\u003e\u003cstrong\u003eSolution\u003c/strong\u003e:\n(a) The first one will output 86 with 4 trailing spaces the second will display only 1040.\n(b) 3.02530e+01\n(c) 83.1620\n(d) 1.0e-06\u003c/p\u003e\n\u003chr\u003e\n\u003cp\u003e\u003cstrong\u003e2\u003c/strong\u003e. Write calls of printf that display a float variable x in the following formats.\u003c/p\u003e\n\u003cp\u003e(a) Exponential notation; left-justified in a field of size 8; one digit after the decimal point.\n(b) Exponential notation; right-justified in a field of size 10; six digits after the decimal point.\n(c) Fixed decimal notation; left-justified in a field of size 8; three digits after the decimal point.\n(d) Fixed decimal notation; right-justified in a field of size 6; no digits after the decimal point.\u003c/p\u003e","title":"Chapter III Solutions"}]