Maker Pro
Maker Pro

Computer programmers' habits in electronics

M

Mike Young

Spehro Pefhany said:
Something like this? (ugh)

#include <string.h>

rev2(char * istring, int start, int fin)

No. Something like this:

#include <algorithm>

void ReverseString(const char * pStr, size_t len = 0);
void ReverseString(const char * pStr, size_t len)
{
if (!pStr) return;
std::reverse(pStr, pStr + (len ? len : strlen(pStr)));
}
 
R

Rich Grise

Sure. I never use floating point in embedded stuff.

Who was it that said "If you have to use floating point, you don't
understand the problem"?

John

I once did an RMS in a 68HC11 by using LUTS, but realized on the bench
that it didn't make a significant difference in the regulation. It
was just a(an?) SCR phase control power supply (well, smart industrial
battery charger, 24V 40A), and so I had really loose tolerances to work
with, so I went with simple. Unfortunately, at the time, I missed the part
about "cheap", so it never made it to market. )-;

Thanks,
Rich
 
R

Rich Grise

It was a display problem, where we had a display of a certain known
size, and we wanted to plot a line on it that reflected an even-numbered
(10, 20, 50, 100, etc.) length. We told them that we wanted a procedure
that took the screen size in real-world coordinates, the desired size in
real-world coordinates, and the number of pixels in real-world
coordinates, and that coughed up the line size in pixels. It actually
came from a real product (sometimes we had system pictures in the
conference rooms & we could point to the box & say "it's in that there
box, as a matter of fact").

Am I allowed to use fractional-brigtness adjacent pixels, to simulate
dithering?

Thanks,
Rich
 
R

Rich Grise, but drunk

[will work for drugs, alcohol, and pussy...]
....
It was a display problem, where we had a display of a certain known
size, and we wanted to plot a line on it that reflected an even-numbered
(10, 20, 50, 100, etc.) length. We told them that we wanted a procedure
that took the screen size in real-world coordinates, the desired size in
real-world coordinates, and the number of pixels in real-world
coordinates, and that coughed up the line size in pixels. It actually
came from a real product (sometimes we had system pictures in the
conference rooms & we could point to the box & say "it's in that there
box, as a matter of fact").

If they didn't start that way themselves we'd encourage them to "just do
the math" first, and look for how well they did their 8th grade algebra
problems.

If at that point I'd started drawing little diagrams of pixels and bytes
and schtuff, what would my chances have been? ;-)

I once came up with a three-line Eratosthenes sieve on the back of an
envelope at a bus stop. I didn't memorize it, but it's almost trivial -
HEY!! I just had a profound revelation! This is where the power of
drawing a picture lies! Dood, it's so simple! I feel like this guy:

------------<begin excerpt from /usr/games/fortunes/fortunes2>-----------
Dr. Oliver Wendell Holmes of Harvard Medical School inhaled ether
at a time when it was popularly supposed to produce such mystical or
"mind-expanding" experiences, much as LSD is supposed to produce such
experiences today. Here is his account of what happened:
"I once inhaled a pretty full dose of ether, with the
determination to put on record, at the earliest moment of regaining
consciousness, the thought I should find uppermost in my mind. The mighty
music of the triumphal march into nothingness reverberated through my
brain, and filled me with a sense of infinite possibilities, which made me
an archangel for a moment. The veil of eternity was lifted. The one great
truth which underlies all human experience and is the key to all the
mysteries that philosophy has sought in vain to solve, flashed upon me in
a sudden revelation. Henceforth all was clear: a few words had lifted my
intelligence to the level of the knowledge of the cherubim. As my natural
condition returned, I remembered my resolution; and, staggering to my
desk, I wrote, in ill-shaped, straggling characters, the all-embracing
truth still glimmering in my consciousness. The words were these (children
may smile; the wise will ponder): `A strong smell of turpentine prevails
throughout.'"
-- The Consumers Union Report: Licit & Illicit Drugs
------------<end excerpt from /usr/games/fortunes/fortunes2>-----------

CHeers!
Rich
 
P

Pooh Bear

Bob said:
Yep. Electronics is just the same. Reach into a bag of parts and start
soldering. Bridge building is similar. You are almost certainly a troll,
but on the odd chance you aren't, I'll bet your code is pretty awful.

I assume your reply is a troll too ?

You're going to create some fabulously bad electronics your way.No different
for code either.

Graham
 
[will work for drugs, alcohol, and pussy...]

Well, this PROVES you're not gay, despite your fondness for musicals...

I once came up with a three-line Eratosthenes sieve on the back of an
envelope at a bus stop. I didn't memorize it, but it's almost trivial -

Three-line sieve of Eratosthenes? Do post do post!

It wouldn't happen to be...

int main(void){
DoSieve(); return 0;
}

, now, would it?

HEY!! I just had a profound revelation! This is where the power of
drawing a picture lies! Dood, it's so simple! I feel like this guy:

------------<begin excerpt from /usr/games/fortunes/fortunes2>-----------
Dr. Oliver Wendell Holmes of Harvard Medical School inhaled ether
at a time when it was popularly supposed to produce such mystical or
"mind-expanding" experiences, much as LSD is supposed to produce such
experiences today. Here is his account of what happened:
"I once inhaled a pretty full dose of ether, with the
determination to put on record, at the earliest moment of regaining
consciousness, the thought I should find uppermost in my mind. The mighty
music of the triumphal march into nothingness reverberated through my
brain, and filled me with a sense of infinite possibilities, which made me
an archangel for a moment. The veil of eternity was lifted. The one great
truth which underlies all human experience and is the key to all the
mysteries that philosophy has sought in vain to solve, flashed upon me in
a sudden revelation. Henceforth all was clear: a few words had lifted my
intelligence to the level of the knowledge of the cherubim. As my natural
condition returned, I remembered my resolution; and, staggering to my
desk, I wrote, in ill-shaped, straggling characters, the all-embracing
truth still glimmering in my consciousness. The words were these (children
may smile; the wise will ponder): `A strong smell of turpentine prevails
throughout.'"
-- The Consumers Union Report: Licit & Illicit Drugs
------------<end excerpt from /usr/games/fortunes/fortunes2>-----------

CHeers!
Rich


Hey, try Ketamine! Let us know if there's life (bacterial or other
animal) around any of the nearer stars (within 100 light-years)... that
is, if you make it back alive, that is...

