HP3000-L Archives

March 2003, Week 4

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Keven Miller <[log in to unmask]>
Reply To:
Keven Miller <[log in to unmask]>
Date:
Sun, 23 Mar 2003 00:35:29 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (73 lines)
Well that didn't take too long.

1. "Hello World" without semicolon.

My answer was the same as Stan's and Tom's.
printf is really a function returning the count of characters
printed. Or (-1) when an error occurs -- so this code
could fail to generate the requested text, if ran
with stdout redirected to a read only device.

#include <stdio.h>
int main ()
{
    if ( printf ( "Hello World\n" ) > 0 )
    {
    }
}



2. A program without using any loop (if, for, while etc) to print
numbers from 1 to 100 and 100 to 1;

As stated below in my response, the interpretation of "loop" and "if"
affects your answer.  As Wirt provided, generating 200 print statements
requires no interpretation at all.  Bill and Stan hit on the same
theme I did with recursion.
Bill's solution uses the (?:) construct to control the loop.
Stan's and my solution use the condition expression for the control.
[using the argument that its an expression, not an "if"]


[Keven's response]
Besides the multi-print approach as Andy stated, problem one
had me curious to find another answer.  Which depends on
your definition of "loop" and "if" statements.

This code below uses recursion for the iteration of 1 to 100
and a condition expression to control the "loop"ing.
The "if" keyword is not used, but must be implied to end
the recursion.
(Well, "must" until proven otherwise.
 Like Marty in Back to the Future being called Yellow,
 I just can't stand the statement, It can't be done.)

#include <stdio.h>

int proc1 ( int n )
{
   printf ( "%3d\n", 100 - n );
   return ( n && proc1 ( n-1 ));
}
int proc2 ( int n )
{
   printf ( "%3d\n", 1 + n );
   return ( n && proc2 ( n-1 ));
}
int main ()
{
   proc1 ( 99 );
   proc2 ( 99 );
}

Keven Miller
[log in to unmask]

ps: Still pondering the last challenge that Tom repeated.
Write the same program ("Hello, World!") in C89 which contains at least
one comment, but does not contain the sequence "*/".

* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *

ATOM RSS1 RSS2