Do you consider writing this program a bit of a challenge?

Click For Summary
The discussion centers on the challenge of writing a program that takes three integer inputs and orders them in ascending order using only if statements, which some consider difficult for beginners. The task is derived from Bjarne Stroustrup's book, "Programming Principles and Practice Using C++," which has received mixed reviews compared to Tony Gaddis's more beginner-friendly book. Participants highlight the common pitfalls of swapping numbers, emphasizing the importance of understanding variable manipulation and memory management. The conversation also touches on the necessity for beginners to attempt coding solutions themselves to gain confidence and understanding. Overall, the task is viewed as a foundational exercise that can be deceptively tricky for novice programmers.
  • #31
You forgot self-modifying code. That was the best!
 
Last edited:
  • Like
Likes anorlunda
Technology news on Phys.org
  • #32
Vanadium 50 said:
You forgot self-mofifying code. That was the best!
Wow, @Vanadium 50, that brings back memories ##-## in the mid-'80s I was working as a mainframe systems engineer for a manufacturer of 'IBM mainframe clones' ##-## Gene you-know-who was the founder of the company ##-## we had a problem, in that our otherwise plug-to-plug compatible product, in the running of a particular database product (known as Model nnn ##-## you presumably know the nnn digits) ran fine on the IBM box, but ran extremely slowly on ours ##-## we had a pipelined processor ##-## the Mnnn code called subroutines sometimes hundreds of levels deep ##-## the code was 'optimized' so that a calling routine would modify the STM instruction at the start of the subroutine to save only the registers the contents of which it anticipated would be actually modified in the subroutine ##-## that meant that our machine had to flush the pipeline, etc., and it was a bit of a nightmare ##-## fortunately, the Mnnn vendor had an option to compile/assemble without the STM optimization, and that allowed our customers to carry on well enough . . .
 
  • Like
  • Informative
Likes hmmm27 and anorlunda
  • #33
I actually met Gene Amdahl as a young man. Twice, in fact. Spooky smart.

Never used x204 or whatever you were talking about. Did use DB2 in its early days, before going on to grad school. Even then I could see the writing was on the wall for mainframes as they existed in the 80's. A 3090 or 5060 was ~100x as fast as a single 8087 chip. But it cost a few million dollars and not a few hundred.
 
  • Like
Likes anorlunda and sysprog
  • #34
berkeman said:
snoopy small 2.jpg
 
  • Haha
  • Like
Likes harborsparrow and sysprog
  • #35
Vanadium 50 said:
You forgot self-modifying code. That was the best!
Oh yes, I once encountered a page of Cobol dense with the infamous ”alter goto”, which did just what the name suggests. No comments or doc of any kind, of course.
 
  • Wow
  • Like
Likes Vanadium 50 and sysprog
  • #36
PAllen said:
Vanadium 50 said:
You forgot self-modifying code. That was the best!
Oh yes, I once encountered a page of Cobol dense with the infamous ”alter goto”, which did just what the name suggests. No comments or doc of any kind, of course.
##\dots## lest we forget ##\dots##
Ed Post; Real Programmers Don't Use Pascal; DATAMATION July 1983 pp. 263-265 (Readers' Forum) said:
  • Real Programmers write self-modifying code, especially if they can save 20 nanoseconds in the middle of a tight loop.
ref: http://homepages.inf.ed.ac.uk/rni/papers/realprg.html
 
  • #37
Vanadium 50 said:
saves only a little bit of a resource there is plenty of

Even now there are still some systems (such as embedded systems) where memory is not such a resource. Also, if you're creating a large enough number of objects of some type, saving just a few bytes of memory per object can add up.
 
  • Like
Likes Vanadium 50 and sysprog
  • #38
PAllen said:
Oh yes, I once encountered a page of Cobol dense with the infamous ”alter goto”, which did just what the name suggests. No comments or doc of any kind, of course.
I had to look that up, because I didn't believe it did what I thought it did. It did. It's not quite as bad as INTERCAL's COME FROM operation, but that language is meant to be a joke.
 
  • Like
Likes sysprog
  • #39
PAllen said:
I once encountered a page of Cobol dense with the infamous ”alter goto”, which did just what the name suggests.

This makes me glad that I've never had to program in Cobol. :wideeyed:
 
  • Like
Likes Mark44, Vanadium 50 and sysprog
  • #40
Ibix said:
I had to look that up, because I didn't believe it did what I thought it did. It did. It's not quite as bad as INTERCAL's COME FROM operation, but that language is meant to be a joke.
INTERCAL is funny -- using PLEASE DO NOT IGNORE COME FROM risks an error for overpolteness ##\dots##

