Some Exercises

F

feivue

1. In Java, intergers can be assigned to real variales, but not vice versa.
What design principle does this violate? In C, this restriction does not
exist. What design principle does this violate?

2. C use the semicolon as a statement terminator, but also allows a
statement to be empty(that is, consisting of only a semicolon), so that the
following is a legal C program:

main() {
;;;;;;;;;
return 0;
}

Discuss the advantages and disadvantages of this.
 
M

Mike Wahler

feivue said:
1. In Java, intergers can be assigned to real variales, but not vice versa.
What design principle does this violate? In C, this restriction does not
exist. What design principle does this violate?

2. C use the semicolon as a statement terminator, but also allows a
statement to be empty(that is, consisting of only a semicolon), so that the
following is a legal C program:

main() {
;;;;;;;;;
return 0;
}

Discuss the advantages and disadvantages of this.

OK, let's discuss.
You start.

-Mike
 
M

Malcolm

feivue said:
1. In Java, intergers can be assigned to real variales, but not vice versa.
What design principle does this violate? In C, this restriction does not
exist. What design principle does this violate?
What do you think? Consider what happens if reals and integers are both 32
bits.
For the C question, consider how, mathematically, an arbitrary real can
become an integer.
You could also try to write the function

void real_tostring(double x, char *str)

without calling any library routines, and without assigning a real to an
integer.
2. C use the semicolon as a statement terminator, but also allows a
statement to be empty(that is, consisting of only a semicolon), so that
the following is a legal C program:

main() {
;;;;;;;;;
return 0;
}

Discuss the advantages and disadvantages of this.
This question seems to be of much lower quality than the first. Your program
is of course gibberish, and the fact that some gibberish happens to be
syntactically correct C helps no-one except the Obfusucated C contest.
However you could consider the role of the pre-processor in C programs.
 
O

osmium

feivue said:
2. C use the semicolon as a statement terminator, but also allows a
statement to be empty(that is, consisting of only a semicolon), so that the
following is a legal C program:

main() {
;;;;;;;;;
return 0;
}

Discuss the advantages and disadvantages of this.

A *huge* disadvantage is that it is much too subtle for a human being. Both
the writer and the reader of the code can be enmeshed in a hellacious mess
because of that subtlety. OTOH a clearly marked null statement might lead
to a new keyword, how many words should a language designer be entitled to
"use up"? NULL and VOID have already been used. Of course there is the
multiple entendre approach, a la const and static.

P.S. Your instructor should fix up his sample code so it doesn't look absurd
and stooopid.
 
M

Mark McIntyre

1. In Java, intergers can be assigned to real variales, but not vice versa.
What design principle does this violate?

1a) the principle that "We are to admit no more causes of natural
things, than such as are both true and sufficient to explain their
appearances. "
In C, this restriction does not
exist. What design principle does this violate?

1b) the principle that "Therefore to the same natural effects we must,
as far as possible, assign the same causes."
2. C use the semicolon as a statement terminator, but also allows a
statement to be empty. Discuss the advantages and disadvantages of this.

2) its great for business if you are a manufacturer of teletype paper,
but not so good if you live in the arctic and get your printer ink
delivered semiannually.

The great disadvantage of homework is that if you quote it verbatim in
usenet, most people spot it instantly and post meaningless, misleading
or wrong answers on purpose. The advantage of this is that the teacher
is given big hints as to which pupils were too lazy to at least ask
the question in normal english, or to make sure they understood the
answer (or for that matter the question).

Next.
 
N

Nils Petter Vaskinn

2. C use the semicolon as a statement terminator, but also allows a
statement to be empty(that is, consisting of only a semicolon), so that the
following is a legal C program:

I'll give you two hints for number2:

#ifdef DEBUG
#define DEBUG_PRINT printf("%s %d\n",__FILE__,__LINE__);
#else
#define DEBUG_PRINT
#endif

int main() {
DEBUG_PRINT;
return 0;
}


#ifdef FORGOTTEN
#define DO_SOMETHING(x) do_something(x)
#else
#define DO_SOMETHING(x)
#endif



Now it's up to you to do your homework and figure out what the advantages
and disadvantages shown are, and if there are any more of them.


hth
 
C

Chris Dollin

feivue said:
1. In Java, intergers can be assigned to real variales, but not vice
versa. What design principle does this violate? In C, this restriction
does not exist. What design principle does this violate?

Why do you want us to do your homework?
2. C use the semicolon as a statement terminator, but also allows a
statement to be empty(that is, consisting of only a semicolon), so that
the following is a legal C program:

main() {
;;;;;;;;;
return 0;
}

Discuss the advantages and disadvantages of this.

Why do you want us to do your homework?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top