A lurker's take on C.L.C pedantry

B

Ben Bacarisse

Kaz Kylheku said:
On the topic of who is knowledgeable and who isn't, there was nice a
thread here in late February, subject line: ``interesting math
problem''.

The only one who solved the problem, giving a correct algorithm in
the form of working C code (ISO standard and all) was me.

I missed that message. I saw your Lisp solution, bit not a C one.
Can you post a link, please? (I can't find it but Google groups is
unreliable or I may just be missing the obvious.)

It was shocking. Nobody else in this group actually knew how to
solve a moderately complex, self-contained programming problem that
can be coded in a few dozen lines of strictly conforming ISO
C---i.e. nothing approaching the complexity of some real-world
software engineering problem.

Woah there! How can you conclude that no one knew how to solve it?
The reason I did not post a solution is that it seemed to be one of
those problems that is better solved in a high-level language. I saw
no reason to post another non-C solution and I don't much care for C
programs that are "translated down" for HLL solutions (though
sometimes that is the best we can do).

'Gene' has posted a C solution that emulates these high-level
structures in C. It has the expression backwards so it does not find
solutions to the problem as posted, but that is trivial to change. I
don't want to detract from that fine solution, but it not "natural" C.

Your fighting talk and my nagging feeling that there might be a simple
solution in C lead me the code posted below. I doubt is anything like
the best way to do this in C and, being un-commented, it is rather
cryptic but I am happy with it. It counts the possible trees using
mixed-base counter. These are mapped into tree shapes that can
evaluated (or printed) using a set of assignments for the values and
the operators.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <float.h>

typedef struct tree {
struct tree *arg[2];
} tree;

bool next_tree(int n, int *sum)
{
int p = 1;
while (p < n && sum[p] == n - p) p++;
if (p == n)
return false;
int d = sum[p] + 1;
while (p) sum[p--] = d;
return true;
}

tree *tree_make(int nops, int *counter, tree *nodes)
{
tree *stack[nops + 1], **sp = stack, *next_node = nodes;
*sp++ = 0;
for (int i = nops; i > 0; i--) {
*sp++ = 0;
int shift = counter[i-1] - counter;
while (shift--) {
tree *n = next_node++;
sp -= 2;
n->arg[0] = sp[0];
n->arg[1] = sp[1];
*sp++ = n;
}
}
return stack[0];
}

double tree_eval_aux(tree *e, double **args, char **ops)
{
if (e) {
double a = tree_eval_aux(e->arg[0], args, ops);
char op = *(*ops)++;
double b = tree_eval_aux(e->arg[1], args, ops);
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
}
return NAN;
}
else return *(*args)++;
}

void tree_print_aux(tree *e, double **args, char **ops)
{
if (e) {
putchar('(');
tree_print_aux(e->arg[0], args, ops);
putchar(*(*ops)++);
tree_print_aux(e->arg[1], args, ops);
putchar(')');
}
else printf("%g", *(*args)++);
}

void tree_print(int nops, int *counter, double *args, char *ops)
{
tree nodes[nops];
tree_print_aux(tree_make(nops, counter, nodes), &args, &ops);
}

double tree_eval(int nops, int *counter, double *args, char *ops)
{
tree nodes[nops];
return tree_eval_aux(tree_make(nops, counter, nodes), &args, &ops);
}

double find_max_tree(int nops, double *args, char *ops, int *best)
{
int tree_number[nops + 1];
memset(tree_number, 0, sizeof tree_number);
tree_number[0] = nops;
double max = -DBL_MAX;
do {
double r = tree_eval(nops, tree_number, args, ops);
if (isfinite(r) && r > max) {
max = r;
memcpy(best, tree_number, sizeof tree_number);
}
} while (next_tree(nops, tree_number));
return max;
}

int main(void)
{
double values[] = {2, 2, 3, 3, 4, 4, 5, 5};
char operators[] = "/-/-/-/";
int n = strlen(operators);
int best[n + 1];
double max_value = find_max_tree(n, values, operators, best);
tree_print(n, best, values, operators);
printf(" = %g\n", max_value);
return 0;
}
 
B

Ben Bacarisse

Richard Heathfield said:
I suspect most of the regs avoided answering the OP because early
answers appeared from people like Ben B,

