C question. |
Author: |
Message: |
.Roy
Veteran Member
Posts: 1526 Reputation: 11
20 / / –
Joined: Aug 2004
|
O.P. C question.
Well i have this:
#include <stdio.h>
#include <conio.h>
void main(void)
{
float a,b;
clrscr();
scanf("%f%f", &a,&b);
printf("%f \n %f", a,b);
}
anyway what i need to do, is make the a and b switch. What i mean is for the rest of the program A will be what i entered for B and B will be what i entered FOR A.
Now i dont want you to tell me how to do this, just tell me if its possible and a tip maybe o.O
|
|
09-21-2006 12:39 PM |
|
|
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
Joined: Mar 2003
|
RE: C question.
Set it in a globally defined variable?
|
|
09-21-2006 12:48 PM |
|
|
.Roy
Veteran Member
Posts: 1526 Reputation: 11
20 / / –
Joined: Aug 2004
|
O.P. RE: C question.
What?
explain im new to this language.
|
|
09-21-2006 12:58 PM |
|
|
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
Joined: Mar 2003
|
RE: C question.
I just removed float a and float b initialisation from the main function and put it above it, causing it to become a global variable.
code: #include <stdio.h>
#include <conio.h>
float a;
float b;
void main(void)
{
clrscr();
scanf("%f%f", &a,&b);
printf("%f \n %f", a,b);
}
|
|
09-21-2006 01:02 PM |
|
|
John Anderton
Elite Member
Posts: 3908 Reputation: 80
37 / /
Joined: Nov 2004
Status: Away
|
RE: C question.
quote: Originally posted by Ezra
Now i dont want you to tell me how to do this, just tell me if its possible and a tip maybe o.O
He wants the swapping function
The answer is as follows
code: #include <stdio.h>
#include <conio.h>
void main()
{
float a,b;
clrscr();
printf("Enter values of a and b: ");
scanf("%f %f",&a,&b);
printf("\n\nBefore swapping\n");
printf("a = %f \nb = %f",a,b);
swap(&a,&b);
printf("\n\nAfter swapping\n");
printf("a = %f \nb = %f",a,b);
getch();
}
void swap(float *x, float *y)
{
float temp = x;
x = y;
y = x;
}
[
KarunAB.com]
[img]http://gamercards.exophase.com/459422.png[
/img]
|
|
09-21-2006 01:11 PM |
|
|
.Roy
Veteran Member
Posts: 1526 Reputation: 11
20 / / –
Joined: Aug 2004
|
O.P. RE: C question.
yeah well my job is to figure it out without swap but nvm dont tell me.
|
|
09-21-2006 01:24 PM |
|
|
.Roy
Veteran Member
Posts: 1526 Reputation: 11
20 / / –
Joined: Aug 2004
|
O.P. RE: C question.
oh and i also cant do 3 letters
like float a,b,c
and then do
a=c
c=b
b=a
|
|
09-21-2006 01:25 PM |
|
|
Eljay
Elite Member
:O
Posts: 2949 Reputation: 77
– / / –
Joined: May 2004
|
RE: C question.
a = a + b;
b = a - b;
a = a - b;
google ftw.
|
|
09-21-2006 01:33 PM |
|
|
.Roy
Veteran Member
Posts: 1526 Reputation: 11
20 / / –
Joined: Aug 2004
|
O.P. RE: C question.
lol that works eljay thanks
But i dont understand how that makes sence o.O
and what did u google to find that.
This post was edited on 09-21-2006 at 01:49 PM by .Roy.
|
|
09-21-2006 01:46 PM |
|
|
RaceProUK
Elite Member
Posts: 6073 Reputation: 57
39 / /
Joined: Oct 2003
|
RE: C question.
quote: Originally posted by Eljay
a = a + b;
b = a - b;
a = a - b;
An example:
a = 4;
b = 6;
a = a + b; // 10
b = a - b; // 4
a = a - b; // 6
a == 6 && b == 4; // true
|
|
09-21-2006 02:23 PM |
|
|
Pages: (2):
« First
[ 1 ]
2
»
Last »
|
|