Forums.Sureshkumar.net : A Perfect Place to Share Knowledge         Blogs     Games    Magazines    

"Sharing knowledge does not lessen your store, often it gets you more. Sharing plays a key role in relationships and bonding, happens in small steps and is assisted through community membership."

Go Back   SURESHKUMAR.NET FORUMS > TECHNICAL DISCUSSIONS > DATA STRUCTURES, C, C++, VC ++
Register FAQ Members List Calendar Games Blogs Search Today's Posts Mark Forums Read

   

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
Old 27-03-06, 01:31 PM   #1 (permalink)
Senior Member
 
Join Date: Jan 2006
Posts: 976
Thanks: 0
Thanked 27 Times in 20 Posts
Thanks: 0
Thanked 27 Times in 20 Posts
Rep Power: 14 HELP has disabled reputation
Write program to swap 2 numbers



Write a C program to read two integers M and N and to swap
their values. Use a user defined functions for swapping. Output the values of M
and N before and after swapping with suitable message.




__________________
WE WISH YOU ALL THE BEST
SURESHKUMAR.NET TEAM
HELP is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 27-03-06, 02:20 PM   #2 (permalink)
Member
 
Join Date: Mar 2006
Location: India
Age: 23
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 3 harishmitty has disabled reputation


Iamgiving 2 versions of the same program ..


// Version-1: In this the swapped value(modified value)can be seen in the main method


#include<stdio.h>
#include<conio.h>



void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}



void main()
{
int *p = 10;
int *q = 20;
clrscr();
printf("\n BEFORE SWAPPING :");
printf(" P = %d \t Q = %d",p,q);
swap(&p,&q);
printf("\n AFTER SWAPPING :");
printf(" P = %d \t Q = %d",p,q);
getch();
}



//Version 2: In this swapped value is not reflected in main function


#include<stdio.h>
#include<conio.h>



void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}



void main()
{
int *p = 10;
int *q = 20;
clrscr();
printf("\n BEFORE SWAPPING :");
printf(" P = %d \t Q = %d",p,q);
swap(p,q);
printf("\n AFTER SWAPPING :");
printf(" P = %d \t Q = %d",p,q);
getch();
}



Watsthe diff b/w the two versions is ....


In version 1 , Iam sending address of the pointer to the function swap() as arguments .. Hence the pointers p & q are modified directly reflecting changed values when i print their values in main function..


In version 2,Iam just sending the pointers reference .. Hence the modified values aren't reflected in main function ..


Regards,


HARISH MITTY


__________________
HARISH MITTY

--- Life is a PUZZLE ---
harishmitty is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 29-03-06, 12:20 AM   #3 (permalink)
Junior Member
 
Join Date: Mar 2006
Location: India
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 3 freshersen has disabled reputation


hi , am trying to swap(M,N) without using temp variable.


this is the question , i could attened the debuging context


#include<stdio.h>


void main()
{
int M = 5, N = 3;
printf("\nBefore Swap ");
printf("\n\tM=%d\tN=%d",M,N);

M = M + N;
N = M - N;
M = M - N;


printf("\nAfter Swap ");
printf("\n\tM=%d\tN=%d",M,N);
}


__________________
Regard\'s
Senthil Kumar M
(9841892000)
freshersen is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 31-03-06, 04:23 PM   #4 (permalink)
Member
 
Join Date: Feb 2006
Location: India
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 3 madhab has disabled reputation


Hi I m trying to do it in a single line logic.


#include<stdio.h>


void main()
{
int M = 5, N = 3;
printf("\nBefore Swap ");
printf("\n\tM=%d\tN=%d",M,N);



M=N+(N=M)-M;



printf("\nAfter Swap ");
printf("\n\tM=%d\tN=%d",M,N);
}


Please verify.
__________________
I am
Mady
madhab is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-04-06, 02:40 AM   #5 (permalink)
Member
 
Join Date: Mar 2006
Location: India
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 3 Jyoti Bhatnagar has disabled reputation


Hi,


As per question provided we have to use user defined function. So correct way could be as given by harishmitty using 3rd variable or as following


#include<stdio.h>


void main()
{
int M = 5, N = 3;


void swap (int m,int n);
printf("\nBefore Swap ");
printf("\n\tM=%d\tN=%d",M,N);
swap (M,N);


getch();


}
void swap(int m, int n)


{


m=m + n;
n = m - n;
m =m - n;


printf("\nAfter Swap ");
printf("\n\tm=%d\tn=%d",m,n);
}
__________________
Jyoti Bhatnagar
Jyoti Bhatnagar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-05-06, 10:27 AM   #6 (permalink)
Senior Member
 
Join Date: Mar 2006
Location: India
Posts: 312
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 6 murari has disabled reputation
nice answers
__________________
Nachithe entha riskaina chesta
okkasari fix ayithe naa maata nene vinanu
murari is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 On
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
C language - How to write a C program to find the power of 2 in a normal way and in single step? GEEK ds. c , c++ Interview / Technical Questions 3 27-03-08 01:30 PM
J2EE - Write a program that singleton objects returns two instances? preethisingh Java Interview / Technical Questions 1 23-01-07 04:11 PM
C language - How to write a program such that it will delete itself after exectution? GEEK ds. c , c++ Interview / Technical Questions 0 23-01-07 01:36 PM
C language - How do you write a program which produces its own source code as its output? GEEK ds. c , c++ Interview / Technical Questions 0 23-01-07 01:32 PM


All times are GMT +6.5. The time now is 01:05 PM.





Search Engine Optimization by vBSEO 3.1.0