May 16, 2008

C Code, Can U Solve This? - Another interesting problem

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming
Pages: 1, 2, 3

free web hosting

C Code, Can U Solve This? - Another interesting problem

cse-icons
Hello,

Look at the code given below


CODE
void fun(void)
{
/* Put your code here so that main does not print 20 */
}

int main()
{
int i = 20;
fun();
printf("i = %d\n", i);
return 0;
}




1. You are allowed to put your code ONLY in fun.
2. You are not allowed to change even one character in main.
3. You should not use functions like exit(),abort() in the function fun.

How do I manipulate and change the value of i?
Have fun...

Reply

no9t9
This smells like a homework assignment... So.. I won't tell you how to do it smile.gif

Here's a POINTER though... variables can be manipulated using their address in memory...

Reply

cse-icons
actually, if u use pointers, u cannot be sure that it will work on all systems.
coz memory allocation is compiler and architecture specific....

btw, this is not an assignment and also I got the answer without using pointers....
just give it a shot....

Reply

tmonty
I haven't tested this code:

CODE

void fun() {

int aaaaaa;
int *q= &aaaaaa;

while ( *(q--)!=20 );   // we look for variable which value is 20 (it couldn't be 'i'!)

*q = 10; // we change it


}

main() { ... }


Hm.... It is only suggestion.

Reply

cse-icons
good try tmonty...
the solution wud work in a few cases, but just as i told earlier.. it cud fail
reason: some other memory location might contain "20"..

chek out my next reply for a hint....

Reply

nooc
CODE
#include <stdio.h>

void fun(void)
{
#define fun() i=10
}

int main()
{
int i = 20;
fun();
printf("i = %d\n", i);
return 0;
}

Reply

cse-icons
yeah, that cud be a solution.... i have not tried out...
My solution is also on similar lines..... here it is:



CODE
fun()
{
#define printf( i , j ) printf( i , j+1 )
}

main ()
{
int i =10;
fun();
printf("i = %d",i);
}

Reply

switch
very smooth. laugh.gif

Reply

tmonty
Yeah. ;p
With #define U can do everything. ;p

Reply

jordanliuhao
How about

CODE
void fun(void)
{
/* Put your code here so that main does not print 20 */
}

void printf(....)
{
/* Above is my code, Just for fun  :D */

}

int main()
{
int i = 20;
fun();
printf("i = %d\n", i);
return 0;
}

Reply

Latest Entries

Trap FeedBacker
code for this program
C Code, Can U Solve This?

Easy code in 'C' program to prove pallidrome rule

Thanking you,
Vikram









-question by vikram

Reply

tricky77puzzle
printf is a standard function in C. You can't modify it.

Try doing the same thing in c++.

Reply

Dark_Prisoner
QUOTE(nooc @ Nov 19 2004, 11:02 PM) *
CODE
#include <stdio.h>

void fun(void)
{
#define fun() i=10
}

int main()
{
int i = 20;
fun();
printf("i = %d\n", i);
return 0;
}


I think that this won't work :
(i guess) we can't put preprocessor directives on a function
If we can , it won't work also , because the compiler or the processor had completed the line :
CODE
fun();

so he won't replace it .

if the preprocessor works in functions i guess this will be the answer
CODE
void fun(void)
{
#define printf("i = %d\n", i) i=0
}

int main()
{
int i = 20;
fun();
printf("i = %d\n", i);
return 0;
}


What do you think ???


Reply

klusk
QUOTE(8bit @ Feb 4 2005, 07:07 AM) *
CODE
void fun(void)
{
  int * ptr =&i;
  *ptr=50;
}


Beat that.

This code wont work as the variable i is undeclared for fun()....it would have worked if "i" had been a global variable

There is another way if you wish to use pointers...


CODE
void fun(int *ptr)
{
  *ptr=50;
}


int main()
{
int i = 20;
fun(&i);
printf("i = %d\n", i);
return 0;
}



the output will be 50

Reply

Csshih
Oh yea?
void fun(void)
{
randomlineofetext(3249830325)
}
the program won't print anything at all xD

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Pages: 1, 2, 3
Recent Queries:-
  1. reply - 1384.63 hr back.
  2. strict xhtml replacement for frames - 1408.58 hr back.
  3. puzzle game program code only - 1422.85 hr back.
  4. puzzle game code only - 1422.93 hr back.
Similar Topics

