In the swap function how do i swap x and y value? Is there an easier way to write this program?
-
My values in the swap function did not return could someone help me out? #include<stdio.h> #include<math.h> double swap(double*); int main (void) { char character; int num1; double num2; double y; double x = swap(&y); printf("Enter a character...\n"); scanf("%c",&character); printf("Enter an integer...\n"); scanf("%d",&num1); printf("Enter a real number...\n"); scanf("%lf",&num2); printf("char = %c at memory location : Ox%p\n",character, &character); printf("char = %d at memory location : Ox%p\n",num1, &num1); printf("char = %lf at memory location : Ox%p\n",num2, &num2); printf("%lf %lf",x,y); return 0; } double swap(double*y) { double x = 5; double y = 10; double xx; double yy; xx = x; yy = y; x = yy; y = xx; return x; }
-
Answer:
Adding to the above comment, try this edited code, maybe it will point you in the right direction (sample run): http://codepad.org/rjwrpp9R __ #include<stdio.h> #include<math.h> double swap(double*); int main (void) { char character='p'; int num1=8; double num2=7.4; double y=9.9; double x = swap(&y); printf("Enter a character...\n"); scanf("%c",&character); printf("Enter an integer...\n"); scanf("%d",&num1); printf("Enter a real number...\n"); scanf("%lf",&num2); printf("char = %c at memory location : Ox%p\n",character, &character); printf("char = %d at memory location : Ox%p\n",num1, &num1); printf("char = %lf at memory location : Ox%p\n",num2, &num2); printf("%lf %lf",x,y); return 0; } double swap(double*y) { double x = 5; double *xx; double *yy; xx = &x; yy = y; x = *xx; y = xx; return x; }
Joe Han at Yahoo! Answers Visit the source
Other answers
Adding to the above comment, try this edited code, maybe it will point you in the right direction (sample run): http://codepad.org/rjwrpp9R __ #include<stdio.h> #include<math.h> double swap(double*); int main (void) { char character='p'; int num1=8; double num2=7.4; double y=9.9; double x = swap(&y); printf("Enter a character...\n"); scanf("%c",&character); printf("Enter an integer...\n"); scanf("%d",&num1); printf("Enter a real number...\n"); scanf("%lf",&num2); printf("char = %c at memory location : Ox%p\n",character, &character); printf("char = %d at memory location : Ox%p\n",num1, &num1); printf("char = %lf at memory location : Ox%p\n",num2, &num2); printf("%lf %lf",x,y); return 0; } double swap(double*y) { double x = 5; double *xx; double *yy; xx = &x; yy = y; x = *xx; y = xx; return x; }
.#include <keperkjr>
It doesn't work because you declare this in main() double y; double x = swap(&y); y is not assigned a value. in your function swap, you declare them again, so it will always return 10.
Rainmaker
It doesn't work because you declare this in main() double y; double x = swap(&y); y is not assigned a value. in your function swap, you declare them again, so it will always return 10.
Rainmaker
Related Q & A:
- How can I plot more than one value in the same date?Best solution by Mathematica
- How to write a program to monitor the temperature of CPU?Best solution by Stack Overflow
- How do I flip round video footage that's been videoed wrong way round?Best solution by Yahoo! Answers
- How do I get the red 'Y' for Yahoo back on the toolbar?Best solution by Yahoo! Answers
- How do I program my second viper 1-way remote starter #5601?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.