http://en.wikipedia.org/wiki/Ketamine
 
R

Rich Grise, but drunk

that's what we want, reverse in place.

'Kay.

richgrise@thunderbird:~/Programming/C
$ cat simple-reverse.c
/* simple-reverse.c */
#include <stdio.h>
#include <string.h>

void reverse_a_string(char *);

int main(int argc, char **argv) {
char string[255];

strcpy(string,"Rich");
reverse_a_string(string);
printf("Output = '%s'\n", string);
exit(0);
}

void reverse_a_string(char *string) {
int i, j, c, d;

for (i = strlen(string) - 1, j = 0; i > j; i--, j++) {
c = string;
d = string[j];
string = d;
string[j] = c;
}
return;
}
richgrise@thunderbird:~/Programming/C
$ gcc simple-reverse.c -o simple-reverse
richgrise@thunderbird:~/Programming/C
$ simple-reverse
Output = 'hciR'
richgrise@thunderbird:~/Programming/C
$


Do I get the job? ;-)

Cheers!
Rich
 
R

Rich Grise, but drunk

for (i = strlen(string) - 1, j = 0; i > j; i--, j++) {
c = string;
d = string[j];
string = d;
string[j] = c;


It could probably be done a little less verbosely by writing a
simple swap(char1, char2) thing, but, well, that's really just
a matter of taking the above and putting it an a swap() routine.

Remember, I wanna telecommute, unless somebody wants to relocate
me. :)

Cheers!
Rich
 
M

Mike Young

Rich Grise said:
YGBSM.

OK, since it's a job app, lessee what I can come up with.

Um, do you want me to reverse it in place, or to create a reversed
copy on the heap?

Well, I think when he said "reverses a string in place", he meant the former
rather than the latter, and probably implied without creating a temporary
copy.
 
