473,549 Members | 2,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Have any solution

#include<stdio. h>
void change()
{
/* Write something in this function so that the output of printf in
main
function should give 5 . Do not change the main function */
}

void main()
{
int i=5;
change();
i=10;
printf("%d",i);
}

Aug 9 '05 #1
9 1269
sojin wrote:

#include<stdio. h>
void change()
{
/* Write something in this function so that the output of printf in
main
function should give 5 . Do not change the main function */
}

void main()
{
int i=5;
change();
i=10;
printf("%d",i);
}


IMHO: Impossible.
There is no way to get your hands at variable i in main().

--
Karl Heinz Buchegger
kb******@gascad .at
Aug 9 '05 #2
sojin wrote:
#include<stdio. h>
void change()
{
/* Write something in this function so that the output of printf in
main
function should give 5 . Do not change the main function */
}

void main()
main() must return int.
{
int i=5;
change();
i=10;
printf("%d",i);
}


That's impossible in standard C++. I also fail to see what that would be
good for.

Aug 9 '05 #3
sojin wrote:
#include<stdio. h>
void change()
{
/* Write something in this function so that the output of printf in
main
function should give 5 . Do not change the main function */
}

void main()
int main() // C++ does NOT have 'void main'
{
int i=5;
change();
i=10;
printf("%d",i);
}


There is no solution to your homework problem in the Standard C++, AFAICS.

Since the value that printf outputs is a local [to 'main'] variable,
nothing that another function can do would access that variable if you
don't pass its address or a reference to it into that function. Passing
that reference (or the address) would require changing the 'main'
function, which would mean not satisfying the requirement not to change
the 'main' function.

There is one way to make the _program_ to output 5 (instead of 10) but
the output will not be generated by the call to 'printf' in 'main'. You
could write

void change()
{
printf("5");
exit(0);
}

which basically causes the program to quit before even getting of the
'change' function. However, that goes against the "output of printf
in main function should give 5" because the output is from a different
call to printf.

Using non-standard means you can tweak the stack to make the 'change'
function return to 'main' in the place _after_ the "i=10" statement,
but that wouldn't be done in C++.

V
Aug 9 '05 #4
"sojin" <ju*****@gmail. com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com
#include<stdio. h>
void change()
{
/* Write something in this function so that the output of printf in
main
function should give 5 . Do not change the main function */
}

void main()
{
int i=5;
change();
i=10;
printf("%d",i);
}


Not sure if it answers the question, but:

void change()
{
printf("5");
exit(0);
}

--
John Carson

Aug 9 '05 #5
* sojin:
#include<stdio. h>
void change()
{
/* Write something in this function so that the output of printf in
main
function should give 5 . Do not change the main function */
Here you don't have access to the values in the 'main' function, so the only
thing you can do, in a semi-portable way, is to redefine 'printf'.

And that's trivial, so I won't spoil it for you, but in your answer,
remember to include a comment in LARGE LETTERS to your instructor stating
that this technique is prohibited by the standard (i.e. formally invalid);
for full marks point out the relevant paragraph(s) in the standard.

Hopefully the children will not spoil it for you, but they always do...

}

