| Forums.Sureshkumar.net : A Perfect Place to Share Knowledge Blogs Games Magazines |
|
|
#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
|
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 |
|
|
|
|
|
#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
|
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 --- |
|
|
|
|
|
#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
|
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) |
|
|
|
|
|
#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
|
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 |
|
|
|
|
|
#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
|
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 |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
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 |