I fear you are misremembering! My posts were not answers and were
barely helpful!

I have rectified that, to some extent, by posting a C solution today. I
am still not sure what the best way is to tackle this in C. It is an
interesting problem.
 
B

BartC

Kaz Kylheku said:
On the topic of who is knowledgeable and who isn't, there was nice a
thread
here in late February, subject line: ``interesting math problem''.

The only one who solved the problem, giving a correct algorithm in the
form of
working C code (ISO standard and all) was me. Moreover, in doing so, I
independently discovered the Catalan numbers, previously unknown to me,
their
connection to the enumeration of permutations of binary trees, and one of
the
recurrence relations which generates them.

Most of the other regs pretended they didn't see the thread, knowing well
that
it would be embarrassing to enter it without a solution in hand. For some
inexplicable reason, a few didn't mind piping up with long-winded versions
of
``I don't quite know how to solve this''.

Writing actual C is hard and possibly unproductive work, especially as the
OP only wanted guidelines.

I did some experiments in another language which I didn't post, but I did
suggest there was an upper value for the expression of 52.50. I remember
seeing loads of code in other posts, but no actual results..
It was shocking. Nobody else in this group actually knew how to solve a
moderately complex, self-contained programming problem that can be coded
in a
few dozen lines of strictly conforming ISO C---i.e. nothing approaching
the complexity of some real-world software engineering problem.

Well, your solution was initially in Lisp. Followed by 150 lines of C.

However no-one would program such a thing in C, even you used a different
tool. And (without reading the code) probably the C code you produced
wouldn't be useful in a real application without a lot of revision. Better
to get the big picture first before writing the C.
 
B

Ben Bacarisse

Ben Bacarisse said:
I missed that message. I saw your Lisp solution, bit not a C one.
Can you post a link, please? (I can't find it but Google groups is
unreliable or I may just be missing the obvious.)

Following BartC's message I see that the C followed the Lisp (in the
same message). I missed that on re-visiting the thread but I've got
it now.
 
A

Antoninus Twink

IMHO the problem with off-topic is that off-topic discussion
effectively robs ppl their time which could be spent on actually
helping someone who is asking on-topic questions.

This argument is completely specious.

All newsreaders (with the exception of Google Groups) will let you kill
a thread that you consider to be "off topic" with a single key press.
This takes *much* less time than composing a diatribe on topicality or
a "get lost to comp.foo.bar" message.

Far more of people's time is wasted reading the same old topicality
arguments (from both sides). Just live and let live.
 
A

Antoninus Twink

I'm curious to see what combination of characters you could put
together that someone could actually perceive as an insult.

Surely one can only truly be insulted by someone one has some degree of
respect for? In which case, who could possibly be insulted by that
pathetic old man?
 
A

Antoninus Twink

The best help that can be given to OPs who ask off-topic questions is
that of directing them to a newsgroup where their question is topical

No, giving clear, concise, correct and timely answers to their questions
is more helpful by anyone's standard.

And frequently, the redirections are not to newsgroups where they will
receive more help, but to newsgroups that either don't exist at all, or
else haven't seen any activity for the past five years.
 
K

Kenny McCormack

Richard said:
I think I mentioned that you're an obnoxious, conceited, self centred
prick once or twice. No need for you to reinforce that any more than
necessary.

But a man (if you can call CBF a man) has got to play to his strengths!
 
K

Kenny McCormack

If he is deliberately lying, he is lying about *me*. There is nothing
gratuitous about calling him on it.

You send like a petulant little child. Grow up.[/QUOTE]

He *is* a petulant little girl. Everybody knows that by now.
 
C

Chris H

CBFalconer said:
Richard said:
Keith Thompson said:
[...]
Good for you. As far as I am concerned Kylheku is a troll, and
has been plonked here. Some years ago there was another person
with the same (I think) name, who was knowledgeable and helpful.
He was a student in Helsinki, as I recall. This one has snuck
in with the same (or similar) name.
[...]

I'm reasonably sure he's the same person (to the extent that
anyone is "the same person" after some years have passed). I
seem to recall he has stated so himself, and I think Richard
Heathfield has cited independent evidence in support of that.

I checked via email. So unless Kaz's email account has been
cracked (which I doubt), it's the same guy, yes.

