* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / homework / PS6.html
blobf261300ab273c4f4f06fbfe3f07914ce2c9ce044
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>Assignment #6</title>
10 </head>
11 <body>
12 <H1>Assignment #6</H1>
18 <B>Introductory C Programming
19 <br>
20 <br>
21 UW Experimental College
22 </B><br>
23 <br>
24 <B>Assignment #6
25 </B><p><B>Handouts:
26 </B></p><p><a href="PS6.html">Assignment #6</a>
27 <br><a href="PS5a.html">Assignment #5 Answers</a>
28 <br><a href="http://www.eskimo.com/~scs/cclass/notes/sx11.html">Class Notes, Chapter 11</a>
29 <br><a href="http://www.eskimo.com/~scs/cclass/notes/sx12.html">Class Notes, Chapters 12</a>
30 <a href="http://www.eskimo.com/~scs/cclass/notes/sx13.html">and 13</a>
31 <p><B>Reading Assignment:
32 </B></p><p><a href="http://www.eskimo.com/~scs/cclass/notes/sx10.html">Class Notes, Chapters 10</a>
33 <a href="http://www.eskimo.com/~scs/cclass/notes/sx11.html">and 11</a>
34 <p><B>Review Questions:
35 </B></p><OL><li>If we say
36 <pre>
37 int i = 5;
38 int *ip = &amp;i;
39 </pre>
40 then what is <TT>ip</TT>?
41 What is its value?
42 <li>If <TT>ip</TT> is a pointer to an integer, what does <TT>ip++</TT> mean?
43 What does
44 <pre>
45 *ip++ = 0;
46 </pre>
47 do?
48 <li>How much memory does the call <TT>malloc(10)</TT> allocate? What if
49 you want enough memory for 10 <TT>int</TT>s?
50 <li>The assignment in
51 <pre>
52 char c;
53 int *ip = &amp;c; /* WRONG */
54 </pre>
55 is in error; you can't mix <TT>char</TT> pointers and <TT>int</TT> pointers like
56 this. How, then, is is possible to write
57 <pre>
58 char *cp = malloc(10);
59 int *ip = malloc(sizeof(int));
60 </pre>
61 without error on either line?
62 </OL><br>
63 <br>
64 <p><B>Exercises:
65 </B></p><OL><li>Write a program to read lines and print only those containing
66 a certain word.
67 (For now, the word can be a constant string in the program.)
68 The basic pattern
69 (which I have to confess I have parroted exactly from K&amp;R Sec. 4.1)
71 <pre>
72 while(<I>there's another line</I>)
74 if(<I>line contains word</I>)
75 <I>print the line</I>;
77 </pre>
78 Use the <TT>strstr</TT> function (mentioned in the notes) to look for the
79 word. Be sure to include the line
80 <pre>
81 #include &lt;string.h&gt;
82 </pre>
83 at the top of the source file where you call <TT>strstr</TT>.
84 <li>Rewrite the checkbook-balancing program from
85 assignment 4 (exercise 6)
86 to use the
87 <TT>getwords</TT> function (from the notes) to make it easy to take the
88 word ``check'' or ``deposit'', <I>and</I> the amount, from a single line.
89 <li>Rewrite the line-reversing function from
90 assignment 4 (exercise 9)
91 to use pointers.
92 <li>Rewrite the character-counting function from
93 assignment 5 (exercise 3)
94 to use pointers.
95 <li>Rewrite the string-concatenation program from assignment 5 (exercise 4)
96 to call <TT>malloc</TT> to allocate a new piece of memory just big enough
97 for the concatenated result. Don't forget to leave room for the
98 <TT>\0!</TT>
99 <li>Rewrite the string-replacing function from
100 assignment 5 (exercise 5)
101 to use pointers.
102 <li>(harder)
103 Write a program to read lines of text up to <TT>EOF</TT>, and
104 then print them out in reverse order. You can use <TT>getline</TT> to
105 read each line into a fixed-size array (as we have been doing
106 all along), but you will have to call <TT>malloc</TT> and make a copy of
107 each line before you read the next one. Also, you will have to
108 use <TT>malloc</TT> and <TT>realloc</TT> to maintain the ``array'' of character pointers
109 which holds all of the lines. (Your code will be similar to that
110 in section 11.3 of the notes, p. 5)
111 <br>
112 <br>
113 Extra credit: remove the restriction imposed by the fixed-size
114 array into which each line is originally read; allow the program
115 to accept arbitrarily many arbitrarily-long lines. (You'll have
116 to replace <TT>getline</TT> with a dynamically-allocating line-getting
117 function which calls <TT>malloc</TT> and <TT>realloc</TT>.)
118 </OL><hr>
119 <hr>
121 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
122 // <a href="copyright.html">Copyright</a> 1995-9
123 // <a href="mailto:scs@eskimo.com">mail feedback</a>
124 </p>
125 </body>
126 </html>