R

Rich The Philosophizer

Rich Grise, but drunk wrote: ....

Hey, try Ketamine! Let us know if there's life (bacterial or other
animal) around any of the nearer stars (within 100 light-years)...

Probably not, Earth is pretty much the seat of humanity. The UFOs and
shit are just angels and other lower astral entities. I've been to
heaven and back, and it's supremely boring - a vast, featureless, plain
plane plain, with one ivory tower. And a hole in the floor where I
blasted through once on the bow wave of an explosion of killing, flaming,
fuming, blaming rage, with intent to kill God.

I beat on the door of his ivory tower, and he finally showed up at the
door, an old, bedraggled-looking, scrawny, wrinkly guy, glasses at his
forehead, looking confused, rubbing sleep out of his eyes, and he
said, "Huh? Wha?"

I said "WHAT THE **** IS GOING ON HERE?!!!!!!"

He looked out at all of the pain and suffering and horror in the world,
and kinda went, "Oh, my Me!" like he had had no clue.

Then, like the fuckhead that he is, he sweeps his hand, and the harps
play, and he says, "Behold," and I behold that I've reached the light
at the end of the tunnel, and the answer is that the answer is at the
other end of the tunnel.
 
D

Dirk Bruere at Neopax

Rich said:
Probably not, Earth is pretty much the seat of humanity. The UFOs and
shit are just angels and other lower astral entities. I've been to
heaven and back, and it's supremely boring - a vast, featureless, plain
plane plain, with one ivory tower. And a hole in the floor where I
blasted through once on the bow wave of an explosion of killing, flaming,
fuming, blaming rage, with intent to kill God.

I beat on the door of his ivory tower, and he finally showed up at the
door, an old, bedraggled-looking, scrawny, wrinkly guy, glasses at his
forehead, looking confused, rubbing sleep out of his eyes, and he
said, "Huh? Wha?"

I said "WHAT THE **** IS GOING ON HERE?!!!!!!"

He looked out at all of the pain and suffering and horror in the world,
and kinda went, "Oh, my Me!" like he had had no clue.

Then, like the fuckhead that he is, he sweeps his hand, and the harps
play, and he says, "Behold," and I behold that I've reached the light
at the end of the tunnel, and the answer is that the answer is at the
other end of the tunnel.

Hell is more interesting, and useful, but not a nice place.

--
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org
 
R

Rich Grise

Hey, try Ketamine! Let us know if there's life (bacterial or other
animal) around any of the nearer stars (within 100 light-years)... that
is, if you make it back alive, that is...

http://en.wikipedia.org/wiki/Ketamine

Nah, sounds like it just gives you all of the clumsiness with none of
the pleasurable parts.

I accidentally smoked some PCP once. Well, twice, me and another guy
were sitting at another friend's house while the friend borrowed
guy B's Jeep to move some stuff. So, we were sitting at the kitchen
table, and spot the guy's baggie, and my bud says, "Wanna smoke one?"
And I said, "Sure, why not?" so he rolled one up and we smoked it, and
he says, "Are you high yet?" and I said, "not really", since I was
very young and had not yet learned to say, "Let's give it a minute or
two, and see what we feel like", so he rolled up another one, and we
started it.

About half-way through that second doobie, it felt like my brain had
suddenly been hit by a ton of bricks. I got terrible tunnel-vision,
I got paranoid, I got anesthesized with none of the benefits, it was
TERRIBLE!

So I don't do PCP, and Ketamine sounds pretty similar, so, thanks but
no thanks.

LSD, on the other hand... %-}

Cheers!
Rich
[if only it were possible to get real LSD these days]
 
S

Spehro Pefhany

No. Something like this:

#include <algorithm>

void ReverseString(const char * pStr, size_t len = 0);
void ReverseString(const char * pStr, size_t len)
{
if (!pStr) return;
std::reverse(pStr, pStr + (len ? len : strlen(pStr)));
}

Isn't that kinda cheating?

Hm... not so different from my one (aside from no reentrancy, of
course):


