Go to Post *referring to Team Titanium's 2012 robot* If only it could play guitar hero at the same time. sigh. - rsisk [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 12-10-2004, 20:02
CmptrGk's Avatar
CmptrGk CmptrGk is offline
Programming Mentor
AKA: Dillon
FRC #0716 (Who'sCTEKS)
Team Role: College Student
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Kent,CT
Posts: 212
CmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud of
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
Attached Files
File Type: zip factorial-permutation programs.zip (19.9 KB, 61 views)
__________________
#14


"Great autonomous mode Dillon."," It hasn't been written yet"
after an Archimedes match



Last edited by CmptrGk : 12-10-2004 at 21:21.
  #2   Spotlight this post!  
Unread 12-10-2004, 23:15
scitobor 617's Avatar
scitobor 617 scitobor 617 is offline
More nerd than you can handle!
AKA: Alan Meekins
FRC #0617 (HSHS 617 "DUKE")
Team Role: Programmer
 
Join Date: Nov 2003
Rookie Year: 2003
Location: Richmond, VA
Posts: 153
scitobor 617 will become famous soon enoughscitobor 617 will become famous soon enough
Send a message via AIM to scitobor 617
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.
__________________
Beta testers needed!
http://www.nullagent.no-ip.com

Your kidding, there are other operating systems besides Linux?!
  #3   Spotlight this post!  
Unread 13-10-2004, 06:15
CmptrGk's Avatar
CmptrGk CmptrGk is offline
Programming Mentor
AKA: Dillon
FRC #0716 (Who'sCTEKS)
Team Role: College Student
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Kent,CT
Posts: 212
CmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud of
Re: i would like your opinion on my programs

ya i was thinking about the filter, i think i will add it later.
__________________
#14


"Great autonomous mode Dillon."," It hasn't been written yet"
after an Archimedes match


  #4   Spotlight this post!  
Unread 14-10-2004, 01:03
rbayer's Avatar Unsung FIRST Hero
rbayer rbayer is offline
Blood, Sweat, and Code
no team (Teamless Orphan)
 
Join Date: Mar 2002
Rookie Year: 2001
Location: Minnetonka, MN
Posts: 1,087
rbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of light
Send a message via AIM to rbayer
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
__________________
New C-based RoboEmu2 (code simulator) available at: http://www.robbayer.com/software.php
  #5   Spotlight this post!  
Unread 14-10-2004, 06:23
CmptrGk's Avatar
CmptrGk CmptrGk is offline
Programming Mentor
AKA: Dillon
FRC #0716 (Who'sCTEKS)
Team Role: College Student
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Kent,CT
Posts: 212
CmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud of
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.
__________________
#14


"Great autonomous mode Dillon."," It hasn't been written yet"
after an Archimedes match


  #6   Spotlight this post!  
Unread 14-10-2004, 14:22
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
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.
__________________


Last edited by Ryan M. : 14-10-2004 at 14:24.
  #7   Spotlight this post!  
Unread 14-10-2004, 14:43
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
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.
__________________
AIM --> Woloi
Email --> ian@woloschin.com
  #8   Spotlight this post!  
Unread 14-10-2004, 15:23
MikeDubreuil's Avatar
MikeDubreuil MikeDubreuil is offline
Carpe diem
FRC #0125 (Nu-Trons)
Team Role: Engineer
 
Join Date: Jan 2003
Rookie Year: 1999
Location: Boston, MA
Posts: 967
MikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond repute
Send a message via AIM to MikeDubreuil
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.
__________________
"FIRST is like bling bling for the brain." - Woodie Flowers
  #9   Spotlight this post!  
Unread 14-10-2004, 15:59
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
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.
__________________

  #10   Spotlight this post!  
Unread 14-10-2004, 17:30
CmptrGk's Avatar
CmptrGk CmptrGk is offline
Programming Mentor
AKA: Dillon
FRC #0716 (Who'sCTEKS)
Team Role: College Student
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Kent,CT
Posts: 212
CmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud of
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)
__________________
#14


"Great autonomous mode Dillon."," It hasn't been written yet"
after an Archimedes match


  #11   Spotlight this post!  
Unread 14-10-2004, 18:17
rbayer's Avatar Unsung FIRST Hero
rbayer rbayer is offline
Blood, Sweat, and Code
no team (Teamless Orphan)
 
Join Date: Mar 2002
Rookie Year: 2001
Location: Minnetonka, MN
Posts: 1,087
rbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of light
Send a message via AIM to rbayer
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
__________________
New C-based RoboEmu2 (code simulator) available at: http://www.robbayer.com/software.php
  #12   Spotlight this post!  
Unread 14-10-2004, 19:19
phrontist's Avatar
phrontist phrontist is offline
Proto-Engineer
AKA: Bjorn Westergard
FRC #1418 (Vae Victus)
Team Role: College Student
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Falls Church, VA
Posts: 828
phrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond repute
Send a message via AIM to phrontist
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.
__________________

University of Kentucky - Radio Free Lexington

"I would rather have a really big success or a really spectacular crash and failure then live out the warm eventual death of mediocrity" - Dean Kamen
  #13   Spotlight this post!  
Unread 14-10-2004, 19:42
CmptrGk's Avatar
CmptrGk CmptrGk is offline
Programming Mentor
AKA: Dillon
FRC #0716 (Who'sCTEKS)
Team Role: College Student
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Kent,CT
Posts: 212
CmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud of
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
__________________
#14


"Great autonomous mode Dillon."," It hasn't been written yet"
after an Archimedes match


  #14   Spotlight this post!  
Unread 14-10-2004, 20:08
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,590
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
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.
  #15   Spotlight this post!  
Unread 14-10-2004, 21:28
CmptrGk's Avatar
CmptrGk CmptrGk is offline
Programming Mentor
AKA: Dillon
FRC #0716 (Who'sCTEKS)
Team Role: College Student
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Kent,CT
Posts: 212
CmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud ofCmptrGk has much to be proud of
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.
__________________
#14


"Great autonomous mode Dillon."," It hasn't been written yet"
after an Archimedes match


Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
dash programs suneel112 Programming 12 26-01-2005 14:44
IFI's Serial Programs Doing Funny Stuff? Ian W. Programming 4 18-03-2004 21:43
Whats your honest opinion? Sean General Forum 50 17-04-2003 21:05
Dashboard programs and the char variable Ian W. Programming 13 26-06-2002 02:07
Dashboard programs archiver 2000 6 23-06-2002 23:10


All times are GMT -5. The time now is 10:55.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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