LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2003, 05:12 PM   #1
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Rep: Reputation: 45
need some constructive critisism on some code I wrote.


Hello. I'm not that advanced in the art of programming. So I just need some evaluation of code I have just written.
Am I doing things efficiently? Do you understand what i'm trying to do?
Here is the code

Basically what I'm trying to create is an computer algabra system. It should look something like :

Code:
> tan(1/2*pi);

infinite

> f:x->sin(x)/x;

          sin(x)
f(x)=  -------
             x

> f(3*pi);

0
Here is a similar, but much more simpeler program I have created, to give you an idea of what I want.

Last edited by qanopus; 06-24-2003 at 05:17 PM.
 
Old 06-24-2003, 05:57 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Two things:
1) Why do you have an empty "#define USER_DEF" statement? Your comment above mentions a index into an array, and some other stuff, but there is no clear definition (or commented examples) of what that define is for or how it will be used if it's needed by another file.

2) Why do you declare the "data" member of your structure to be void? I would declare it as an int, long int, or a character pointer.

Without some actual functional code though, it's not really possible to have many suggestions.
 
Old 06-25-2003, 03:53 AM   #3
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Original Poster
Rep: Reputation: 45
Well, your right about the definition of USER_DEF. It's not really needed. I just wanted to make the point that an element having a data type FUNCTION, can be interpreted two way. If the data it holds is smaller then a specified value, it's somthing that has been predefined, like a trigonometric function. Else it's somthing that the user defined.
I have a bit of a problem with the "data" member of my structure. The thing is, most of the time it will hold an integer value, because it will just be a referance to where the data actually is. It can, for example be an index of an array. Somthimes though, it will have to be a floating point value, if the type of data it holds is a number. Now I considder it quite an waste of memory if most of the time I use the presision of a floating point value when it really just holds an integer. So just for now I declared it void till I figure out how to handle this. Any suggestions?
 
Old 06-25-2003, 08:42 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Type casting can solve that problem. Just declare data as a long, a double, or whatever you want. When you reference it, just typecast it to whatever value you need it to be. For example:

Code:
if (data_type == INTEGER)
  printf("You gave me an int with a value of %d\n", (int)data );

if (data_type == FLOAT)
  printf("You gave me a float with a vallue of %f\n", (double)data);

...
 
Old 06-25-2003, 09:31 AM   #5
qanopus
Senior Member
 
Registered: Jul 2002
Location: New York
Distribution: Slackware
Posts: 1,358

Original Poster
Rep: Reputation: 45
Ye I know I can do that. And I have. It's just that the variable will hold enough "room" for a floating point value, while most of the time it will represent an integer. And that's a waste of memory.
 
Old 06-25-2003, 10:03 AM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Then make data a pointer. A pointer to an int is the same size as a pointer to a float, a pointer to a string, and a pointer to a function. It will require the extra steps of reserving and freeing memory. More efficient? Well, it depends on whther you'll use strings. If you use it just for ints and doubles, then the addition of four bytes of overhead for each doesn't make sense when compared to using the space for a double as an int or a double; you'd be wasting space roughly half as many times.

To ensure your data field is EXACTLY the right width for your specific data type, you will need to use the pointer method described above, or use strings to hold your data, and convert back and forth when necessary.

That is, unless somebody else out there knows a way that I haven't been exposed to yet.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the last program you wrote (or are currently writing)? lowpro2k3 Programming 2 08-15-2005 01:14 AM
font looks like its been wrote with a pen insurin Linux - Newbie 4 07-05-2005 09:50 AM
Why The block driver I wrote can't read? freasy Programming 2 07-17-2004 06:50 AM
Can anyone help me with a C prog I wrote? WorldBuilder Linux - Software 10 10-23-2003 09:11 AM
Wrote a program: TEXTDRAW lea Programming 1 08-13-2002 09:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration