CE Home
Navigation
Go Back   CrazyEngineers Forum > CE : Technical Discussions > Computer Science & IT Engineering
Notices


Advertisements
Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)
Old 8th March 2007, 01:46 PM
CE - Apprentice
 
Join Date: 4th November 2006
I'm a Crazy Computer Science Engineer
Posts: 10
Default Find sum of two numbers

#include<stdio.h>
int main()
{
int i;
printf("Enter 2 numbers\n");

/*
Write here few lines of code so that user gives 2 numbers as
input(on console) and display the addition of those 2 numbers.. and
the condition is no more variable(s) except for 'i' declared in first line
should be used throughout the program.
*/

return 0;
}

Regards,
Uday
uday.bidkar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)
Old 11th March 2007, 12:34 PM
CE - Apprentice
 
Join Date: 25th September 2006
Location: chennai
I'm a Crazy computer science Engineer
Posts: 36
Default Re: Find sum of two numbers

hi,
i have used the consecutive memory location of i to store the 2 data.this works.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
scanf("%u",&i);
scanf("%u",&i+1);
printf("%d",(*(&(i)))+(*(&(i)+1)));
}
sahana is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)
Old 12th March 2007, 12:07 PM
CE - Apprentice
 
Join Date: 4th November 2006
I'm a Crazy Computer Science Engineer
Posts: 10
Default Re: Find sum of two numbers

Quote:
Originally Posted by sahana View Post
hi,
i have used the consecutive memory location of i to store the 2 data.this works.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
scanf("%u",&i);
scanf("%u",&i+1);
printf("%d",(*(&(i)))+(*(&(i)+1)));
}
The memory pointed to &i+1 is not valid memory that you can use for operations as you have not allocated the same neither asked compiler to allocate it for you on stack. Hence the program may crash because of invalid memory operations. So solution not acceptable!

-Uday
uday.bidkar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)
Old 12th March 2007, 09:41 PM
DEP
CE - Enthusiast
 
DEP's Avatar
 
Join Date: 16th February 2007
Location: EARTH
I'm a Crazy information technology Engineer
Posts: 102
Send a message via Yahoo to DEP
Cool Re: Find sum of two numbers

Quote:
Originally Posted by uday.bidkar View Post
The memory pointed to &i+1 is not valid memory that you can use for operations as you have not allocated the same neither asked compiler to allocate it for you on stack. Hence the program may crash because of invalid memory operations. So solution not acceptable!

-Uday
hi uday,
i have tried a lot to solve this problem and believe me whether you accept it or not pointers is the only way to solve it.

i guess i quite agree with the solution provided by sahana and am unable to figure out any other solution.

so please post any other solution if you have it !!!
__________________
To have a right to do a thing is not at all the same as to be right in doing it.
DEP is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)
Old 14th March 2007, 07:31 PM
CE - Apprentice
 
Join Date: 4th November 2006
I'm a Crazy Computer Science Engineer
Posts: 10
Default Re: Find sum of two numbers

Quote:
Originally Posted by DEP View Post
hi uday,
i have tried a lot to solve this problem and believe me whether you accept it or not pointers is the only way to solve it.

i guess i quite agree with the solution provided by sahana and am unable to figure out any other solution.

so please post any other solution if you have it !!!
There is a way to solve this. But before i post the answer for this, I would like to relax the condition and wait for few more days.
And the relaxed question is
#include<stdio.h>
int main()
{
/*
Write here few lines of code so that user gives 2 numbers as
input(on console) and display the addition of those 2 numbers.. and the
condition is only one integer variable should be declared and used
throughout the program. You are now free to declare the variable as you want.
*/

}
uday.bidkar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)
Old 14th March 2007, 08:14 PM
DEP
CE - Enthusiast
 
DEP's Avatar
 
Join Date: 16th February 2007
Location: EARTH
I'm a Crazy information technology Engineer
Posts: 102
Send a message via Yahoo to DEP
Question Re: Find sum of two numbers

Quote:
Originally Posted by uday.bidkar View Post
There is a way to solve this. But before i post the answer for this, I would like to relax the condition and wait for few more days.
And the relaxed question is
#include<stdio.h>
int main()
{
int *a;
cin>>*a;
cin>>*(a+1);
cout<<*a+*(a+1);
}
well uday i am sorry but this is the only solution that i have worked out.and i guess this is the c++ equivalent code of what sahanna had posted.
please please please tell the answer. am dying to know it.
__________________
To have a right to do a thing is not at all the same as to be right in doing it.
DEP is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)
Old 15th March 2007, 12:05 PM
CE - Apprentice
 
Join Date: 4th November 2006
I'm a Crazy Computer Science Engineer
Posts: 10
Default Re: Find sum of two numbers

Quote:
Originally Posted by DEP View Post
please please please tell the answer. am dying to know it.
Here is answer to the problem when variable is not
already declared.

int main()
{
static int i = 0;
if( !i )
{
i = -1 ;
printf( "The Sum is %d\n", main() + main() ) ;
return 0 ;
}
printf( "Enter a number\n" );
scanf( "%d",&i );
if( !i )
{
i = -1 ;
return 0 ;
}
else
{
return i ;
}
}

Regards,
Uday
uday.bidkar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)
Old 16th March 2007, 11:14 PM
CE - Apprentice
 
Join Date: 25th September 2006
Location: chennai
I'm a Crazy computer science Engineer
Posts: 36
Default Re: Find sum of two numbers

wow.that was really smart.
i accept my mistake.well actually i actually got the output ,as i was lucky that the unallocated memory was not a part of anyother program, and didnt overwrite any data of another program.
sahana is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)
Old 17th March 2007, 05:04 PM
CE - Apprentice
 
Satyajit's Avatar
 
Join Date: 17th February 2007
I'm a Crazy Computer Engineer
Posts: 10
Send a message via Yahoo to Satyajit
Smile Re: Find sum of two numbers

hi i have the programme of sum of 2 numbers
#include<stdio.h>
void main()
{
int a,b,c;
printf("enter any 2 numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("%d",&c);
}
Satyajit is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)
Old 17th March 2007, 05:19 PM
Good Administrator
 
The_Big_K's Avatar
 
Join Date: 26th November 2005
Location: Terra-Firma
I'm a Crazy Electrical Engineer
Posts: 5,673
Send a message via Yahoo to The_Big_K
Default Re: Find sum of two numbers

Quote:
Originally Posted by Satyajit View Post
hi i have the programme of sum of 2 numbers
#include<stdio.h>
void main()
{
int a,b,c;
printf("enter any 2 numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("%d",&c);
}
Satyajit, mind reading the first post in this thread? There is a special condition to be followed.

-The Big K-
The_Big_K is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +5.5. The time now is 12:15 PM.
Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Member comments are owned by the poster. Copyright © 2005-2008 CrazyEngineers.com. All rights reserved.

Advertisements