Shoutbox

C question. - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: C question. (/showthread.php?tid=66499)

C question. by .Roy on 09-21-2006 at 12:39 PM

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


RE: C question. by Ezra on 09-21-2006 at 12:48 PM

Set it in a globally defined variable?


RE: C question. by .Roy on 09-21-2006 at 12:58 PM

What?

explain im new to this language.


RE: C question. by Ezra on 09-21-2006 at 01:02 PM

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);

}


RE: C question. by John Anderton on 09-21-2006 at 01:11 PM

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;
}


RE: C question. by .Roy on 09-21-2006 at 01:24 PM

yeah well my job is to figure it out without swap but nvm dont tell me.


RE: C question. by .Roy on 09-21-2006 at 01:25 PM

oh and i also cant do 3 letters

like float a,b,c

and then do

a=c
c=b
b=a


RE: C question. by Eljay on 09-21-2006 at 01:33 PM

a = a + b;
b = a - b;
a = a - b;

google ftw.


RE: C question. by .Roy on 09-21-2006 at 01:46 PM

lol that works eljay thanks ;)


But i dont understand how that makes sence o.O

and what did u google to find that.


RE: C question. by RaceProUK on 09-21-2006 at 02:23 PM

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
RE: C question. by .Roy on 09-21-2006 at 03:53 PM

yes i managed to make mine work since u guys werent supposed to tell me the answer o.O

i dod
a=ab
b=a/b
a=a/b


RE: C question. by RaceProUK on 09-26-2006 at 11:29 AM

quote:
Originally posted by .Roy
a=ab
b=a/b
a=a/b
* RaceProUK slaps .Roy around a bit with a large trout.
That will be many times slower than using the add-subtract method above. Processors can add and subtract many times faster than they can multiply or divide.