1608804433923.png
 
  • #41
PeterDonis said:
This makes me glad that I've never had to program in Cobol. :wideeyed:
That was dropped in '85.
 
  • #42
PeterDonis said:
Even now there are still some systems (such as embedded systems) where memory is not such a resource.

Well, I might argue that this is not a good reason to avoid C++ strings in favor of C strings. It might be a good reason to avoid C++ in favor of C. :wink:

However, embedded systems aren't what they used to be. The Broadcom SOC chip used for the Raspberry Pi Zero can't cost more than a buck or two and it has 512K of memory. My thermostat runs unix for heaven's sake!

As fas as the in-memory swap, I can't imagine any modern compiler not assigning the temp variable to a register. You don't really save the memory because it was never used in the first place.

I'll concede that we can find an edge case where using C-strings over std::string can make sense. (I'm not sure the answer is "overhead in a zillion objects", though) But I think it is far more likely that edge case involves legacy code than saving one byte out of thousands.
 
  • Like
Likes pbuk and sysprog
  • #43
Vanadium 50 said:
You forgot self-modifying code. That was the best!
Yeah, I remember once when I was in my early days at NASA my boss and I had to write code to capture digital telemetry data, massage it, and transfer it to digital tape. After a fair amount of refinement we got it down to about 130% of real-time. Problem was, the data didn't care about us, it was going to come in at real-time whether we could capture it or not. So we resorted to program self modification at the machine level (assembly language). and FINALLY got it down just below 100% which at first had not seemed possible.
 
  • Like
Likes sysprog
  • #44
If we're thinking of small resources like embedded systems, don't forget that the use of an OS and a high level language that brings in libraries, adds far more overhead than the differences between tight versus sloppy coding.

In some cases, we opt out of both OS and high level languages to work with a bare machine. In other cases, we might use a frugal environment such as Forth.

Just slightly before my time: Around 1964, GE successfully implemented closed loop digital boiler control for a power plant in Huntington Beach California. The computer had 3 words of 24 bits each to work with. The rest resided on the drum memory.
 
  • Like
Likes Merlin3189 and sysprog
  • #46
One issue with optimization is that today's CPUs are so complex it's possible to outclever yourself while attempting to outclever them. I remember a poster session where a grad student explained that she had cleverly optimized her code for the CPU (I think it was a KNL) causing it to do more work in less time. That in turn generated more heat. That in turn caused the CPU to throttle its clock.

The net effect was no effect.
 
  • Haha
  • Informative
  • Love
Likes jtbell, sysprog, berkeman and 2 others
  • #47
Vanadium 50 said:
I actually met Gene Amdahl as a young man. Twice, in fact. Spooky smart.
I posted this about 5 years ago on a Police forum:
Mainframe Pioneer Gene Amdahl Passes On at Age 92
11-16-2015, 05:19 AM

It was a few days ago but I just found out about it.

http://nytimes.com/2015/11/13/techno...?_r=0&referer=

I worked as a Systems Engineer for Amdahl Corp..
I wept on learning of his passing.
He was a great man.

Every day the Police use technology he played crucial roles in
conceiving, designing, and developing over a span of decades.

The next few times you run anyone's name through NLETS or NCIC,
or any mainframe-based system, please think of Gene Amdahl.

1608941362921.png

RIP Mr. Gene Amdahl Sir -- your great work still works -- God bless you and yours.

1608942189965.png
 
  • #48
"Do it without a temp variable". Somewhere I've seen it. Something like this?

x = 3;
y = 4;
x = x + y; // x now is 7
y = x - y; // y now is 3
x = x - y; // x now is 4

But IMO it is not reasonable to ask all students to come up with this. Only a few will be able to, and I only because I saw it done by a student once.
 
  • #49
harborsparrow said:
But IMO it is not reasonable to ask all students to come up with this. Only a few will be able to, and I only because I saw it done by a student once.
Does that include having seen @Vanadium 50 do it in post #29 ? :rolleyes:
 
  • Love
Likes harborsparrow
  • #50
phinds said:
Does that include having seen @Vanadium 50 do it in post #29 ?

Oops! Missed that. And the reason for preferring XOR. Doh!
 

Similar threads

Replies
69
Views
9K
  • · Replies 54 ·
2
Replies
54
Views
5K
Replies
81
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
8
Views
3K
Replies
9
Views
3K
Replies
6
Views
3K
Replies
5
Views
7K
  • · Replies 17 ·
Replies
17
Views
4K