help with a funcition-please

N

natman

Hi i need to write a funcition called install, its takes as arguments
1- a vector
2-a number
3-a list called next_level

so heres where it get kinda weird:

next_level is a list comprising of elements that are { vectors, with
pointers}, defined by the following struct

typedef struct wnode {
int weight[maxrank+1];
struct wnode *next;
} WEIGHTPTR,*WEIGHTPTR;

Now install is a funcition that returns a pointer to the frist vector
in the list, Can anyone give the code to define this funcition.?

Thanks:
 
A

Andrew Poelstra

Hi i need to write a funcition called install, its takes as arguments
Okey doke.
1- a vector

You'll need a struct (for an n-dimensional vector):

struct vector {
int direction[n];
int magnitude;
}
2-a number

int will work; long or short may be more appropriate, but I can't
know without more information.
3-a list called next_level

What kind of list? All programming textbooks have code for linked lists;
the only one I can recommend from experience is C Unleashed by Richard
Heathfield, Lawrence Kirby, and a few others guys.
so heres where it get kinda weird:

next_level is a list comprising of elements that are { vectors, with
pointers}, defined by the following struct

typedef struct wnode {
int weight[maxrank+1];
struct wnode *next;
} WEIGHTPTR,*WEIGHTPTR;

I see a pointer, but I'm not sure how you are representing a vector in
that; I suspect that the +1 allows for magnitude, as well as maxrank
directions.
Now install is a funcition that returns a pointer to the frist vector
in the list, Can anyone give the code to define this funcition.?
Nope. We'll help you out when you've got a compilable example, but until
then we won't do your homework for you. I'll give you a hint, though:
Double-linked lists will help you.
 
N

natman

My vectors are defined via

int vector[n]

n dim vector


Andrew said:
Hi i need to write a funcition called install, its takes as arguments
Okey doke.
1- a vector

You'll need a struct (for an n-dimensional vector):

struct vector {
int direction[n];
int magnitude;
}
2-a number

int will work; long or short may be more appropriate, but I can't
know without more information.
3-a list called next_level

What kind of list? All programming textbooks have code for linked lists;
the only one I can recommend from experience is C Unleashed by Richard
Heathfield, Lawrence Kirby, and a few others guys.
so heres where it get kinda weird:

next_level is a list comprising of elements that are { vectors, with
pointers}, defined by the following struct

typedef struct wnode {
int weight[maxrank+1];
struct wnode *next;
} WEIGHTPTR,*WEIGHTPTR;

I see a pointer, but I'm not sure how you are representing a vector in
that; I suspect that the +1 allows for magnitude, as well as maxrank
directions.
Now install is a funcition that returns a pointer to the frist vector
in the list, Can anyone give the code to define this funcition.?
Nope. We'll help you out when you've got a compilable example, but until
then we won't do your homework for you. I'll give you a hint, though:
Double-linked lists will help you.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
If we would just let the poachers into the zoo, we'd
have less squashed people and more fancy pianos!
 
N

natman

Ok i have the whole program working, but it is a fair amount of code,
with somestange stuff that really only cofuses people, so i will post
my guess for defining the install func


WEIGHTPTR install ( int *v , int rank , WEIGHTPTR next_level ){
++next_level.next;
return next_level;
}

see previous post for defn of struct WEIGHTPTR,



Andrew said:
Hi i need to write a funcition called install, its takes as arguments
Okey doke.
1- a vector

You'll need a struct (for an n-dimensional vector):

struct vector {
int direction[n];
int magnitude;
}
2-a number

int will work; long or short may be more appropriate, but I can't
know without more information.
3-a list called next_level

What kind of list? All programming textbooks have code for linked lists;
the only one I can recommend from experience is C Unleashed by Richard
Heathfield, Lawrence Kirby, and a few others guys.
so heres where it get kinda weird:

next_level is a list comprising of elements that are { vectors, with
pointers}, defined by the following struct

typedef struct wnode {
int weight[maxrank+1];
struct wnode *next;
} WEIGHTPTR,*WEIGHTPTR;

I see a pointer, but I'm not sure how you are representing a vector in
that; I suspect that the +1 allows for magnitude, as well as maxrank
directions.
Now install is a funcition that returns a pointer to the frist vector
in the list, Can anyone give the code to define this funcition.?
Nope. We'll help you out when you've got a compilable example, but until
then we won't do your homework for you. I'll give you a hint, though:
Double-linked lists will help you.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
If we would just let the poachers into the zoo, we'd
have less squashed people and more fancy pianos!
 
K

Keith Thompson

natman said:
Hi i need to write a funcition called install, its takes as arguments
1- a vector
2-a number
3-a list called next_level

so heres where it get kinda weird:

