473,698 Members | 2,873 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 3669
Ben Bacarisse wrote:
I think it also pushes the requirement that "N is a int parameter to
the function" (sic) a bit far. Still, neither is a fatal flaw to the
deliciously sneaky idea:
(fx:snip)
int print_one(const void *a, const void *b)
(fx:ditto)
void print(int n)
Two functions. In this (sub)thread, one of the Dollins claims
that's out-of-spec, since the original post called for /a/
function.

--
A /And/ B Hedgehog
The shortcuts are all full of people using them.

Nov 24 '07 #61
Chris Dollin <eh@electriched gehog.netwrites :
Ben Bacarisse wrote:
>I think it also pushes the requirement that "N is a int parameter to
the function" (sic) a bit far. Still, neither is a fatal flaw to the
deliciously sneaky idea:

(fx:snip)
>int print_one(const void *a, const void *b)

(fx:ditto)
>void print(int n)

Two functions. In this (sub)thread, one of the Dollins claims
that's out-of-spec, since the original post called for /a/
function.
Harsh, very harsh.

--
Ben.
Nov 24 '07 #62
Ben Bacarisse wrote:
Chris Dollin <eh@electriched gehog.netwrites :
>Ben Bacarisse wrote:
>>I think it also pushes the requirement that "N is a int parameter to
the function" (sic) a bit far. Still, neither is a fatal flaw to the
deliciously sneaky idea:

(fx:snip)
>>int print_one(const void *a, const void *b)

(fx:ditto)
>>void print(int n)

Two functions. In this (sub)thread, one of the Dollins claims
that's out-of-spec, since the original post called for /a/
function.

Harsh, very harsh.
What would you expect from someone who had already devised a
single-function solution? They're probably just ratty about
people who's solutions aren't just like their own.

--
What? Hedgehog
Notmuchhere: http://www.electrichedgehog.net/

Nov 25 '07 #63
Chris Dollin wrote:
>
Richard Heathfield wrote:
Chris Dollin said:

<snip>
`print` is recursive.
That's hardly Peter's fault. He's just calling a standard library
function.

I have no problem with that. But it makes `print` recursive,
and hence disallowed by the original specification.
Must print() call qsort() to itself? Couldn't it call a different
function?

#include <stdio.h>
#include <stdlib.h>

static int mymax; /* Specs didn't say "no global variables" */

int print(const void *a, const void *b) {
static int current = 0;
if (current < mymax) printf("%d\n", ++current);
return 0;
}

void doit(int i) {
void *ptr = malloc(i+1); /* Should have test for failure */
mymax = i;
qsort(ptr,mymax +1,1,print);
free(ptr);
}

int main(int argc, char *argv[]) {
int val;
sscanf(argv[1], "%d", &val);
doit(val);
return 0;
}
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Nov 26 '07 #64
Why not something more simple ?

Like a sub calling itself.

------------8<----------------------
#include<stdio. h>

void printchar(char *mychar, int number)
{
int newnumb;
printf("%s\n", mychar);
if (number 1) {
newnumb = number - 1;
printchar(mycha r, newnumb);
}
}

int main(void)
{
printchar("a",1 0); // Print character "a", 10 times! ("a" can be a
character string too eg: "hello world")
return 0;
}
------------------------------------
Nov 26 '07 #65
Benoit Lefebvre wrote:
Why not something more simple ?

Like a sub calling itself.
Because recursion was one of the many things that were prohibited.
Nov 26 '07 #66
RoS
In data Wed, 21 Nov 2007 09:27:16 +0100, RoS scrisse:
>In data Tue, 20 Nov 2007 06:55:08 -0800 (PST), prady scrisse:
>>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.
why not "add" ifs? :)
>this is not portable goes well here but for your all computer could
cause possibly the format of your hard disk
#include <stdio.h>

unsigned i=1;
char *w=0;
unsigned ww=0;

/*
0ra, 4j
label:
*/
void fun0(unsigned j)
{unsigned *k=&j;
k-=1;
ww=*(unsigned*) k;
}

int fun3(unsigned j)
{unsigned *a=&j;
printf("%u\n", i); ++i;
i!=j+1 && (a-=1)!=0 && (*(unsigned*)a= ww)!=0;
return 0;
}
int main(void)
{fun0(0);
fun3(45);
return 0;
}
Dec 3 '07 #67

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
8683
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
9170
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
9031
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
8904
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
5867
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();...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.