473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1264
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.googlegr oups.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
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...
8
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...
1
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:\...
1
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)...
2
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...
19
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...
1
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
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...
0
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...
8
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...

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.