template<typename _RandomAccessIterator>
void
__reverse(_RandomAccessIterator __first, _RandomAccessIterator
__last,
random_access_iterator_tag)
{
while (__first < __last)
std::iter_swap(__first++, --__last);
}




Best regards,
Spehro Pefhany
 
B

Baxter

The code should be self-documenting, the comments should tell something
(like the "why") that the code does not tell.
 
S

Spehro Pefhany

<snip>

P.S., I don't think you want those 'const' thingies up there..


Best regards,
Spehro Pefhany
 
I

Ignoramus32515

Someone who likes to ask that question told me of an interviewee who got
as far as saying "I think it involves recursion...".

So I wrote a version that did it using recursive function calls and sent
it to her. I don't know if I would have gotten the job -- they had
already made the mistake of hiring me.

Something like this? (ugh)

#include <string.h>

rev2(char * istring, int start, int fin)
{
char t;
t = istring[start];
istring[start] = istring[fin];
istring[fin] = t;
start++; fin--;
if (fin > start) rev2(istring, start, fin);
}

how about this, untested, uncompiled c++

void rev3( const char* str, int l = -1 )
{
if( l == -1 ) l = strlen( str )-1;

if( l > 0 ) {
char c = str[l]; str[l] = *str; *str = c; // swap
rev3( str+1, l-2 );
}
}

the nice thing about it is that one does not have to supply
length, start or fin for initial invocation. Not that I think that
recursion is appropriate for this problem.

In your example, the user would need to supply strlen( l ) - 1 for
fin, for the initial call. And in my example, the user would only
supply str.

A bonus question: how to swap two integer (or char) values without
using a temporary variable? In any language? Without any function
calls?

i
...

/* reverse the string in place */
rev2(mystring, 0, strlen(mystring)-1);

...



Here's one I did to approximate e^(-x) over the range 0 .. 0.75
(the end product would be written in assembler**, the C is just to
explain).




where xf, yf are 32-bit integers.

xf = x * 128;
yf = 32768 - xf + (xf* xf)/65536 - (((xf * xf)/32768
)*xf)/(6*32768) + (((xf*xf)/32768)* ((xf*xf)/32768))/(32768 * 24)
- (((((xf*xf)/32768)* ((xf*xf)/32768))/32768) * xf)/(32768 * 120)
;

The result is ~= exp(-x/256) * 32768 over the range
x = 1 to 192.

yf/32768 agrees with exp(-x/256) within 0.001 over that range.


You can easily extend this to additional terms, although it gets a bit
tedious.

It's based on the textbook Taylor/Maclaurin series

infinity
\
exp(x) = > (x^n)/n!
/
n= 0


** actually assembler macros, but that's another story



Best regards,
Spehro Pefhany


--
 
I

Ignoramus32515

YGBSM.

OK, since it's a job app, lessee what I can come up with.

Um, do you want me to reverse it in place, or to create a reversed
copy on the heap?

In place. Try to do it so that you could say afterwards, that was a
ncie solution.

i
 
I

Ignoramus32515

that's what we want, reverse in place.

'Kay.

richgrise@thunderbird:~/Programming/C
$ cat simple-reverse.c
/* simple-reverse.c */
#include <stdio.h>
#include <string.h>

void reverse_a_string(char *);

int main(int argc, char **argv) {
char string[255];

strcpy(string,"Rich");
reverse_a_string(string);
printf("Output = '%s'\n", string);
exit(0);
}

void reverse_a_string(char *string) {
int i, j, c, d;

for (i = strlen(string) - 1, j = 0; i > j; i--, j++) {
c = string;
d = string[j];
string = d;
string[j] = c;
}
return;
}
richgrise@thunderbird:~/Programming/C
$ gcc simple-reverse.c -o simple-reverse
richgrise@thunderbird:~/Programming/C
$ simple-reverse
Output = 'hciR'
richgrise@thunderbird:~/Programming/C
$


Do I get the job? ;-)


You would get the job, yes, I mean you would pass this test. I must
note, however, that you would get a compiler warning about assigning
integer to a char, and also you do not need both c and d. The last
return statement is also not needed. You are definitely in the top 90%
of computer programmers, no kidding.

i
 
Top