void main()
{
As already remarked on, 'void main' is non-standard. Use 'int main'.
Include also this information in a LARGE LETTERS comment to your instructor.
int i=5;
change();
i=10;
printf("%d",i);
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 9 '05 #6
Hi experts,

I have been asked this question by one of my friend... nd claiming that
he's having the answer (not funny one..)
i dont think so..any way i just want u good peoples suggetion also...

I'm sorry to post this 'c' stuff is this highly acclaimed standard c++
group...
Thnx,
soj

Aug 9 '05 #7
sojin wrote:
Please quote. From Google, click "show options" and use the Reply in
the expanded header.
I have been asked this question by one of my friend... nd claiming
that he's having the answer (not funny one..)
i dont think so..any way i just want u good peoples suggetion also...
As others of said, there's no standard way.
I'm sorry to post this 'c' stuff is this highly acclaimed standard c++
group...

printf() is C++ as well. The answer is the same in either language, you
can't do what you want in any sort of portable or standard manner.
PS, if this was a homework assignment, see if you can drop the class
and get into a real one some other time.


Brian
Aug 9 '05 #8
sojin wrote:
Hi experts,

I have been asked this question by one of my friend... nd claiming that
he's having the answer (not funny one..) i dont think so..any way i just
want u good peoples suggetion also...
That's what you got. We all agree with you.
I'm sorry to post this 'c' stuff is this highly acclaimed standard c++
group...


In C, the answer would be the same.

Aug 9 '05 #9
It's probably not really useful to even try to resolve such puzzles but
anyway here's one "standard" solution:
#include<stdio. h>
void change()
{
/* Write something in this function so that the output of printf in
main
function should give 5 . Do not change the main function */ #define int int dummy;
#define i 5, dummy }

void main()
{
int i=5;
change();
i=10;
printf("%d",i);
}


The printf *does* print "5" doesn't it ;)

SerGioGioGio
Aug 9 '05 #10

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

Similar topics

47
18851
by: Matt Kruse | last post by:
http://www.mattkruse.com/temp/css_expressions.html One of the standard CSS questions is "how can I shade every other table row a different color with CSS?" The answers are usually 1) you can't (yet), 2) put a class on every other tr, or 3) use javascript to do the styling for you. Well, I've been playing the CSS expressions quite a bit...
8
1556
by: M O J O | last post by:
Hi, I'm creating an CRM solution for my company. I want to split up the solution into several classlibraries, so I dont need to build the entire solution every time I run my project. First I thought about splitting my application up into these libraries: Solution (solution)
1
4461
by: hzgt9b | last post by:
When adding my VB .NET solution (with two sub-projects) the folder structure in VSS gets an extra level of folders... For example, here's the structure of the solution on my C:\ ....\mySolution\mySolution.sln ....\mySolution\myProject1\myProject1.vbproj -- and -- ....\mySolution\myProject2\myProject2.vbproj But after adding the solution...
1
2030
by: Lulu | last post by:
What is the easiest way to 1) delete a File System Website Solution and 2) rename a File System Website solution? I noticed when I create the ASP.NET Web Site (via File System location option) that I get one directly with my code (.aspx etc files). but the solution is under the default Visual Studio directory. So I assume if I delete these...
2
1762
by: mkd1919 | last post by:
I'm developing a web service that will eventually be sent to other like organizations to communicate for a particular application. After a few weeks, I decided it would be better to split of the web service and custome code from the required code. In doing so, I created a class project. This left me with a new solution of a web site and a...
19
2386
by: Mountain | last post by:
I would like opinions on whether this solution could be important and whether anyone knows if it is already solved. (I would also like to know if anyone thinks it cannot be solved.) First, we start with a general tree (any of the trees that my data structures book calls rooted trees or unordered trees). Then we are given two specific nodes...
1
1389
by: Tom C | last post by:
In the visual studio IDE, how do I delete a whole solution using Solution Explorer? I can delete the projects in the solution, but not the solution itself.
2
2257
by: =?Utf-8?B?SmVmZnJleQ==?= | last post by:
I made a typo on a Project name (e.g. Wong, instead of Wang). Later on I renamed the Soution, Project, WebForm., etc, except the file folder name, back to Wang. Then after I closed the VS.net and went back to the Windows Explorer, I found out the file folder name was Wong, so I changed it to Wang. Next time, when I tried to open the Wang...
0
2332
by: techie | last post by:
List of Solution manuals: Engineering Circuit Analysis 6Ed - Hayt Solutions Manual Norbury - Solutions manual for mechanics and thermodynamics Physics For Scientists And Engineers - Solution Manual RC Hibbeler statistics 11th edition
8
1319
by: =?Utf-8?B?bXVzb3NkZXY=?= | last post by:
Hi I've just come back to a project I've had a week or so off from, haven't touched it, and it seemed to be working fine when I left it. However, it's not compiling!! Here's the warnings/errors I get... Warning 1 The member 'admind_cafe_Default.Master' does not hide an inherited member. The new keyword is not...
0
7450
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...
0
7957
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...
1
7470
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...
0
7809
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...
0
6043
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...
1
5368
isladogs
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...
1
1941
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
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
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...

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.