Q. what is calling by reference? How it is different from call by value? Write a C function to swap two given numbers using call by reference mechanism.
Ans.
When an argument is passed by reference, the caller actually allows the called function to modify the original variable's value.
- Sends the address of a variable to the called function.
- Use the address operator(&) in the parameter of the called function.
- Anytime we refer to the parameter, therefore we actually referring to the original variable.
- If the data is manipulated and changed in the called function, the original data in the function are changed.
/*swap function to interchange values by call by reference*/
#include<stdio.h>
#include<conio.h>
void swaping(int *x, int *y);
int main()
{
int n1,n2;
printf("Enter first number (n1) : ");
scanf("%d",&n1);
printf("Enter second number (n2) : ");
scanf("%d",&n2);
printf("\nBefore swapping values:");
printf("\n\tn1=%d \n\tn2=%d",n1,n2);
printf("\nBefore swapping values:");
printf("\n\tn1=%d \n\tn2=%d",n1,n2);
swaping(&n1,&n2);
printf("\nAfter swapping values:");
printf("\nAfter swapping values:");
printf("\n\tn1=%d \n\tn2=%d",n1,n2);
getch();
return 0;
}
getch();
return 0;
}
void swaping(int *x, int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
}
/***************** OUTPUT **************/
Swap using call by reference |
How do we draw a flowchart to the following code ?
ReplyDelete#include
void swap(int *, int *);
main()
{
int a,b;
printf("Enter two numbers : ");
scanf("%d%d",&a,&b);
printf("Values before swap are\n");
printf("a=%d\tb=%d\n",a,b);
swap(&a,&b);
printf("values before swap are\n");
printf("a=%d\tb=%d\n",a,b);
return 0;
}
void swap(int *x, int *y)
{
int tmp;
tmp=*x;
*x=*y;
*y=tmp;
}
Flowchart for your above program (call by reference) at:
Deletehttp://cprogrammingcodes.blogspot.in/2012/10/call-by-reference-swapping-flowchart.html
output should be values after swamp are
ReplyDelete#include
ReplyDelete#include
int main()
{
char str[30];
char *p,*t;
printf("Enter any string : ");
gets(str);
for(p=str ; *p!=NULL ; p++);
for(t=str, p-- ; p>=t; )
{
if(*p==*t)
{
p--;
t++;
}
else
break;
}
if(t>p)
printf("\nString is palindrome");
else
printf("\nString is Not palindrome");
getch();
return 0;
}
akash;
@Akash,
Delete(You can write your any program problem!)
All palindrome program at:
http://cprogrammingcodes.blogspot.in/2012/01/palindrome-using-pointer.html
thank u so much.
ReplyDelete#include
ReplyDelete#include
void swap (int*,int*);
void main ()
{
int a,b;
clrscr();
printf("enter the two numbers:");
scanf("%d %d"&a,&b);
printf("numbers before swapping are: %d %d",a,b);
swap(&a,&b);
getch();
}
void swap (int *x,int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
printf("\n numbers after swapping are: %d %d",*x,*y);
}
/* shubh jaiswal */
int p=10,q=20;
ReplyDeleteprintf("\nThe numbers before swapping");
printf("p:= %d q:= %d",p,q);
p ^=q ^=p ^=q;
printf("\nThe numbers after swapping");
printf("p:= %d q:= %d",p,q);
write a program to evaluate sum=((x-14)/(a*b)-(a+b)/(x-4))
ReplyDeleteplease help friends
Anyone know about brute force algorithm?
ReplyDeleteI need algorithm for above program
ReplyDelete