Keywords : code interesting

  1. Most Efficient Code To Get Prime Numbers - (7)
  2. A Telephone Directory Source Code In C - (2)
    Well i had made this project some years before. It is a telephone directory which has a very good
    User Interface operatable with scroll key. This code can be usefull in learning about file handling
    and User Interface. CODE #include <stdio.h> #include <box.h> #include
    <conio.h> #include <dos.h> #include <stdlib.h> #include <bios.h>
    #include <graphics.h> FILE *fp; addnew2(char [],char []);
    prnd(); deleteit(); info(); pl(); checkcmd();
    showall(...
  3. Simple C File Handling In Action - Small code snipet which covers most of basic file handling and navigat (2)
    Yesterday I suddenly got a lot of work. The same work we try to push off, yes you are right all
    formalities to get the code review incorporated and update all source code files with code review
    headers. Imagine if you need to open 300 files one by one and append code review headers at the
    end. Since most files are reviewed in groups of 20 to 30 files. We require one header to be placed
    in say 20 to 30 files. To simplify I went back to my class assignment days and wrote this small c
    utility to open all files passed on command line and open attach code review headers an...
  4. Source Code For Paint Like Program Under Dos In C - (2)
    this is a dos program that you have been waiting for you learn from it you how to do graphic
    programing under dos if you understand it your then will be able to do any kind of windows GUI in
    dos it make icons and use them it deal with graphic file youwill also learn how to use mouse have
    nice time ...
  5. Crazy Looking C Program - Interesting (18)
    hi friends, Have a look at the following code... Can u guess what this wud do... nope... no virus
    nor executable code... It is a completely valid and compilable C program... dont worry.. I have
    executed it on my system and it perfectly nice program /smile.gif' border='0'
    style='vertical-align:middle' alt='smile.gif' /> CODE #include <stdio.h>
    main(t,_,a) char *a; {return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
    main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94,
    -27+t, a )&&t == ...
  6. C++ Roman Numeral Conversion Code Help - If-else Problem (5)
    thanks...
  7. Wolfenstein Source Code Now In Public Domain - (3)
    the Wolfenstein game which we enjoy it alot becouse of its highly graphic technique now u can
    learn alot from it and become profissional programer becouse now its source code in Public Domain
    get the source code now and make your way to become profissional programer get it at
    Wolfenstein source code ...
  8. Prob With My C Code - (4)
    Hi Guys! this is a wonderful place.. although i wasnt a member till now i have referred to
    these forms quite a few times for my programming solns /biggrin.gif" style="vertical-align:middle"
    emoid=":D" border="0" alt="biggrin.gif" /> My problem is ::: I am writing a Code in C using
    Bloodshed Dev C++ 4.9.9.2 on Windows XP. I wanted to open an executable file from my code. After
    using the solution mentioned on these forums, I used QUOTE system("myprg"); It works
    fine.. But the prob is that i want my C program to execute this command and then return to t...
  9. Code Documentation - A better way? (1)
    This is not so much a question, actually, but a bit of a plug for an interesting tool I've
    discovered recently. Anyway. How does everyone else document their code? Are in-source comments
    sufficient? Do you write extra detailed documentation outside of the code? Or do you generate
    external documentation from the code? Since I've been doing Java recently, I was in awe when I
    discovered javadoc. An incredible tool that allows you to generate external documentation for your
    code from your comments in-source. The syntax is reasonably simple, allowing the comme...
  10. Adding Myfileexists Function To My Basic Code? - (2)
    Here is a short and basic piece of my first ever application written in Visual C++ 6.0 . I have also
    commented the code well to show you EXACTLY what my code is meant to do with each function. What
    exactly I am asking for, is for someone to please replace anywhere that you see "EXISTS" with the
    proper function "myFileExists" to actually check to see if the file exists. Also I am asking for
    anyone that may see an error in this code, to please point it out to me and how I can fix it. If
    anyone can help me with getting this very basic code to work, it would be GREATLY appr...
  11. Where Can I Get Full Souce Code Of Turbo C++ ? - where can i get full souce code of Turbo (2)
    where can i get full souce code of Turbo C++ ? Al... it must be complete... complete... includes and
    libraries... all.. pls.. help me.. coz i cant compile my programs.......



Looking for c, code, solve, interesting, problem

Searching Video's for c, code, solve, interesting, problem
advertisement



C Code, Can U Solve This? - Another interesting problem



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE