ProductsLogo
LogoStudy Documents
LogoAI Grader
LogoAI Answer
LogoAI Code Checker
LogoPlagiarism Checker
LogoAI Paraphraser
LogoAI Quiz
LogoAI Detector
PricingBlogAbout Us
logo

Defending Against Confusion

Verified

Added on  2019/09/22

|7
|3161
|293
Essay
AI Summary
The assignment content explains how to write a program that can be run on both PC and Mac without being disrupted by carriage return (CR) characters. It suggests using a function called `mygetchar()` which discards CR characters before returning the actual character read. The content also mentions the importance of testing programs with various input files, and provides sample test data for reference. Additionally, it addresses questions about using structs in the project, and outlines the marking rubric that will be used to evaluate submissions.

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
COMP20005 Assignment 1, 2017s1
Last updated: April 28, 2017
Oldest entries are at the bottom.
Items 1-10 were in place when this page was first released on 5 April.
Submission Instructions
The submission process was illustrated starting at around the forty-three minute
mark of the lecture on Friday 7 April. Review that lecture section if you are having
trouble withsubmit.
Information and Resources
23.Programs that Crash on dimefox: Note that on dimefox the programs are
being executed in non-interactive mode, and partial output
does not automatically get captured if the program crashes. This can make it
hard to determine where it is in the program that the problem is occuring.
Best bet is to put a command fflush(stdout); after each Stage's output is
complete, so that anything generated by that stage is guaranteed to be
captured in the output stream. You can add this after any
other printf() statements in the program too, if you want to be sure that you
want to capture every ounce of output generated. Then what you see
with verify should be the same as what you would have seen if you had run
the program interactively at the command-line. You may leave
the fflush() statements in place when you submit, or include them as part
of what is controlled by DEBUG, see #19.
22.Upper Bound on Arrays: A couple of students have said that on their
computers the 50,000 limit cannot be achieved, that their programs fails
with a Segmentation Fault error if multiple arrays of that size are declared as
local variables in the main(). You may decrease the limit to 20,000 if you
are experiencing this problem.
21.Assert? (A question from "an awestruck & brainwashed programming
student"). The sample solution to 2015 has assert.h included. If you include
this, it gives you access to the function assert() and enables you to write
things in your program like:
22. /* compute average */
23. assert(denominator>0);
24. avg = numerator/denominator;
where you are sure that the denominator will be positive, and you think the
program is correctly computing the things you want, but if something has
gone wrong (bizarre data, or weird unexpected input combinations, or

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
whatever) you want to get a "soft crash" rather than a "hard crash". What
happens is that the expression in the assert gets tested at runtime, and if it is
not true, the program will halt at this point, and print the line number and
file nname, and the assertion that failed. Try it if you like!
20.Stage 4 Output: Yes, one of the input files you have been given
(data00031.txt) doesn't comply with the specification, because it doesn't fit
the description of "you may assume that each year that is represented in the
input will have data for all twelve months (so that the scores are always out
of 24)". This question came up at the beginning of the lecture on Monday 24
April; please listen to that before asking again about it. My program
generates 0/2 for that data file, and 0/24 will also be regarded as being
correct for this data file. Ok?
19.DEBUG, huh? Student question, "Sir-oh-illustrious-one, I noticed in your
program yesterday that you had a #define DEBUG 0, what was that about?"
Yes, good question. At the top of the program I have
20. #define DEBUG 0
and then right through the program I can do things like
if (DEBUG) {
printf("line %3d, x = %d\n", __LINE__, x);
}
which doesn't do anything when DEBUG is zero, but prints out the program
line number and the value of x when DEBUG is 1. This lets you put helpful
debugging printf()s right through your program, and then switch them all
"off" by changing DEBUG to 0. Then they can stay there in the code, inactive,
in case they are needed again later. The constant __LINE__ (two underscore
characters on each side) gets expanded to the current line number by the
compiler when it is processing your program. (Just make sure that DEBUG is
zero when you submit your final version, of course.)
18.File Operations: Student question, "Can we use fopen() in our programs?
No, on no account should you be using fopen() in your programs. The data
that your program will execute on in the test harness will be supplied via
redirection of stdin. If you try and open a named file using fopen() your
program will fail.
17.Stage 4 Output: I have had this question several times now, "When we're
given between 6 and 9 years of data, how should it be printed out in Stage
4? I agree that the spec doesn't cover this case, and so anything
reasonable/sensible will be accepted. But for concreteness, if you want to be
told what is reasonable/sensible, feel free to implement this rule: "at most 5
lines at the beginning, followed by a -- if any lines are omitted, followed by
Document Page
at most five lines from the end, with no lines duplicated". Both of these next
examples would fit that rule:
18. Stage 4
19. -------
20. 2009: score is nn/24
21. 2010: score is nn/24
22. 2011: score is nn/24
23. 2012: score is nn/24
24. 2013: score is nn/24
25. 2014: score is nn/24
26. 2015: score is nn/24
27. 2016: score is nn/24
28.
29. Stage 4
30. -------
31. 2006: score is nn/24
32. 2007: score is nn/24
33. 2008: score is nn/24
34. 2009: score is nn/24
35. 2010: score is nn/24
36. --
37. 2012: score is nn/24
38. 2013: score is nn/24
39. 2014: score is nn/24
40. 2015: score is nn/24
41. 2016: score is nn/24
where nn is the score you have to calculate.
16.Debugging: Yes, I do sometimes take a look at programs if you are stuck
(debugging is also fun!), and can usually identify issues that might be giving
you your problems. But please don't just mail me a screenshot, or paste a
little bit of program fragment into an email -- I'll want to see the whole
program. So submit it on dimefox so that I can see all of it (and what
happens when it executes) before sending me a nice email that says "Dear
Sir, I'm really truly enjoying your lectures, and indeed I am beginning to see
just how much fun programming could be (and I would very definitely to
make it even more fun in the future), but I have a slight problem right now
that is detracting from the immense fun experience. Would you mind, oh-
inspirational-one, taking a look at my current submission and see if you can
explain why " and then summarize the symptoms of the problem that you
are having. I wouldn't go so far as to say that I answer such emails in order
of decreasing obsequiousness, but the ones that are truly sucky do certainly
make me smile...
15.Question from a student: On the first page, you state that it is 45 years for
the data from 1971-2016 inclusive. Wouldn't it actually be 46 years
including both 1971 and 2016?: Yes, you are right. That's a mistake, 1971-
2016 is indeed 46 years.
Document Page
13.Question from a student: Will marks be deducted because of not using
functions? Yes, you will lose marks if you have avoided using functions. I'll
get the marking rubric finalized by the middle of the non-teaching week.
12.Typo in Handout: Where it says "These values should be ignored when
computing the average, as the corresponding years were each a little shorter"
, it should really have said "These values should be ignored when computing
the average, as if the corresponding years were each a little shorter", that is,
insert the word "if".
11.Likely Mark Distribution: To help soothe the fears that some of you may
have after the test marks were released, here is the Assignment 1 mark
distribution from 2016s1:
12. 0 |4
13. 0.5- 1 |
14. 1.5- 2 |3
15. 2.5- 3 |44
16. 3.5- 4 |42
17. 4.5- 5 |44442
18. 5.5- 6 |444441
19. 6.5- 7 |444444442
20. 7.5- 8 |444444444444443
21. 8.5- 9 |44444444444444444444444442
22. 9.5-10 |4444444444444444444444444444444444444444
23.
24. 415 values; min=0.0; max=10.0; mean=8.2; median=9.0; sd=2.0
10.Tabs: The default in jEdit is for tabs to be aligned every 8 character
positions. Some of you have altered that to four (Preferences->Editing->Tab
width), to reflect the layout that the programs in the book have. Then, on
submission, the tabs have "appeared" in the output as being 8 again, which
can make your program spill past the 80-character RH boundary. When I
run the programs for marking, they'll all get formatted with tabs reflecting 4
character positions, not 8. But don't use any fewer than 4 in your jEdit (or
other editor) settings.
There are no tabs anywhere in the sample output files.
9. Magic numbers: Here is a summary of the rules about magic numbers:
o Where a number is totally self-defining, I'm happy for it to be used
any number of times without a hash-define, provided the code is
commented each time and/or explicitly sensible variable names are
used. For example, in
o /* compute percentage */
o pcent = 100.0*count/totcount;
I wouldn't expect 100 to have been hash-defined, since the comment
explains the role of the 100, and it isn't going to change, ever, even if
other percentages are calculated in the program using 100 too.

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
This rule also allows 0 and 1, of course, unless they represent
something other than the additive and multiplicative identities, in
which case they should be hash-defined.
This rule also allows while (scanf("%lf%lf", &x, &y)==2), since the
2 is immediately obvious from the adjacent context (two variables to
be read).
o Where a constant is one that is a fact that is in no way ever going to
be varied, then provided it only appears once in the program and is
explained with a comment, then it need not be hash-defined. The
example here is the -32.0 in the temperature conversion computation,
assuming that it is entirely within a function calledCels2Fahr or etc
and that it isn't used in other places scattered through the program.
Anything of this type that appears even twice should be hash-defined.
o Where a factual constant is used more than once in a program, even if
all occurrences are in a single function, it should be hash-defined.
o Where a constant is one that is clearly an artifact of the problem
description or the program that implements the solution (for example,
numbers like MAXINT, or the number of variables), then they must be
hash-defined, even if only used once in the program.
Make sense?
8. Trouble with newline characters: Text files that are created on a PC, or
copied to a PC, edited and then saved again on the PC, may end up with PC-
format two-character (CR+LF) newline sequence, see the Wiki page for
details.
If you have compiled your program on a PC, and it receives
a CR+LF sequence, then getchar() will consume them both, and hand a
single-character '\n' newline to your program. So in that sense, everything
works as expected. Likewise, on a PC when you write a '\n' to stdout,
a CR+LF pair will be placed in to the output file (or passed through the pipe
to the next program in the chain).
The problems arise when you copy your program and a PC-format test file
to a Unix system and then try compiling and executing your program there.
Now the CR characters get in the way and arrive via getchar() into your
program as stand-alone '\r' characters.
The easiest way to defend against these confusions is to write your program
so that it looks at every character that it reads, and if it ever sees a CR come
through, it throws it away. That way, if you do accidentally get CR characters
in your test files on the Unix server (or on your Mac) your program won't be
disrupted by them. Here is a function that you should use to do this:
Document Page
int
mygetchar() {
int c;
while ((c=getchar())=='\r') {
}
return c;
}
Then just call mygetchar() whenever you would ordinarily call getchar(),
on both PC and Mac.
Because most of you work on PCs (including in the labs), the test files that
are provided have been created with the PC-style CR+LF newlines, and
should work correctly when copied (use right-click->"Save as") to a PC.
With mygetchar() they can also be used on a Mac, but won't interact
sensibly using the standard getchar() function.
To be consistent, the final post-submission testing will also be done using
PC-style input files but will be executed on a Unix machine, meaning
that all submitted programs will need to make use of mygetchar().
You can use the "Preferences->Encodings" menu ("screwdrive/hammer
Options->Encodings" in the PC version) in jEdit to select whether to use
Unix (LF) or DOS/Windows (CR+LF) encodings in any test files that you
create with jEdit. Note that this only applies to newly created files. jEdit will
by default respect the formatting in any current files.
Note also that jEdit doesn't automatically add a newline after the last line of
text files, you need to put it there explicitly yourself (just press enter one
more time, so that jEdit thinks there is an empty line at the end of the file).
Watch out for this problem if you are creating your own test files on Mac or
PC.
If in any doubt, use od -a <file> in a Unix shell to look at the byte-by-byte
contents of a file, and check which format is being used, and whether there
is a final newline character (or final CR+LF pair). You can do this on a PC by
starting the MinGW shell and then using cd to reach the right directory.
There is an od version available within the MinGW shell on the PCs in the
labs. On a Mac, Terminal is a Unix shell.
7. Example program: Here is a sample solution to the comp20005 2015
Assignment 1, so that you can see the style of programming/commenting
that is expected of you. (The original specification is here, if you are
interested; but note that you don't need to read this, the idea is for you to
browse through the program and see the level of commenting that is
expected, and the way in which arrays and functions are used.)
Document Page
6. Question: Can we use structs in the project? Answer: yes, if you are sure
you know what you are doing; if used correctly, they will make your
program more elegant. We won't be looking at Chapter 8 until after the
deadline though, so you will be on your own. And it will be possible to get
full marks without using them.
5. Sample test data: Here are the test files used in the examples in the handout:
o data00031.txt and data00031-out.txt.
o data00365.txt and data00365-out.txt.
o data16802.txt and data16802-out.txt.
You should also develop your own test data, of course; you can't have any
confidence in a program until it has been tested on a wide range of inputs.
4. Illness, extensions: Students seeking extensions for medical or other
"outside my control" reasons should email me as soon as possible after those
circumstances arise. If you attend a GP or other health care professional as a
result of illness, be sure to take a Health Professional Report form with you
(get it from the Special Consideration section of the Student Portal, or
from here); you will need this form to be filled out if your illness develops
in to something that later requires a Special Consideration application to be
lodged. You should scan the HPR form and send it to me connection with
any non-Special Consideration assignment extension requests. (Note that if
you are ill during examination period, you should get the same HPR form
completed and then lodge it via the Student Portal as part of the University's
formal Special Consideration process; those applications get processed
centrally, and not by me.)
3. Marking rubric: The marking rubric that will be used to evaluate your
submissions is now linked here.
2. Submissions are now open: A link to the submission instructions is right at
the top of this page once.
1. Specification: The specification for the project is linked here.
1 out of 7
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]