What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » C question.

Pages: (2): « First [ 1 ] 2 » Last »
C question.
Author: Message:
.Roy
Veteran Member
*****

Avatar

Posts: 1526
Reputation: 11
20 / Male / –
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
Profile PM Web Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
RE: C question.
Set it in a globally defined variable?
[Image: 1-0.png]
             
09-21-2006 12:48 PM
Profile PM Web Find Quote Report
.Roy
Veteran Member
*****

Avatar

Posts: 1526
Reputation: 11
20 / Male / –
Joined: Aug 2004
O.P. RE: C question.
What?

explain im new to this language.
09-21-2006 12:58 PM
Profile PM Web Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
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);

}

[Image: 1-0.png]
             
09-21-2006 01:02 PM
Profile PM Web Find Quote Report
John Anderton
Elite Member
*****

Avatar

Posts: 3908
Reputation: 80
37 / Male / Flag
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
Profile E-Mail PM Web Find Quote Report
.Roy
Veteran Member
*****

Avatar

Posts: 1526
Reputation: 11
20 / Male / –
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
Profile PM Web Find Quote Report
.Roy
Veteran Member
*****

Avatar

Posts: 1526
Reputation: 11
20 / Male / –
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
Profile PM Web Find Quote Report
Eljay
Elite Member
*****

Avatar
:O

Posts: 2949
Reputation: 77
– / Male / –
Joined: May 2004
RE: C question.
a = a + b;
b = a - b;
a = a - b;

google ftw.
09-21-2006 01:33 PM
Profile PM Find Quote Report
.Roy
Veteran Member
*****

Avatar

Posts: 1526
Reputation: 11
20 / Male / –
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
Profile PM Web Find Quote Report
RaceProUK
Elite Member
*****

Avatar

Posts: 6073
Reputation: 57
39 / Male / Flag
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
[Image: spartaafk.png]
09-21-2006 02:23 PM
Profile PM Web Find Quote Report
Pages: (2): « First [ 1 ] 2 » Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On