View Full Version : i would like your opinion on my programs
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
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.
ya i was thinking about the filter, i think i will add it later.
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
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.
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...
void foo(void);
void bar(void);
int main(int argc, char *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. :)
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
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.
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. :)
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)
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
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.
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
"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.
well since im using DevC++ it automatically saves things in the .cpp extention. i think that it should probably support the long long variable.
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.