next_level is a list comprising of elements that are { vectors, with
pointers}, defined by the following struct

typedef struct wnode {
int weight[maxrank+1];
struct wnode *next;
} WEIGHTPTR,*WEIGHTPTR;

I doubt that. You've defined WEIGHTPTR twice, once as an alias for
your struct, and again as an alias for a pointer to your struct.

Presumably maxrank is a macro for some integer constant. As a matter
of style, it should be in all-caps: MAXRANK.

Indentation is important if you want your code to be readable, even in
small fragments.

Many people (myself included) think that typedefs for structure types
are useless in most cases. There's no good reason to define another
name (like, say, "WEIGHT") for something that already has a perfectly
good name (like "struct wnode"). There are even better reasons to
avoid typedefs for pointer types; a pointer to an object is very
different from the object itself, and hiding that distinction behind a
typedef can be positively dangerous.

So here's how I'd re-write your declaration above:

#define MAXRANK some_integer_constant_expression

struct wnode {
int weight[MAXRANK+1];
struct wnode *next;
};

Any code using this type would then simply refer to it as "struct wnode";
if you want a pointer, refer to it as "struct wnode *".
Now install is a funcition that returns a pointer to the frist vector
in the list, Can anyone give the code to define this funcition.?

With the information you've given us, probably not. Show us some real
code. Don't try to paraphrase it; copy-and-paste the exact code that
you fed to the compiler. Otherwise, there's no way we can guess which
errors are in your actual code, and which you introduced by re-typing
it (such as your double declaration of WEIGHTPTR).

Try implementing the function yourself. If you run into trouble, show
us what you have and explain what it's supposed to do, and how that
differs from what it actually does. We'll be glad to help if we can,
but we aren't going to write it for you.
 
A

Andrew Poelstra

Andrew said:
Hi i need to write a funcition called install, its takes as arguments
Okey doke.
1- a vector

You'll need a struct (for an n-dimensional vector):

struct vector {
int direction[n];
int magnitude;
}
2-a number

int will work; long or short may be more appropriate, but I can't
know without more information.
3-a list called next_level

What kind of list? All programming textbooks have code for linked lists;
the only one I can recommend from experience is C Unleashed by Richard
Heathfield, Lawrence Kirby, and a few others guys.
so heres where it get kinda weird:

next_level is a list comprising of elements that are { vectors, with
pointers}, defined by the following struct

typedef struct wnode {
int weight[maxrank+1];
struct wnode *next;
} WEIGHTPTR,*WEIGHTPTR;

I see a pointer, but I'm not sure how you are representing a vector in
that; I suspect that the +1 allows for magnitude, as well as maxrank
directions.
Now install is a funcition that returns a pointer to the frist vector
in the list, Can anyone give the code to define this funcition.?
Nope. We'll help you out when you've got a compilable example, but until
then we won't do your homework for you. I'll give you a hint, though:
Double-linked lists will help you.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
If we would just let the poachers into the zoo, we'd
have less squashed people and more fancy pianos!
My vectors are defined via

int vector[n]

n dim vector

Don't top post. I've fixed it this time.

Vectors need a magnitude, which I would state explicitly, but that's
just my opinion.

Which element of vector is the magnitude?
 
A

Andrew Poelstra

Andrew said:
Hi i need to write a funcition called install, its takes as arguments
Okey doke.
1- a vector

You'll need a struct (for an n-dimensional vector):

struct vector {
int direction[n];
int magnitude;
}
2-a number

int will work; long or short may be more appropriate, but I can't
know without more information.
3-a list called next_level

What kind of list? All programming textbooks have code for linked lists;
the only one I can recommend from experience is C Unleashed by Richard
Heathfield, Lawrence Kirby, and a few others guys.
so heres where it get kinda weird:

next_level is a list comprising of elements that are { vectors, with
pointers}, defined by the following struct

typedef struct wnode {
int weight[maxrank+1];
struct wnode *next;
} WEIGHTPTR,*WEIGHTPTR;

I see a pointer, but I'm not sure how you are representing a vector in
that; I suspect that the +1 allows for magnitude, as well as maxrank
directions.
Now install is a funcition that returns a pointer to the frist vector
in the list, Can anyone give the code to define this funcition.?
Nope. We'll help you out when you've got a compilable example, but until
then we won't do your homework for you. I'll give you a hint, though:
Double-linked lists will help you.
Ok i have the whole program working, but it is a fair amount of code,
with somestange stuff that really only cofuses people, so i will post
my guess for defining the install func


WEIGHTPTR install ( int *v , int rank , WEIGHTPTR next_level ){
++next_level.next;
return next_level;
}

see previous post for defn of struct WEIGHTPTR,

Don't top post, and snip signatures. I've fixed both for you.

As Keith mentioned, aliasing structs is not good style.

