The following code fragment from Programming Pearls:

for i = [0, n)
       if comlen(a[i], a[i+1]) > maxlen
              maxlen = comlen(a[i], a[i+1])
              maxi = i

I am not sure why he called the comlen function twice? Instead he could save the computed value in a temporary and use it to assign to maxlen. Isn't the function being called unnecessarily for the second time?

Recommended Answers

All 11 Replies

That completely depends on why he called the function twice. Not enough information for a solid guess.

The following code fragment from Programming Pearls:

for i = [0, n)
       if comlen(a[i], a[i+1]) > maxlen
              maxlen = comlen(a[i], a[i+1])
              maxi = i

I am not sure why he called the comlen function twice? Instead he could save the computed value in a temporary and use it to assign to maxlen. Isn't the function being called unnecessarily for the second time?

Quick question is this syntax not unusual for C ? Or is this some old/new version ?

/* longdup.c -- Print longest string duplicated M times */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int pstrcmp(char **p, char **q)
{ return strcmp(*p, *q); }
int comlen(char *p, char *q)
{ int i = 0;
while (*p && (*p++ == *q++))
i++;
return i;
}
#define M 1
#define MAXN 5000000
char c[MAXN], *a[MAXN];
int main()
{ int i, ch, n = 0, maxi, maxlen = -1;
while ((ch = getchar()) != EOF) {
a[n] = &c[n];
c[n++] = ch;
}
c[n] = 0;
qsort(a, n, sizeof(char *), pstrcmp);
for (i = 0; i < n-M; i++)
if (comlen(a[i], a[i+M]) > maxlen) {
maxlen = comlen(a[i], a[i+M]);
maxi = i;
}
printf("%.*s\n", maxlen, a[maxi]);
return 0;
}

Unfortunately(fortunately!!), there is not only one way to write a program for a given requirement. You are right. He could have assigned to a variable and then used it later. But, he might have thought that "Why do I need to use another variable unnecessarily? Instead I could call this function twice. Cos, I think this function is not a heavy one. It justs compares length". It completely depends on developer's choice.

~RDX~

This guy is like a purist. He himself wrote a book "writing efficient code" which he claims to be his favorite book. This guy was the phd supervisor of one of the co-authors of our favorite CLRS algorithm book. I always take whatever this person says pretty seriously. So I am not sure whether there is some trick behind this.

Oops! sorry sir! I just realised this guy was my supervisor when I did my PhD in CLRS algorithm. This guy drafted the CLRS algorithm and thought me. I forgot the golden rule "Never overcome your master!".

I wish programming is so simple so that people gets PhD for solving simple stuffs so that every programmer will be holding a PhD. I dont claim any books as my favorite one and I dont supervise too!! I just develop "INEFFICIENT" code!

I am a bad student. I should have learned from this guy properly!

Anyhow, PEACE! :)

~RDX~

Oops! sorry sir! I just realised this guy was my supervisor when I did my PhD in CLRS algorithm. This guy drafted the CLRS algorithm and thought me. I forgot the golden rule "Never overcome your master!".

I wish programming is so simple so that people gets PhD for solving simple stuffs so that every programmer will be holding a PhD. I dont claim any books as my favorite one and I dont supervise too!! I just develop "INEFFICIENT" code!

I am a bad student. I should have learned from this guy properly!

Anyhow, PEACE! :)

~RDX~

Your comment is not the least bit funny.

And I appreciate your trying hard to be funny. You just posted 14 times already. I hope by the time you post like 100 posts you would learn how to be funny.

How can I try to be funny?!! No matter how hard I try I cant be as funny as you! :) You are the best comedian! My 14 posts are only in perl + one in your funny thread and I saw you try to be funny in different fields!! Keep going. We need some post like yours inbetween some serious tech discussion!

~RDX~

How can I try to be funny?!! No matter how hard I try I cant be as funny as you! :) You are the best comedian! My 14 posts are only in perl + one in your funny thread and I saw you try to be funny in different fields!! Keep going. We need some post like yours inbetween some serious tech discussion!

~RDX~

To be funny you need to have some sense of humor. My original was not meant to be funny at all. And you try to think it to be funny and make fun of me and only you are the only smartass who can laugh at your own silly joke. And the result is you being a pest and a clown. Do me a favor, stop showing your clown shit-face to my post ever.

As I stated earlier you are the best comedian, no matter how serious your posts are, you still seems to be funny. I am the only guy who use that smiley to express how funny you are but the people reading your doesnt need to express how funny you are to them.

Ah, one more thing. Now I realized why you said that you need 100 posts to know how to be funny. You are nearing it. Right? As per your theory and your practical experience shows how to be funny. Keep rocking smarter-ass!

Do me a favor, your 100 is nearing and you have proved yourself, now stop being seriously funny by posting shit comments!

~RDX~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.