Please can anyone help me on this code? I want
to write all odd numbers in range from a to b but it doesn't work at all
cases mainly when a is higher than b and when i type in negative numbers.
Here is the code and i would appreciate anybody helping me. THANKS.
#include<stdio.h>
#include<math.h>
void odd(float x, float y);
void change(float x, float y);
void odd(float x, float y)
{
int i;
if (x>y) change(x,y);
x=roundf(x);
y=roundf(y);
for(i=1;i<(y-x)+1;i++)
if ( fmodf ((x+i),2)==1 || fmodf ((x+i),2)==-1 ) printf( "%f
",(x+i) );
}
void change(float x, float y)
{
float c;
c=x;
x=y;
y=c;
}
main()
{
float a,b;
printf("Type two real numbers :");
scanf("%f%f",&a,&b);
odd(a,b);
getchar();
getchar();
} 9 1372
kupiko wrote: Please can anyone help me on this code? I want to write all odd numbers in range from a to b but it doesn't work at all cases mainly when a is higher than b and when i type in negative numbers.
Here is the code and i would appreciate anybody helping me. THANKS.
#include<stdio.h> #include<math.h> void odd(float x, float y); void change(float x, float y);
void odd(float x, float y) { int i; if (x>y) change(x,y); x=roundf(x); y=roundf(y); for(i=1;i<(y-x)+1;i++) if ( fmodf ((x+i),2)==1 || fmodf ((x+i),2)==-1 ) printf( "%f ",(x+i) ); }
void change(float x, float y) { float c; c=x; x=y; y=c; }
Just on first glance (i.e. I wouldn't swear that you don't have
other bugs...), your problem is in how you call change().
When you call a C function, the function works on local copies
of the parameters -- not on the values in the scope of the caller.
So here -- you have the following:
main gets a and b (main:a, main:b for short), calls odd with them.
odd gets copies of main:a, main:b which are named odd:x, odd:y.
main:a and main:b are (of course) unchanged).
odd calls change with odd:x, odd:y -- and change's copies are
called change:x, change:y.
change:x, change:y and change:c are modified and change returns..
leaving odd:x and odd:y unchanged.
So if you want to do this, I'd recommend one of the following:
i) Rewrite change to be a macro, not a function so that
you work with the actual variables you intend, such as:
#define SWAP_FLOAT(float __x, float __y ) \
do { \
float __temp; \
__temp = __x; \
__x = __y; \
__y = __temp; \
} while (0)
This lets you use SWAP_FLOAT as if it were a function,
having odd() do SWAP_FLOAT(x, y) will change the
variables in odd()'s scope [odd:x, odd:y] as you intend.
ii) Rewrite change to work with the addresses of the
variables, not the contents.
void change (float *x, float *y)
{
float temp;
if ( x == NULL || y == NULL ) {
/* Die with error? Your call on handling */
return;
}
temp = *x;
*x = *y;
*y = temp;
}
Don main() {
float a,b; printf("Type two real numbers :"); scanf("%f%f",&a,&b); odd(a,b); getchar(); getchar(); }
>void change (float *x, float *y) { float temp; if ( x == NULL || y == NULL ) { /* Die with error? Your call on >handling */ return; } temp = *x; *x = *y; *y = temp; }
Thank you very much for this advice. It's first time I see how to use
pointers and it is useful as I see but still there are errors in my
rewritten code. And i don't know anything about pointers so i can't solve
what it mean could you please help me?
I haven't much time otherwise i would not annoy you.
ERROR:
cannot convert `float' to `float*' for argument `1' to `void
vymen(float*, float*)'
CODE:
#include<stdio.h>
#include<math.h>
void odd(float x, float y);
void change(float *x, float *y);
void odd(float x, float y)
{
int i;
if (x>y) change(x,y);
x=roundf(x);
y=roundf(y);
for(i=1;i<(y-x)+1;i++)
if ( fmodf ((x+i),2)==1 || fmodf ((x+i),2)==-1 ) printf( "%f
",(x+i) );
}
void change(float *x, float *y)
{
float c;
if ( x==NULL || y==NULL ) return;
c=*x;
*x=*y;
*y=c;
}
main()
{
float a,b;
printf("Type two real numbers :");
scanf("%f%f",&a,&b);
odd(a,b);
getchar();
getchar();
}
kupiko wrote: void change (float *x, float *y) { float temp; if ( x == NULL || y == NULL ) { /* Die with error? Your call on >handling */ return; } temp = *x; *x = *y; *y = temp; }
Thank you very much for this advice. It's first time I see how to use pointers and it is useful as I see but still there are errors in my rewritten code. And i don't know anything about pointers so i can't solve what it mean could you please help me? I haven't much time otherwise i would not annoy you.
It isn't annoying me... just a sign my mind isn't focused on
work yet this morning. :) Sorry I wasn't more clear. ERROR: cannot convert `float' to `float*' for argument `1' to `void vymen(float*, float*)'
Right -- we've changed the function to take pointers, which means
you need to call it with pointers (aka the address of x, not x
itself). CODE:
#include<stdio.h> #include<math.h>
void odd(float x, float y); void change(float *x, float *y);
void odd(float x, float y) { int i; if (x>y) change(x,y);
if ( x > y ) {
change(&x, &y);
}
Don
x=roundf(x); y=roundf(y); for(i=1;i<(y-x)+1;i++) if ( fmodf ((x+i),2)==1 || fmodf ((x+i),2)==-1 ) printf( "%f ",(x+i) ); }
void change(float *x, float *y) { float c; if ( x==NULL || y==NULL ) return; c=*x; *x=*y; *y=c; }
main() { float a,b; printf("Type two real numbers :"); scanf("%f%f",&a,&b); odd(a,b); getchar(); getchar(); }
On Fri, 01 Apr 2005 14:07:53 +0000, Don Morris wrote: #define SWAP_FLOAT(float __x, float __y ) \
Is this a C99 thing? I've never seen a function-like macro defined this
way (with types) and my compiler won't accept it.
Maybe you meant to type:
#define SWAP_FLOAT(__x, __y ) \
do { \ float __temp; \ __temp = __x; \ __x = __y; \ __y = __temp; \ } while (0)
And why are you using the undercores in the identifier names? They seem
to me to be completely unnecessary. Couldn't you just do it like this:
#define SWAP_FLOAT(x, y ) \
do { \
float temp; \
temp = x; \
x = y; \
y = temp; \
} while (0)
--Mac
Mac wrote: On Fri, 01 Apr 2005 14:07:53 +0000, Don Morris wrote:
#define SWAP_FLOAT(float __x, float __y ) \
Is this a C99 thing? I've never seen a function-like macro defined this way (with types) and my compiler won't accept it.
And your compiler is correct! Maybe you meant to type: #define SWAP_FLOAT(__x, __y ) \
do { \ float __temp; \ __temp = __x; \ __x = __y; \ __y = __temp; \ } while (0)
And why are you using the undercores in the identifier names? They seem to me to be completely unnecessary. Couldn't you just do it like this:
Well, it was a misguided attempt at avoiding inadvertent variable
capture. Identifiers starting with an underscore are reserved for the
implementation. #define SWAP_FLOAT(x, y ) \ do { \ float temp; \ temp = x; \ x = y; \ y = temp; \ } while (0)
Consider, however, what happens if you have the following code:
void nonsensical() {
float temp = 3.0;
float other = 4.0;
printf("before swap, temp = %f, other = %f", temp, other);
SWAP_FLOAT(temp, other);
printf("after swap, temp = %f, other = %f", temp, other);
}
Macros can be useful -- but (particularly if they declare variables) can
bite you in the^H^H^H^H^H^H^H^H^H^H^H^H^H^H^Hcause problems.
HTH,
--ag
--
Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays
Artie Gold wrote: Mac wrote: #define SWAP_FLOAT(x, y ) \ do { \ float temp; \ temp = x; \ x = y; \ y = temp; \ } while (0)
Consider, however, what happens if you have the following code:
void nonsensical() { float temp = 3.0; float other = 4.0; printf("before swap, temp = %f, other = %f", temp, other); SWAP_FLOAT(temp, other); printf("after swap, temp = %f, other = %f", temp, other); }
The problem is that 'temp' in the macro clashes with 'temp' in
the parameter. So how about this:
#define SWAP_FLOAT(x, y) \
do { \
float x##y; \
x##y = x; \
x = y; \
y = x##y; \
} while (0)
No possibility of clashes now (even if 'x' and 'y' are #defined
to something else already, I think)
Old Wolf wrote: ... The problem is that 'temp' in the macro clashes with 'temp' in the parameter. So how about this:
#define SWAP_FLOAT(x, y) \ do { \ float x##y; \ x##y = x; \ x = y; \ y = x##y; \ } while (0)
SWAP_FLOAT(er, rno);
SWAP_FLOAT(x, *y);
--
Peter
Peter Nilsson wrote: Old Wolf wrote: ... The problem is that 'temp' in the macro clashes with 'temp' in the parameter. So how about this:
#define SWAP_FLOAT(x, y) \ do { \ float x##y; \ x##y = x; \ x = y; \ y = x##y; \ } while (0) SWAP_FLOAT(er, rno);
No problem, local errno takes precedence over global errno.
SWAP_FLOAT(x, *y);
Ah, serious problem.I guess that takes us back to the
pick-something-weird-and-wacky option.
Old Wolf wrote: Peter Nilsson wrote: Old Wolf wrote: ... The problem is that 'temp' in the macro clashes with 'temp' in the parameter. So how about this:
#define SWAP_FLOAT(x, y) \ do { \ float x##y; \ x##y = x; \ x = y; \ y = x##y; \ } while (0)
SWAP_FLOAT(er, rno);
No problem, local errno takes precedence over global errno.
You don't seem to realise that, if <errno.h> is included,
errno is a _macro_ "...which expands to a modifiable lvalue
that has type int". [7.5p2] It needn't expand to the name of
a 'global'. SWAP_FLOAT(x, *y);
Ah, serious problem.I guess that takes us back to the pick-something-weird-and-wacky option.
Or to 'inline' under C99, if such an implementation is available.
--
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: mike |
last post by:
Hello,
After trying to validate this page for a couple of days now I was
wondering if someone might be able to help me out. Below is a list of
snippets where I am having the errors.
1. Line 334,...
|
by: John Flynn |
last post by:
hi all
i'm going to be quick i have an assignment due which i have no idea how to
do. i work full time so i dont have the time to learn it and its due date
has crept up on me ..
As follows:...
|
by: xunling |
last post by:
i have a question about answering ..... this topic is "need help"
what do i have to write at te topic line, !after i have klicked the
"answer message" button
ive tried many possibilities,...
|
by: sk |
last post by:
I have an applicaton in which I collect data for different parameters
for a set of devices. The data are entered into a single table, each
set of name, value pairs time-stamped and associated with...
|
by: Timothy Shih |
last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I
wrote a simple function which takes in 2 buffers (one a byte buffer, one a
char buffer) and copies the contents of the byte...
|
by: Cheryl Langdon |
last post by:
Hello everyone,
This is my first attempt at getting help in this manner. Please
forgive me if this is an inappropriate request.
I suddenly find myself in urgent need of instruction on how to...
|
by: pamelafluente |
last post by:
I am still working with no success on that client/server
problem. I need your help. I will submit simplified versions
of my problem so we can see clearly what is going on.
My model:
A client...
|
by: skumar434 |
last post by:
i need to store the data from a data base in to structure
.............the problem is like this ....suppose there is a data base
which stores the sequence no and item type etc ...but i need only...
|
by: U S Contractors Offering Service A Non-profit |
last post by:
Brilliant technology helping those most in need Inbox
Reply
U S Contractors Offering Service A Non-profit
show details
10:37 pm (1 hour ago)
Brilliant technology helping those most in need ...
|
by: mike |
last post by:
I help manage a large web site, one that has over
600 html pages... It's a reference site for ham radio
folks and as an example, one page indexes over
1.8 gb of on-line PDF documents.
The site...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |