[Duncan's Home] Duncan's Jotter
faq -  feedback -  home 
Members
Logon   -   Sign Up

A wee C problem to ponder

Msg#776 - A wee C problem to ponder

In response to: Top of Thread. | <<Back | Next>> | Top of Thread | View Full Thread | Reply | Edit

Posted: 1/19/2001 by Duncan
Modified: 1/20/2001 by Duncan

I posted the following problem for my exam-exempted students to ponder whilst those less fortunate than themselves were sitting the LLP exam yesterday afternoon. [Caveat: no warnings were reported by the particular compiler that was being used at the time this problem was first set for an exam - 1994. I believe the current Borland compiler issues three warnings but it's perfectly valid C and has no syntax errors - only semantic one(s) so the code should compile and run as is. But don't cheat and use the compiler to (help) work out the answer. Feel free to post answers here. FYI, hardly anyone that attempted this problem in the extra coursework came even close to answering this part of the question correctly. And they could have used a compiler to help them!!]

4 (a) The following C source text has been compiled without any errors or warnings being reported:

#include <stdio.h>

main() { unsigned char c; int j,n; for (c=getchar(),j=0,n='\n'; c!=EOF; (j%5==0)?putchar(n),putchar(c):\ putchar(c),c=getchar(),j++) exit (0); }

Nevertheless, due to some simple coding errors, the program does not split the input lines on every fifth character as intended. Answer the following questions:

  1. What does the program, as given, actually do?
  2. What mistakes have been made by the programmer that prevent it working as intended?
(5 marks)

(b) As well as being in error, this program is badly written and violates good programming practice. Rewrite it to make it as readable and as understandable as possible, eliminating the original mistakes in the process.

(5 marks)

Enclosures:
None.

Replies:
None.

Tell ICANN to keep their hands off .org!


Run the HTML validator for this page
Webmaster: web at smeed.org