473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

help with a funcition-please

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:

Jun 5 '06 #1
11 1675
On 2006-06-05, natman <na*******@gmail.com> wrote:
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!
Jun 5 '06 #2
My vectors are defined via

int vector[n]

n dim vector
Andrew Poelstra wrote:
On 2006-06-05, natman <na*******@gmail.com> wrote:
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!


Jun 5 '06 #3
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 Poelstra wrote:
On 2006-06-05, natman <na*******@gmail.com> wrote:
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!


Jun 5 '06 #4
"natman" <na*******@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 5 '06 #5
On 2006-06-05, natman <na*******@gmail.com> wrote:
Andrew Poelstra wrote:
On 2006-06-05, natman <na*******@gmail.com> wrote:
> 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?

--
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!
Jun 5 '06 #6
On 2006-06-05, natman <na*******@gmail.com> wrote:
Andrew Poelstra wrote:
On 2006-06-05, natman <na*******@gmail.com> wrote:
> 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.

--
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!
Jun 6 '06 #7
Andrew Poelstra wrote:
On 2006-06-05, natman <na*******@gmail.com> wrote:
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);
}

--
Simon.
Jun 6 '06 #8
On 2006-06-06, Simon Biber <ne**@ralmin.cc> wrote:
Andrew Poelstra wrote:
On 2006-06-05, natman <na*******@gmail.com> wrote:
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.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 6 '06 #9
Andrew Poelstra wrote:
On 2006-06-06, Simon Biber <ne**@ralmin.cc> wrote:
Andrew Poelstra wrote:
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.


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.

--
Simon.
Jun 6 '06 #10
In article <44********@news.peopletelecom.com.au>,
Simon Biber <ne**@ralmin.cc> wrote:
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.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Jun 7 '06 #11
Walter Roberson wrote:
In article <44********@news.peopletelecom.com.au>,
Simon Biber <ne**@ralmin.cc> wrote:
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.


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.

--
Simon.

--
Posted via a free Usenet account from http://www.teranews.com

Jun 7 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
4
by: nguyenlh | last post by:
i make a gridview and i make a code : delete row in gridview public bool Delete(int id, out string Msg) { Msg = ""; try { SqlServerDb sql = new SqlServerDb();// library connect...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.