Jul 9, 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

iGuest
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. turbo c souce codes - 516.24 hr back.
  2. how to write for pallidrome in c - 548.57 hr back.
Similar Topics

Keywords : c, code, solve, interesting, problem

  1. Why Doesn't This Code Work On Computinghost?
    (1)
  2. Fantastico And Cpanel Skin Problem.
    (1)
    I changed my domain from tf2maps.trap17.com to fearclan.trap17.com recently, and when I did,
    everything was working fine, except cpanel became really ugly with this rvblue skin that I
    couldn't change, and fantastico and phpmyadmin weren't working. Can I get my skin changed
    from rvblue to somethiong better, say the one I had before? I think it was called cpanel mod x3 or
    something. The frontend for rvblue is my cpanel ip:2082/frontend/rvblue/index.html But the one I
    want is my cpanel ip:2082//frontend/x3/index.html which says 404 not found. I really want tha....
  3. Weird Write Problem
    (0)
    Any ideas whats up with this error? It just started appearing now. ....
  4. Phpmyadmin Problem
    (1)
    PHPMyAdmin Problem CODE Warning: session_write_close()
    [function.session-write-close]: SQLite: session write query failed: database is
    full in /usr/local/cpanel/base/3rdparty/phpMyAdmin/index.php on line 42 Warning:
    session_write_close() [function.session-write-close]: Failed to write session
    data (sqlite). Please verify that the current setting of session.save_path is correct
    (/var/cpanel/userhomes/cpanelphpmyadmin/sessions/phpsess.sdb) in
    /usr/local/cpanel/base/3rdparty/phpMyAdmin/index.php ....
  5. Enzymes And Health
    Some interesting info on enzymes (0)
    Enzymes act as catalysts in living organisms which allow important chemical reactions to occur at
    lower temperatures. They are not altered in the process and can be used in many reactions. Most of
    the chemical reactions that occur in living organisms are regulated by enzymes and would happen much
    slower without them. For example, without the digestive enzyme carboxypepdidase, it would take
    seven years to digest a hamburger. Ethyl carbamate is a compound found in fermented beverages that
    can cause cancer in various organisms. Its precursor is urea, an intermediate du....
  6. Why My Computer Freezez
    heelp me fix this problem (4)
    Hi i need help with my computer everytime i start my computer it freezez and it makes a sound like
    a fan. I told my uncle but he said he dint found anything that it could be the hardware but he dint
    told me how to fix it (he works with computer) I think that the problem is that the computer needs
    a clean up from inside. ....
  7. Sql Problem
    (2)
    Hello everybody, I've recently got my phpBB3.0.1 on my website and everything was going fine.
    But a few minutes ago, I told my friend to sign up and then she did. And when she tried to post she
    got a SQL error, and then I wanted to see what's wrong and I got the same error as well. Also,
    it didn't show up that my friend's post was the latest post, and instead it showed that it
    was the post just before it. So I don't know what's going on and I hope that someone could
    help me out solving this one cuz I know nothing about databases to be honest with ....
  8. Create Table - Mysql Code - Help
    (1)
    I need your feedback about setting the database issues. Please, review them and correct some entries
    in the code if they got some mistakes. This is the code itself: SQL CREATE TABLE
    `news` ( `id` int(250) NOT NULL auto_increment, `title` varchar(255)
    NOT NULL default '', `text` text NOT NULL, `author` varchar(255) NOT
    NULL default '', `valid` varchar(255) NOT NULL default '',
    `date` varchar(255) NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE = ....
  9. Credits Problem
    (14)
    Well thanks to BuffaloHELP now my problem is solved and I can login to my cPanel anytime I want to
    and I already started building my website and everything is going smoothly so far /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> but there's still one
    simple problem that needs to be solved and it has something to do with my credits because when
    BuffaloHELP re-activated my account and changed my status from HOSTED back to MEMBERS and then I
    created my hosting account all over again with the same login information I requested when I f....
  10. I'm Having A Strange Problem With My Ping In Cs:s
    (27)
    Hey guys. I bought Counter Strike Source, and I was playing it for a while but it was very slow
    because of my bad graphics card. I ended up moving and getting a new graphics card at about the same
    time, so graphically the game runs great now. However, I can't play online now because, for some
    reason, every server thnks my ping is too high and kicks me off. It doesn't matter what location
    the server is in, it always thinks my ping is too high. The starnge part is, my highest ping is
    around maybe 150, and I have cable internet, I believe it is a 1M connection, mayb....
  11. Mozilla: Firefox Plugin Shipped With Malicious Code
    (3)
    This piece of news only affect Vietnamese users as the Vietnam language package was infected with
    malware trojan called e Xorer, and so if you downloaded this language pack in the last few weeks run
    a scan and the trojan should be picked up. Although this trojan is only a couple of months old and
    so I don't think everyone has something for it, but check at your vendors website and see if
    they have a solution for it. As for the cause of this infected plugin, they assume the authors
    computer was infected at the time when they upload this plugin to the mozilla website....
  12. Flash Problem
    sometimes the buttons will load the wrong link. (9)
    Its weird but sometimes when you hit the forums or roster button on my flash banner it will load:
    www.childrenofconan.trap17.com/index.html instead of what its suppost to. It works fine, but if you
    mess around and click the buttons for a little bit you will notice this happening. This is a problem
    because people visiting the site will sometimes click the button once and be like oh I guess the
    link is broken and never come back, if anyone has a solution to what could be the problem it would
    be awesome. This is one of my first custom flash banners and im just confused what....
  13. Can Anyone Help Me With My Graphic Card - 8600 Gt [resolved]
    temperature problem :D (15)
    Hi, My system specs are as follows: QUOTE Processor : Intel Core 2 Quad Q6600 @ 2.4 GHz
    Motherboard: nVidia 650i Ultra RAM : 2 GB GfxCard : XFX 8600 GT (512 MB) Problem is all of my
    nVidia component are now managed by a single Control panel - nVidia Control panel and it doesn't
    show any tab for graphic's card temperature. I want to know the temperature of my graphics
    card. does any body know any software for this ?? or at least a work around ? thanks /biggrin.gif"
    style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" />....
  14. Mysql-essential-5.0.51 Installion Problem
    please help me.(urgent) (2)
    Dear friend, please help me. Iam working on php project (matchmaking site). I have downloaded mysql
    essensial 5.0.51. When i was installing mysql 5.0.51, i got following error - Error 1901: Error
    attempting to read from the source installation database:
    c:\windows\installer\527ec6.msi What is solution of this problem ? Iam using
    windows xp professional version 2002 (service pack 2). Please post your reply. Iam waiting for your
    answer. ....
  15. Php Code?
    Mathematical Applications (12)
    Hello, everyone. The help is needed again. How can I make calculator in PHP language? That will act
    like that a user just type in the fields known values, then click the button, and it's going to
    be solved automatically. In other words, have can I write a formula in PHP, how to plug it inside
    that language. For example, the formula to find a peremeter of square is: P=4a. So, a user
    just can write the known value which is peremeter itself and it will find the side of a square; and
    vice versa. If you can write many things how to do such formulas, such as comp....
  16. Ashes
    interesting ideas for when I die (5)
    When i die I don't like bugs and being eaten by my own enzymes so I want to be "turned to ash"
    or cremated. so I was wondering what to do with the ashes.. I want something cool and interesting.
    My idea is that they get turned into a diamond and then that diamond is flown into space and left
    there! To float about. I don't personally believe in a God/Higher being so don't worry
    about what it is Anyone got any good/better ideas?....
  17. How Do I Code A Design?
    (6)
    Okey, so I saw another topic here regarding how to make a design. I know how to do that, so that is
    no problem. But I have always had some problems on how to code a design. I am tired of that I always
    need to use some sort of program, drag&drop webhost (like piczo) or use a free design I found
    online. I have noticed that people need to "cut" the design in different parts, but I have never
    understood what way the do it. So I do not understand anything about this, and really need some
    help. I will be happy for all help I get. /smile.gif" style="vertical-align:middle"....
  18. Html Code Tester. Online Script
    (15)
    Yes, yes. I have another script that I have written and I am distributing. I am not entirely sure if
    this works. I have not tested it yet, but I will later and post back with a demo and fix it up.
    Current script: CODE <?php //Save this as something like htmltest.php function
    CheckForm() { $html_unsafe=$_POST['code']; //Gives us our user
    input $html_safe=str_replace("<?php"," ",$html_unsafe);
    //Starts security measures $html_safe=str_replace("?>","
    ",$html_sa....
  19. How To Reformat Your Harddrive...
    Is your computer slow, reformatting will fix the problem! (10)
    To use this tutorial you first must have prior knowledge of computing and basic experience within
    them. *****WARNING!***** This can damage your PC, if you perform the actions incorrectly.
    ****************** HERE WE GO! 1. Backup all important information on your computer, as
    everything will be deleted. 2. Make Sure you have your Windows XP CD at hand (We need this to
    re-install Windows!) 3. Restart your PC and press 'DEL' (or the key that boots into your
    bios) when prompted. 4. Change the boot sequence to boot from your CD drive first. (This v....
  20. Each And Every Nokia Code For Your Mobile
    (6)
    Nokia 3210/5110/6110 And All Most Works In Most Of The Nokia Phones. QUOTE To view IMEI number
    *#06# To view Software Version enter *#0000# To view Status of Sim Clock Stop. Enter *#746025625#
    Latest Version is under Phone Info Type is NSE-3NX *#92702689# offers you Serial Number and also
    IMEI number. There are various options to scroll here. The code is easier to remember as *#war0anty#
    (warranty) The next screen is the date of manufacture in the format Made: 1197 The next screen is
    the purchase date in the format Purchasing Date: 1197 (this can be edited) The nex....
  21. Remote Assistance Problem
    Won't work (7)
    I have tried tons of times and i can not get it too work. I did't even know about this until
    Paul from Trap 17 used it to help me with something on my site. Whenever i try it, it never
    connects. I have tried all three ways. Email, MSN, and download the file and send it. Both computers
    are connected to the internet. On microsoft's site they have a lot of people with the same
    problem, but no one ever helps with it. They are all unanswered. Paul: If you read this i know you
    know how to do it.....
  22. Wap Source Code Viewer
    Mobile/wap source code viewer page (4)
    This is a source code viewer that will workl on wap/mobile sites but you can easily convert it to
    work on web im sure ;-) CODE <? header("Content-Type:
    text/vnd.wap.wml"); echo '<?xml version="1.0"
    encoding="utf-8"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
    "http://www.wapforum.org/DTD/wml_1.1.xml"> <wml> <head><meta
    http-equiv="Cache-Control" content="no-cache"
    forua="true"/></head> <card title="s60.nerds.....
  23. My Dog Pukes Regularly
    what would be the cause of this problem? (16)
    AHHH!!!! my dog puked everywhere and my mom is making me clean up ALL of it!
    Ugh. Do any of you have mutts that puke on a regular basis? Is this a common problem? If so please
    reply, I'm trying to figure out if there's a common reason/cause for the problem, hopefully
    we can find a solution or make it better.....
  24. Kitten Born With Only One Eye And No Nose
    basically the title says it all. problem (27)
    http://www.msnbc.msn.com/id/10792937/ Basically this kitten is named Cy, short for cyclops was
    born with this facial deformity. I think its just sad. The kitty dies in one day. All the mothers
    litter were normal. What do you think about this??? Like I just think life is so unfair sometimes.
    Everybody is not born the same.....
  25. Myspace Music Player
    Anyone have the code? (9)
    Hey, I was looking around on MySpace to see if they had a particular song with a player that you see
    on many different MySpaces, but they did not have one for the song I wanted. I was wondering if
    anyone had the proper code for a music player. I do have the song I want and have the capability to
    upload it, but I just need the code for it. If you can help at all I'd really appreciate it.
    Example on the top right of this MySpace (but I just want a one song player):
    http://www.myspace.com/ofarevolution ....
  26. Auto Webpage Resolution Format
    Viewing Problem Help Needed Please (7)
    Hi, my website was created with Frontpage 2003. At screen resolution 1024 * 768. Now the problem
    that I am finding is people with a screen resol. 800 * 600 see the table and everything in the wrong
    place and its all messed up. Is there a way to make it so the site is compatiable with all screen
    resolutions. Thanx in advance for your help. Edited topic title. ....
  27. What Is A Chupacabra?
    Very Interesting. (96)
    Has anyone heard of el chupacabra? A chupacabra is a creature that is allegedly responsible for
    draining blood or removing the organs of livestock. It's name is Spanish for "goat sucker" due
    to the earliest report of this creature in Puerto Rico where goats were completely drained of all
    blood. People still claim to see these creatures. Some other people believe that it is just like
    big foot. As people hear about it, they believe it more, which makes some think its real when in
    fact its not. Heres a picture. http://www.uncoveror.com/chupa.htm http://www....
  28. Crazy Looking C Program
    Interesting (19)
    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 == ....
  29. Redirect Code Help
    (7)
    Can someone give me the code which redirects you automatically in the whole page instead of only in
    the frame? I'm using this code, but it just redirects inside a frame... CODE <meta
    HTTP-EQUIV="REFRESH" content="0; url=http://www.something.com"> But now
    .tk has added a ad to the pages so I don't want to use it anymore, so I want that people
    entering the site is being redirected to the "real" domain name now.......
  30. How do you test your php code
    (75)
    We know that php is a server side scripting language. So we will need a server with the php parser
    to parse/test our code. How are you doing that. Do you upload it to a server for testing or did you
    instal php and the server (apache) on your computer (localhost)....

    1. 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