And no, he was not a student in Helsinki. CBFalconer has confused
him with Joona Palaste (who, like Kaz, was knowledgeable and
helpful).

That could be. Kaz is not, IMO.


Isn't this al way off topic?
Take it to a social news group.... :))))
 
C

Chris H

jaiprabhu said:
Let me give some input from someone who's a "regular" lurker on C.L.C
and someone who has gained enormously from the disucussions here. I am
not exactly a novice at C. In fact, I am quite proficient in the
language and I for one, absolutely agree with the "autistic-pedantry"
of C.L.C regulars who are in favor of:

1. Addressing queries that are strict C questions only


However they have an incorrect definition of what C is.
2. Point out what's implementation-specific, not C but C++,
platform-specific etc.

That appears to be a contradiction.
 
C

Chris H

Malcolm McLean said:
I think that half of the problem is that for newsgroup X topicality is
pretty easily understood by the outsider. However most C programmers,
certainly most novice C programmers, don't know what ANSI C is.

My first point proved in spades... This NG is for discussing ISO 9899
not some local US standard that was superseded about 19 years ago.
I remember
being puzzled for a long time by the absence of an fgetint() to match
fgetc() when I first learning C. So the boundaries of the standard language
and l.ibrary come as a surprise.

Nothing wrong with pointing that out. However there are ways of doing
it. Also saying the ISO (NOT ANSI) C way of doing it is this way and
what you have is a windows specific way. BTW the Unix version is this
way. Then everyone learns.
 
C

Chris H

Richard Heathfield said:
Chris H said:



No, that's comp.std.c. This group is for discussing the usage of the
language defined by ISO 9899.

And I thought I was being pedantic :))

No wonder I don't usually post here any more.
 
C

Chris H

Richard said:
No it's not. It's for discussing the C language. Hence
"comp.lang.c". And no amount of you or your new nemesis "Chuck" whining
will change that.

Get them to show you the Charter for this News Group :)
 
C

Chris H

Richard said:
I have seen the original "starter post" and it was for discussion of all
things C.

I thought so... I have been around the Internet and USENET about 20
years and I seem to recall in years gone by there were a lot fewer
pedants around here.
I am happy to help people use emacs with C for example. Others
will help with Eclipse etc. I could help people develop good gdb
debugging strategies to reduce development time.

I would probably send people to more suitable groups where they exist
for in-depth discussion but I get pissed off with some of the pedants on
here refusing to only talk about "standard-C" and get very stupid about
anything that does not fit their somewhat strange interpretation of what
standard C is.
Chuck and his mob can
kiss my fat arse as far as I'm concerned. I never saw such a bunch of
stuck up dickwads in years of accessing the wire for help and
information as in this group.

Well there are some other groups I could point you to but this one is
pretty bad.
Honestly, the make me mad....

I have stopped posting here for some time as it's not worth it.
 
C

CBFalconer

Chris said:
.... snip ...

My first point proved in spades... This NG is for discussing ISO
9899 not some local US standard that was superseded about 19
years ago.

surprise.

Nothing wrong with pointing that out. However there are ways of
doing it. Also saying the ISO (NOT ANSI) C way of doing it is
this way and what you have is a windows specific way. BTW the
Unix version is this way. Then everyone learns.

I thought you were on the ISO group. You mean you don't realize
than ANSI C is word for word identical with ISO 9899? There is no
such thing as a 'Unix version'.
 
S

stijnvandongen

Chris H said:



There really are people who get the two confused, hence the
pedantry.

Why not treat the worst case only in the rare event it actually
happens? If it happens unnoticed, not to worry.
You write as though pedantry were a bad thing. This is a point of
view that I've always struggled to understand.

Don't give up the good fight. A dictionary may prove useful.

Q. How many pedants does it take to change a light bulb?
A. Change or replace?

Stijn
 
K

Keith Thompson

Chris H said:
In message <[email protected]>, Richard


I thought so... I have been around the Internet and USENET about 20
years and I seem to recall in years gone by there were a lot fewer
pedants around here.
[...]

There were also a lot fewer newsgroups. For example, there was no
comp.std.c when comp.lang.c (actually net.lang.c) was created.
 

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,112
Latest member
VinayKumar Nevatia
Top