I'm not sure what is meant by WEIGHT and WEIGHTPTR; are either of
those pointers? In your previous code, there was no distinction.
Also, you return a WEIGHTPTR even though the function says to return
a WEIGHT. If the two are the same, why do they have different typedefs?
I'll assume that neither are pointers for my analysis:

WEIGHT install (int *v, int rank, WEIGHTPTR next_level)
/* WARNING: int *v and int rank are unused. */
{
next_level.next++; /* The ++ should be on the right for simple statements,
in my opinion. */

return next_level; /* Note: This function merely increases the next pointer,
which isn't defined as an array. I don't know
why you are doing that, but it doesn't look like
it's worth an entire function. */
}

You need to post more code.
 
S

Simon Biber

Andrew said:
My vectors are defined via

int vector[n]

n dim vector

Don't top post. I've fixed it this time.

Vectors need a magnitude, which I would state explicitly, but that's
just my opinion.

Which element of vector is the magnitude?

You don't need to store the magnitude within the vector. Most vector
code does not do that. It makes it slow to change components of the
vector, if you have to update the magnitude every time. Just calculate
the magnitude when you need it.

struct Vector {
float x, y, z;
};

float magnitude(const struct Vector *v)
{
return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
}
 
A

Andrew Poelstra

Andrew said:
My vectors are defined via

int vector[n]

n dim vector

Don't top post. I've fixed it this time.

Vectors need a magnitude, which I would state explicitly, but that's
just my opinion.

Which element of vector is the magnitude?

You don't need to store the magnitude within the vector. Most vector
code does not do that. It makes it slow to change components of the
vector, if you have to update the magnitude every time. Just calculate
the magnitude when you need it.

struct Vector {
float x, y, z;
};

float magnitude(const struct Vector *v)
{
return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
}

Couldn't you calculate it once and store it in the struct? Then
you would only have to update it in certain situations, and you
wouldn't need expensive operations such as sqrtf as frequently.
 
S

Simon Biber

Andrew said:
Couldn't you calculate it once and store it in the struct? Then
you would only have to update it in certain situations, and you
wouldn't need expensive operations such as sqrtf as frequently.

Yes, you could -- but it depends on how your code is used. If you need
to access the magnitude of a vector more often than you create new
vectors or modify vector components, then you will benefit from
calculating it once and storing it. On the other hand, if you don't need
to access the magnitude for all the vectors you create, then you are
wasting time calculating it when it's not necessary.

You could calculate it on demand but store it anyway.

#include <math.h>

struct Vector
{
float x, y, z;
float mag;
};

float getMag(struct Vector *v)
{
if(v->mag == -1) // must calculate magnitude
{
v->mag = sqrtf(v->x * v->x, v->y * v->y, v->z * v->z);
}

return v->mag;
}

void setVector(struct Vector *v, float x, float y, float z)
{
v->x = x;
v->y = y;
v->z = z;
v->mag = -1; // invalidate calculated magnitude
}

The disadvantages of this include storing an extra float per vector, and
remembering to invalidate the calculated magnitude when creating or
changing a vector.
 
W

Walter Roberson

Simon Biber said:
On the other hand, if you don't need
to access the magnitude for all the vectors you create, then you are
wasting time calculating it when it's not necessary.
You could calculate it on demand but store it anyway.
#include <math.h>
struct Vector
{
float x, y, z;
float mag;
};
float getMag(struct Vector *v)
{
if(v->mag == -1) // must calculate magnitude
{
v->mag = sqrtf(v->x * v->x, v->y * v->y, v->z * v->z);
}

return v->mag;
}

If the vectors represent complex numbers (or their components are
complex numbers) then the magnitude could be -1, leading to
unnecessary recalculations.

But more of a problem is that you are relying on comparison
of floating point numbers. Even though -1 is in the range that
should be exactly representable, floating point comparisons taint
something we want to encourage.
 
S

Simon Biber

Walter said:
If the vectors represent complex numbers (or their components are
complex numbers) then the magnitude could be -1, leading to
unnecessary recalculations.

The result of the sqrtf function can never be -1. While of course the
square roots of 1 include both 1 and -1, the sqrtf function must return
the positive root.

As for complex numbers, quoting from C99 7.3.8.3 "The csqrt functions
return the complex square root value, in the range of the right
halfplane (including the imaginary axis)."

Pray tell, what complex number would make csqrtf return -1?

But more of a problem is that you are relying on comparison
of floating point numbers. Even though -1 is in the range that
should be exactly representable, floating point comparisons taint
something we want to encourage.

If you feel tainted by comparing floating point values, you are free to
use a NaN as the marker value, or even include a separate _Bool or
similar in the struct.

I believe that -1 is adequate and should work in all situations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top