hi all,
could any one solve the following C program. If any one knows the
answer please post it
Ques:
A C function that will print 1 to N one per each line on the
stdout , where N is a int parameter to the function. The function
should not
use while, for, do-while loops, goto statement, recursion, and switch
statement.
--
prady 66 3637
In article
<ca************ *************** *******@b40g200 0prf.googlegrou ps.com>,
prady <pr*******@gmai l.comwrote on Tuesday 20 Nov 2007 8:25 pm:
hi all,
could any one solve the following C program. If any one knows the
answer please post it
This is not a homework group.
<snip absurd exercise>
Previous on Buffy the Vampire Slayer, oops I meant,
comp.please.do. my.homework prady wrote:
hi all,
could any one solve the following C program. If any one knows the
answer please post it
Ques:
A C function that will print 1 to N one per each line on the
stdout , where N is a int parameter to the function. The function
should not
use while, for, do-while loops, goto statement, recursion, and switch
statement.
This was discussed a while back, I personally can't see the point of the
question. I expect some perverted mind will come up with a solution, but
I wouldn't be surprised to find their solution flawed/non-portable.
int PRINT (int N)
{
if(N>0)
PRINT (N-1);
printf("%d\n",N );
return 0;
}
Mark Bluemel wrote:
Previous on Buffy the Vampire Slayer, oops I meant,
comp.please.do. my.homework prady wrote:
>hi all,
could any one solve the following C program. If any one knows the answer please post it
Ques:
A C function that will print 1 to N one per each line on the stdout , where N is a int parameter to the function. The function should not use while, for, do-while loops, goto statement, recursion, and switch statement.
This was discussed a while back, I personally can't see the point of the
question.
To make the student thing & explore the possibilities ...
I expect some perverted mind will come up with a solution,
Thank you, kind sir.
but I wouldn't be surprised to find their solution flawed/non-portable.
Actually I believe it's portable and completely straightforward . Also
an utterly stupid way of printing 1-to-N lines, but that's a different
issue.
Note that I'm not saying what the solution /is/, since I too believe
that Prady will get more benefit from working out the solution themself
than from having one of us provide it.
Here's a clue: it requires functions from C's standard library.
--
Chris "cosh - no. Kosh - 'you do not understand, but you will.'" Dollin
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
daya wrote:
int PRINT (int N)
{
if(N>0)
PRINT (N-1);
printf("%d\n",N );
return 0;
}
What part of "The function should not use while, for, do-while loops,
goto statement, /recursion/, and switch statement" [emphasis mine]
don't you understand?
[Why is `PRINT` spelled with CAPS when it's not a MACRO?]
--
Chris "my HAT isn't a MACRO either" Dollin
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
In article <fh**********@t adcaster.hpl.hp .com>,
Chris Dollin <ch**********@h p.comwrote:
>>A C function that will print 1 to N one per each line on the stdout , where N is a int parameter to the function. The function should not use while, for, do-while loops, goto statement, recursion, and switch statement.
[...]
>Actually I believe it's portable and completely straightforward . Also an utterly stupid way of printing 1-to-N lines, but that's a different issue.
Can you see any point to the prohibition of switch statements?
-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
There's the silly solution:
int i=1;
/* and then write out INT_MAX times: */
if (i<=N) {
printf("%d\n",i );
i++;
}
I once failed an interview question very similar to this, because I
couldn't imagine they were looking for such an inelegant "solution",
but they were. ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <fh**********@t adcaster.hpl.hp .com>,
Chris Dollin <ch**********@h p.comwrote:
>>>A C function that will print 1 to N one per each line on the stdout , where N is a int parameter to the function. The function should not use while, for, do-while loops, goto statement, recursion, and switch statement.
[...]
>>Actually I believe it's portable and completely straightforward . Also an utterly stupid way of printing 1-to-N lines, but that's a different issue.
Can you see any point to the prohibition of switch statements?
-- Richard
To encourage students to think "out of the box" pretty much like any
teaching course. Most exercises given on courses have almost no
practical usage in their own right. The idea is to stimulate the student
into thinking about the language and it's features.
Richard Tobin wrote:
In article <fh**********@t adcaster.hpl.hp .com>,
Chris Dollin <ch**********@h p.comwrote:
>>>A C function that will print 1 to N one per each line on the stdout , where N is a int parameter to the function. The function should not use while, for, do-while loops, goto statement, recursion, and switch statement.
[...]
>>Actually I believe it's portable and completely straightforward . Also an utterly stupid way of printing 1-to-N lines, but that's a different issue.
Can you see any point to the prohibition of switch statements?
It might be to prevent the student from wasting their time thinking of a
way of using them; otherwise, no.
[I've now managed to turn my insight into code. Perhaps "completely
straightforward " was a little optimistic, eg there's an `if` in there
whose condition is always true ... I think there's room for improvement.]
--
Chris "and another one which isn't" Dollin
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bruno R. Dias |
last post by:
Perhaps it would be interesting to program a virtual machine simulating
an ancient computer (such as the pdp-7). Then, it would be rather
interesting to code for it (porting gcc to it maybe?). I think it would
be fun to play with the long-forgotten art of coding in machine language.
And what about a fictional computer, such as one that works...
|
by: Nick Coghlan |
last post by:
Thought some folks here might find this one interesting. No great revelations,
just a fairly sensible piece on writing readable code :)
The whole article:
http://www.acmqueue.com/modules.php?name=Content&pa=showpage&pid=271&page=1
The section specifically on white space:...
|
by: David Sachs |
last post by:
The following program illustrates an interesting effect of the way C++
resolves function overloading.
I have verified with a member of the C++ stardard committee that the output
shown is correct.
*********************************Program********************************
#include <iostream>
using std::cout;
|
by: jrmsmo |
last post by:
Hi there, I have an interesting problem that maybe you pros can suggest how I solve.
I'm working with a third party program that serializes an XML document (it was obviously not designed with schema in mind). I created a schema from this document. It works fine. Except for some unknown reason, in a small part of the XML document, this program...
|
by: Dave Vandervies |
last post by:
I just fixed a bug that some of the correctness pedants around here may
find useful as ammunition.
The problem was that some code would, very occasionally, die with a
segmentation violation error. (Not as infrequent as some bugs that
have been discussed here in the past, but maybe once in an overnight
run of the program when it was...
| |
by: git_cs |
last post by:
Hey, guys and gals
Somedays ago, I had asked for the DES algorithm in C language.
Although I have written the algorthim in C myself, I am facing a
peculiar problem, which I hope some of u guys and gals solve.
I use Turbo C++ version 3.0 and WINXP as the operating system.
Pls observe the following program.
1 #include<stdio.h>...
|
by: makko |
last post by:
Hello,
anyone know how to writre a program that take a commandline formula
and prints the calculated result? example;
$program 1+(2x3(3/2))-8
reagrds;
Makkko
|
by: Jeff Gerber |
last post by:
/*
This test program will give a "Value cannot be null" error.
If the lock in this code is removed (or Monitor.Enter()) the program will
run as expected.
I have found no explaination in lock() or Monitor.Enter() documentation as
to why this occurs. I suspect that it is by design of the behind-the-scenes
process that lock uses and is...
|
by: Michael Sutherland |
last post by:
Here's an interesting problem I've come across.
I'm writing a program that essentially generates random strings (its a
simulator of the game Boggle) and sends them to a function that does a
binary search on a word list to see if that string is listed. However, at
seemingly random intervals (I've seen it happen on everything from a few
dozen...
|
by: Frederick Gotham |
last post by:
I thought it might be interesting to share experiences of tracking down a
subtle or mysterious bug. I myself haven't much experience with tracking
down bugs, but there's one in particular which comes to mind.
I was writing usable which dealt with strings. As per usual with my code, I
made it efficient to the extreme. One thing I did was...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |