ASSIGNMENT

R

Richard Tobin

They're not a problem *now*, but they might be in the future when
new functions are added.
[/QUOTE]
And when those new functions are added, the compiler will no longer warn
about code that is now suddenly visibly broken?

No, I would hope it would give me a more serious diagnostic! Though
a quick test suggests that gcc doesn't.

-- Richard
 
C

CBFalconer

Keith said:
CBFalconer said:
pete said:
CBFalconer wrote:
Harald van D?k wrote: [...
total is reserved as an external identifier for use by the
implementation.

Where is this specified?

N869
7.26
[#1]
7.26.2
[#1]

I find no such section in either n869 or n1256.

Really? I see it in both; I wonder how you missed it.

n1256 7.26 Future library directions

7.26.2 Character handling <ctype.h>

Function names that begin with either is or to, and a lowercase
letter may be added to the declarations in the <ctype.h> header.

Now I see what I did. I searched for 7.2, not 7.26. Sloppy.
 
L

luserXtrog

unsigned char a[36][2]= { 0, 0, 0, 0,
0, 2,
0, 22,
'l', 1,
'+', 2,
's', 1,
'-', 3,
'z', 13,
'l', 1,
'+', 0,
's', 0,
'j', 4,
'l', 0,
'w', 0,
'q', 0,
};
unsigned char ip = 0;
unsigned short acc = 0;

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

int init() { return true; }

#define load ( (a[ar][0] << 8) + a[ar][1] )
int run() { unsigned char op,ar;
while(1)
switch(ar= a[ip][1], op= a[ip++][0]) {
case 'l':acc= load; break;
case 's':a[ar][1]= acc; a[ar][0] = acc >> 8; break;
case '+':acc+= load; break;
case '-':acc-= load; break;
case '*':acc*= load; break;
case 0:case 'n':break;
case 'j':ip = ar; break;
case 'z':(acc==0)?(ip=ar):0xF00L;break;
case 'r':scanf("%hd", &acc); break;
case 'w':printf("%d\n", (int)acc); break;
case 'q':goto done;
}
done: return true; }

int main() {return init()&&run()?EXIT_SUCCESS:EXIT_FAILURE;}
 
G

Guest

You know the "give a man a fish" saying? Well, ISTM that it applies to this
situation as well, but even more so. It is secondary that the OP doesn't
know how to solve a simple problem with elementary C code. The *primary*
problem is that the OP doesn't know how to "think like a programmer". Once
he conquers /that/ step, it becomes trivial to learn and use /any/
programming language to solve programming problems.

So, What should we (in CLC) do for such questions? Should we ignore them?
Should we give them the (useless) answer to their question? (Useless,
because without the "programmer mindset", the "answer" does not solve their
real problem). Do we show them their failings? Do we carp about other
posters? Or, do we try to teach them to "think like a programmer" so that
they can learn?

It depends on the circumstances. Is the persone requesting help
reasonably polite? Does he appear to have made some effort on his
own? Is it reasonable that someone can't even start such a problem?

In the past I've tried to "bootstrap" such posters. Using pseudo
code (not Lisp-like p-code either!) or structured english or the
old "do it on paper first". Sometimes I consider a list of library
calls a good hint.

But *this* request was extremely simple. You'd expect he'd have seen
a similar example in the classroom. He was also rude and abrupt.

<boring anecdote> civil engineering students at my university
were required to survey the local park. In mid-winter in north-
western europe. Some of them were from tropical countries (which
seemed the be adding insult to injury). The going rate for a
pre-completed survey was quite considerable.

A relative studying architecture (this was 70 years or so ago so
don't worry about things falling down) in similar circumstances
reversed engineered the survey from an ordanace survey map.
Learned a lot in the process apparently.
If the person posts an obvious homework problem here for solution by others
(presumably, so that s/he can claim unearned credit for the solution), then
s/he has /already/ brought shame on themselves and on their family. If I
were taken to judgments, I would say that they were cheats and liars, and
lazy (or at least inattentive) to boot. Thankfully, I'm not taken to
judgments.


I seriously doubt it. But then again, if someone is so off-centre that they
commit suicide because someone here gave them flack for posting a homework
problem, it is likely that they are already on the suicide track,
and /anything/ could set them off.


Think of it as evolution in action.

beat me to it
 
R

Richard Bos

Han from China said:
By the way, Azo, the solution I gave you is a serious one. If you
have any more assignments to do, just let me know on this newsgroup,
and I'll help you as best I can. I consider opinions about learning
theory, plagiarism, ethics, etc., off-topic for this newsgroup, so
you can count on me to address the C content of your posts. Suffice
it to say, I have my own views about learning and education, views
I picked up from a number of 20th-century thinkers who were experts
in the field and whose opinions I respect.

Steiner, I suppose?
I'd suggest making sure
you understand the solutions I give you, and perhaps after reading
them, try your hand at coding the solutions without looking at mine.
All the best with your studies, buddy.

Yeah. All the best with cheating on your homework, all the best with
getting a diploma you don't deserve, a job you will botch up, and rest
assured that if I ever get to work with you, and have to clean up after
your lazy arse, then after I dismember you using nothing but a teaspoon
and a jar of cider vinegar, I will hunt out the person who spoon-fed you
your homework solutions - and _his_ death will be much more painful than
yours, and involve at least a litre of formic acid and a pound of salt.

Richard
 
C

CBFalconer

Richard said:
.... snip ...


Yeah. All the best with cheating on your homework, all the best
with getting a diploma you don't deserve, a job you will botch
up, and rest assured that if I ever get to work with you, and
have to clean up after your lazy arse, then after I dismember
you using nothing but a teaspoon and a jar of cider vinegar, I
will hunt out the person who spoon-fed you your homework
solutions - and _his_ death will be much more painful than yours,
and involve at least a litre of formic acid and a pound of salt.

A fine elegant response, with a significant lack of cursing.
Congratulations.
 
K

Keith Thompson

Keith Thompson said:
#include <stdio.h>

int (*p(int n))(void);

int main(void)
{
int s = 0;
int t = !++s;
int i;
for (i = 2; i < 13; i ++) {
if (p(i)) {
if (++t&1) {
s *= i;
}
}
}
printf("%d\n", s);
return 0;
}

int (*p(int n))(void)
{
int i;
for (i = 2; i < n; i ++) {
if (n % i) {
continue;
}
return +'/'/(+'/')+(-'/')/'/';
}
return main;
}

Generalizing this solution is left as an exercise.

I must say I'm a little disappointed that nobody ever asked me how
this works. :cool:}

Oh, and I realized the parentheses in the resturn statement are
superfluous; I should have written:

return +'/'/+'/'+-'/'/'/';
 
L

luserXtrog

I must say I'm a little disappointed that nobody ever asked me how
this works.  :cool:}

I wondered, but was too embarrassed to ask.
I was able to wade through all the pieces, but couldn't
grasp the whole. I you feel an urge to explain, be
assured: ears are open!
 
K

Keith Thompson

luserXtrog said:
I wondered, but was too embarrassed to ask.
I was able to wade through all the pieces, but couldn't
grasp the whole. I you feel an urge to explain, be
assured: ears are open!

Well, if you insist ...

The assignment, in essence, is to print the value 110. Rather than
computing it as 2+4+6+8+10+12+14+16+18+20, I decided to start with the
prime factorization of 110: 2*5*11. Those happen to be the first,
third, and fifth (i.e., alternating) prime numbers.

(Embarrassing confession: I initially miscomputed the result as 210,
which is the product of the first 4 primes, 2*3*5*7; I was going to
use that, but fortunately I redid the sum. Sometimes it's the simple
things that trip you up.)

The function p, as the name nearly suggests, returns a result
that indicates whether its argument is a prime number, using a
rather inefficient but straightforward method. But rather than
just returning 0 or 1, I decided to have it return a null or
non-null function pointer -- which can be used the same way in a
conditional context. The non-null result is the address of main.
The null result is a rather elaborate null pointer constant, which,
after deleting the parentheses (which I should have done before
posting the code) is:
+'/'/+'/'+-'/'/'/'
I started with the infamous constant expression '/'/'/', which is the
numeric value of the '/' character divided by itself, which is 1 (just
using '-'-'-' would have been too easy). Subtracting it from itself
gives '/'/'/' - '/'/'/', which is 0, which is therefore a null pointer
constant. Then I threw in a couple of unary "+" operators. The whole
thing is equivalent to "return 0;".

In the main program, s is the sum (which is actually computed as a
product), and t is a toggle (its low-order bit alternates between 0
and 1). The initializer for t has the side effect of setting s to 1,
which is where it needs to start. I loop over all integers from 2
to 13, selecting just the primes, and then use the alternating
low-order bit of t to select every other prime. So the program
ends up computing 2*5*11, which is the result we were looking for.

One could argue that this doesn't actually meet the requirement,
since it doesn't actually "CALCULATE THE SUM" -- but it does perform
a calculation whose result is the correct sum.

It was probably too much to hope that the original poster would
try to turn this in; by the time I posted it was pretty obvious
what we were up to.
 
L

luserXtrog

Well, if you insist ...

I meant to say "pretty please".
The assignment, in essence, is to print the value 110. Rather than
computing it as 2+4+6+8+10+12+14+16+18+20, I decided to start with the
prime factorization of 110: 2*5*11. Those happen to be the first,
third, and fifth (i.e., alternating) prime numbers.

(Embarrassing confession: I initially miscomputed the result as 210,
which is the product of the first 4 primes, 2*3*5*7;

There's a really cool house I can't afford whose street number is
2357, pink and white iris lawn; ~~sigh~~.
I was going to
use that, but fortunately I redid the sum. Sometimes it's the simple
things that trip you up.)

The function p, as the name nearly suggests, returns a result
that indicates whether its argument is a prime number, using a
rather inefficient but straightforward method. But rather than
just returning 0 or 1, I decided to have it return a null or
non-null function pointer -- which can be used the same way in a
conditional context. The non-null result is the address of main.

I saw that part, but the prime decomposition eluded me.
So this exercise to generalize is to find Goedel encoding?
The null result is a rather elaborate null pointer constant, which,
after deleting the parentheses (which I should have done before
posting the code) is:
+'/'/+'/'+-'/'/'/'
I started with the infamous constant expression '/'/'/', which is the
numeric value of the '/' character divided by itself, which is 1 (just
using '-'-'-' would have been too easy). Subtracting it from itself
gives '/'/'/' - '/'/'/', which is 0, which is therefore a null pointer
constant. Then I threw in a couple of unary "+" operators. The whole
thing is equivalent to "return 0;".

In the main program, s is the sum (which is actually computed as a
product), and t is a toggle (its low-order bit alternates between 0
and 1). The initializer for t has the side effect of setting s to 1,
which is where it needs to start. I loop over all integers from 2
to 13, selecting just the primes, and then use the alternating
low-order bit of t to select every other prime. So the program
ends up computing 2*5*11, which is the result we were looking for.

Holy shit.
One could argue that this doesn't actually meet the requirement,
since it doesn't actually "CALCULATE THE SUM" -- but it does perform
a calculation whose result is the correct sum.

It was probably too much to hope that the original poster would
try to turn this in; by the time I posted it was pretty obvious
what we were up to.

I tried to pass the torch with a byte-coded single-address
instruction interpreter, so the problem's all in the data
structure. The trick to grokking it is to read it with
line numbers so you can see what the addresses refer to.
I started it with /* n */ comments but got rid of them
when it became to slow to keep updating them for every
little change (I wanted to put a little compiler in it, too,
that became too much to handle).

Great post. Long Liv bf00sk8n
 
K

Keith Thompson

luserXtrog said:
I saw that part, but the prime decomposition eluded me.
So this exercise to generalize is to find Goedel encoding?

Sort of, I suppose. But the "Generalizing this solution is left as an
exercise" remark was actually a joke. I suppose the problem statement
might be "Write an obfuscated program along these lines that prints an
arbitrary integer", but the solution as written doesn't work for
numbers with a prime factor that occurs more than once.

Now that I think about it, though, I suppose you could construct a
finite state machine that iterates over a number's prime factors,
hitting certain factors multiple times if necessary. Though not in
order, of course. I might develop that idea further if a similar
question comes up.

[...]
 
R

Richard Bos

Richard said:
Wow, you sure have a high opinion of your own self worth.

No, I have a high opinion of the worth of my future customers. They will
pay good money for the programs that we will provide for them, and they
deserve not to have them written by someone who got his credentials by
cheating.
Tell me, when you had problems who did you ask?

Oh, any number of people. But, and this is the important bit, I did not
ask for hand-outs. I never wanted to be spoon-fed solutions to homework
problems.

Richard
 

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
473,772
Messages
2,569,593
Members
45,113
Latest member
Vinay KumarNevatia
Top