Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   i would like your opinion on my programs (http://www.chiefdelphi.com/forums/showthread.php?t=30722)

CmptrGk 12-10-2004 20:02

i would like your opinion on my programs
 
1 Attachment(s)
ok so im learning c to program the robot with and i decided to intergrate my algebra 2 into my programming, so i made two programs in c. one is a program that deals with factorials and the other is one that deals with permutations.

so i wanted to get your guys opinion on the programs. i have included the source code and executable files in the zip file attaced to this post. btw it is 19kb

scitobor 617 12-10-2004 23:15

Re: i would like your opinion on my programs
 
I tested your permutation code which compiled and ran without a hitch. I then reviewed your code and it is very good. However, you may want to add a filter to prevent someone from entering values that are too large.

CmptrGk 13-10-2004 06:15

Re: i would like your opinion on my programs
 
ya i was thinking about the filter, i think i will add it later.

rbayer 14-10-2004 01:03

Re: i would like your opinion on my programs
 
First of all, excellent work for someone just starting out with C. A few comments though to help make the transition from just playing with C to actually using it well:
First, unless you absolutely need to represent fractional numbers, you should always try avoid using doubles. They are _incredibly_ slow compared to ints. Since both factorial and permutations are purely integer functions, you would be much better off with simply using ints.
Second, code modularity is very good thing. If you have some time, I'd definately try re-writing your programs so that factorial is a function that you can call from within your permutation program. This idea of re-usable code and breaking things into smaller functions is the key to really using C well.
Third, global variables are a bad thing (unless you've got a darn good reason for using them). You can easily fix this by just moving your variable declarations to be inside the function where you want them.

Anyways, as I said, nice work and good luck learning C!

Rob

CmptrGk 14-10-2004 06:23

Re: i would like your opinion on my programs
 
the only reason i used the doubles was because the i wouldnt get any results for some larger numbers. i will also try to make seperate functions for each part (once i learn how to use them). and i did not know that you could instalise(is this the correct term) variables within functions.

Ryan M. 14-10-2004 14:22

Re: i would like your opinion on my programs
 
Quote:

Originally Posted by CmptrGk
the only reason i used the doubles was because the i wouldnt get any results for some larger numbers. i will also try to make seperate functions for each part (once i learn how to use them). and i did not know that you could instalise(is this the correct term) variables within functions.

Yeah, you can declare (your words was correct, if slightly misspelled ;)) a variable inside a function. This is greatly preferred by most authors, as it helps to keep everything modular and reusable. A variable declared inside a function, as foosFavorite is in this example...
PHP Code:

void foo(void);
void bar(void);

int main(int argcchar *argv[])
{
   
foo();
   
foosFavorite 1// Compile error - foosFavorite doesn't exist
}

void foo(void)
{
    
// Decalare a variable
    
int foosFavorite;
    
foosFavorite 1// Fine

    // Call bar()
    
bar();
}

void bar(void)
{
   
foosFavorite = -100// Compile error - foosFavorite doesn't exist


...is visible only to that function. For instance, in my example here, foosFavorite is able to be used only by foo(), not by the function which called it nor by the function foo() calls.

In C, you have to declare all local variables at the beginning of the function. No other executed line can come before them; comments are fine, but no function calls or anything. C++ relaxes this rule, so if you're actually using a C++ compiler, it will let you get away with not having declarations at the beginning. Be warned though; the MPLAB compiler only accepts at the beginning. :)

Ian W. 14-10-2004 14:43

Re: i would like your opinion on my programs
 
Quote:

Originally Posted by Ryan Morehart
In C, you have to declare all local variables at the beginning of the function. No other executed line can come before them; comments are fine, but no function calls or anything. C++ relaxes this rule, so if you're actually using a C++ compiler, it will let you get away with not having declarations at the beginning. Be warned though; the MPLAB compiler only accepts at the beginning. :)

In general though, it's a good habit to declare anything and everything you'll possibly need for the function in the first lines of the function, rather than spread out amongst the random executed lines. It makes the code so much easier to read ;-). Plus, as Ryan said, it's the way to do it in C, and since the robots are all programmed in C (and I don't ever see that changing, because C has been around for quite some time...).

Also, I'm not 100% certain, but I believe it's faster if all your code is C, instead of C++ (this comes from extremely limited programming for Palm IIIs), but someone would have to confirm this, because I can't really remember the reasons or anything.

MikeDubreuil 14-10-2004 15:23

Re: i would like your opinion on my programs
 
Quote:

Originally Posted by Ian W.
In general though, it's a good habit to declare anything and everything you'll possibly need for the function in the first lines of the function, rather than spread out amongst the random executed lines. It makes the code so much easier to read ;-).

Just a FYI...
Some complilers will give you errors if you begin assigning values to variables and then try to declare another variable. Therefore, it is extremely important to declare all variables at the top of the function.

Ryan M. 14-10-2004 15:59

Re: i would like your opinion on my programs
 
Quote:

Originally Posted by Ian W.
Also, I'm not 100% certain, but I believe it's faster if all your code is C, instead of C++ (this comes from extremely limited programming for Palm IIIs), but someone would have to confirm this, because I can't really remember the reasons or anything.

Maybe very slightly, but one of the biggest reasons that programmers have moved on/are moving to C++ is the fact it adds so many features with often little or not penalty. Granted, on a Palm with a relatively slow chip, any difference at all might be a noticeable. But, I've never done Palm stuff. :)

CmptrGk 14-10-2004 17:30

Re: i would like your opinion on my programs
 
thank you for giving me some input on my programs, with all of your suggestions i will probably be able to write better programs.

(i also cant spell at all)

rbayer 14-10-2004 18:17

Re: i would like your opinion on my programs
 
Quote:

Originally Posted by CmptrGk
the only reason i used the doubles was because the i wouldnt get any results for some larger numbers.

Depending on the computer/compiler, you can usually declare a variable as a "long int" or even as a "long long", which (again, depending on compiler) tends to be 64 bits. This is large. Very, very large. Also, if you add the word "unsigned" before int, it lets you use much bigger numbers (at the expense of not allowing numbers less than 0). So, for example, you could declare a variable as type "unsigned long int" and it should be plenty big for anything and everything you would ever want to take the factorial of.

-Rob

phrontist 14-10-2004 19:19

Re: i would like your opinion on my programs
 
Quote:

Originally Posted by CmptrGk
the only reason i used the doubles was because the i wouldnt get any results for some larger numbers.

Use a long int or long long int.

CmptrGk 14-10-2004 19:42

Re: i would like your opinion on my programs
 
Quote:

Originally Posted by phrontist
Use a long int or long long int.

i will try the long int and the long long

Joe Ross 14-10-2004 20:08

Re: i would like your opinion on my programs
 
"long int" is longhand for just long. long long is what Dillon wants, if his compiler supports it.

Also, while C++ is generally slower then C, it's only slower if you use C++ features. In Dillon's case, he named his file cpp, but didn't use anything not in C.

CmptrGk 14-10-2004 21:28

Re: i would like your opinion on my programs
 
well since im using DevC++ it automatically saves things in the .cpp extention. i think that it should probably support the long long variable.


All times are GMT -5. The time now is 04:22.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi