473,699 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

interesting C program

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
Nov 20 '07
66 3670
Rachael <ra************ @gmail.comwrite s:
There's the silly solution:

int i=1;
/* and then write out INT_MAX times: */
if (i<=N) {
printf("%d\n",i );
i++;
}
Why use a variable then?

if (i <= 1)
puts("1");
if (i <= 2)
puts("2");
if (i <= 3)
puts("3");
....
--
"If I've told you once, I've told you LLONG_MAX times not to
exaggerate."
--Jack Klein
Nov 20 '07 #11
prady <pr*******@gmai l.comwrites:
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.
Need it be portable?

void function(int n)
{
char cmd[64];
sprintf(cmd, "seq %d", n);
system(cmd);
}
--
char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
=b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}
Nov 20 '07 #12
Rachael <ra************ @gmail.comwrite s:
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.
I'd love to see some of the questions that a few of the core members of
this group would set the hapless interviewees :-;
Nov 20 '07 #13
Chris Dollin wrote:
[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.]
There was.

Shiny! That fixed the 0 case, too!

--
Chris "money for old rope" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Nov 20 '07 #14
On 20 Nov, 15:55, Ben Pfaff <b...@cs.stanfo rd.eduwrote:
Why use a variable then?

if (i <= 1)
puts("1");
if (i <= 2)
puts("2");
if (i <= 3)
puts("3");
...
Assuming you mean
if (N >= 1)
then yes, you're right.
Nov 20 '07 #15
Ben Pfaff <bl*@cs.stanfor d.eduwrites:
Rachael <ra************ @gmail.comwrite s:
>There's the silly solution:

int i=1;
/* and then write out INT_MAX times: */
if (i<=N) {
printf("%d\n",i );
i++;
}

Why use a variable then?

if (i <= 1)
puts("1");
if (i <= 2)
puts("2");
if (i <= 3)
puts("3");
...
Because for a program which covers all integers from 0 to MAX_INT N is
the cut off.

Nov 20 '07 #16
Ben Pfaff wrote:
prady <pr*******@gmai l.comwrites:
>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.

Need it be portable?

void function(int n)
{
char cmd[64];
sprintf(cmd, "seq %d", n);
system(cmd);
}
If you're not worried about portability, then look at my program:

void _G(int n);

void f(int n) {
_G(n);
}

Naturally, _G() is to be provided by the implementation.
Nov 20 '07 #17
Rachael schrieb:
There's the silly solution:
[Snip]

That's what I thought first, but couldn't really believe they were
asking for that.
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.
Most braindead assignment *ever*, seriously. Whoever asks these
questions should not be allowed to teach/have a leading position in HR.
BTW, did you get the job anyways?

Greetings,
Johannes

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerli ch. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathemat ik <47************ **********@news .sunrise.ch>
Nov 20 '07 #18
On Tue, 20 Nov 2007 06:55:08 -0800, 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.
Hint: look up the standard library functions setjmp and longjmp.
Nov 20 '07 #19
On 20 Nov, 17:21, Johannes Bauer <dfnsonfsdu...@ gmx.dewrote:
Most braindead assignment *ever*, seriously. Whoever asks these
questions should not be allowed to teach/have a leading position in HR.
BTW, did you get the job anyways?
The other questions in the interview were very interesting. I think
with that question they wanted to see whether I was blind to the
"braindead" solution when there were no other options.
I didn't get the job, but it's OK, because it was about an hour away
by train and my current job is 15 minutes away by bike :)
Nov 20 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1675
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 on an entirely different way (such as a non-binary computer)? It wouldn't be very useful, but...
15
2091
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: http://www.acmqueue.com/modules.php?name=Content&pa=showpage&pid=271&page=3 Cheers,
8
1575
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;
1
1761
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 switches the order around, and of course the validator I built then fails. Its always the same two...
56
4108
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 configured to aggressively exercise the section that the bug was in.) It was easy enough to trap the...
7
3028
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> 2 #include<conio.h>
16
1721
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
1
2200
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 not a bug, however, it would be nice if there
2
1239
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 calls to the function to a few thousand) I get a system.stackoverflowexception. I'm assuming I've...
27
2332
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 replace, where possible, any usages of "strlen" with something like: struct PtrAndLen { char *p;
0
8685
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2008
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.