Computer Science Canada Resistor Colour Code Calculator |
Author: | Srlancelot39 [ Wed Feb 05, 2014 6:39 pm ] | ||
Post subject: | Resistor Colour Code Calculator | ||
I'm so excited about this, being my first personal C project, that I wanted to put an exclamation mark in the title ![]() Anyone who's just starting Electrical Engineering, or uses resistors a lot could really appreciate this! ![]() Anyhow, here it is! (It's not completely error-proofed, meaning you CAN crash it, but it works perfectly)
|
Author: | Raknarg [ Wed Feb 05, 2014 8:37 pm ] |
Post subject: | RE:Resistor Colour Code Calculator |
So what does this do, exactly? I have no way to compile and run c code at the moment. |
Author: | Insectoid [ Wed Feb 05, 2014 8:58 pm ] |
Post subject: | RE:Resistor Colour Code Calculator |
Looks like it takes the colours of the bands on a resister as input and returns the resistance and tolerance of that resistor. |
Author: | Srlancelot39 [ Thu Feb 06, 2014 12:11 am ] |
Post subject: | RE:Resistor Colour Code Calculator |
Precisely what Insectoid said ![]() Nice job on that console connect 4 by the way! Haven't tried it, but I examined the code and it looks pretty damn complex lol |
Author: | Insectoid [ Thu Feb 06, 2014 1:06 pm ] | ||
Post subject: | RE:Resistor Colour Code Calculator | ||
This can be improved dramatically. Let's start with your sigfigs function. You have a list of strings that correspond to the numbers 0 through 9. An array and a loop can shorten that ugly function to five or six lines. Same with your mult function. I notice you have a separate variable for each band. You only need one, because once you've processed a band, you do not need it any more, so you can discard the value and re-use the variable. While processing band 1, you don't need to reference band 2, and when processing band 2, you don't need band 1 anymore. All you need is a counter to total up the values. For example:
|
Author: | Srlancelot39 [ Wed Feb 12, 2014 8:15 am ] |
Post subject: | RE:Resistor Colour Code Calculator |
Thanks for the improvements! Yeah I never went back afterwards to make the code more efficient, I sort of just wanted to see it working ![]() Very smart idea using one variable for all bands, but now I'm wondering, how would you display the information for each band with only the one variable? Would you need to display as you go, and also do the math as you go as well? |
Author: | Insectoid [ Wed Feb 12, 2014 5:05 pm ] |
Post subject: | RE:Resistor Colour Code Calculator |
You can print the digit values of the bands as you go but without knowing the multiplier band you can't print the actual value. However, with an array of bands you can still use the looping algorithm and print the data after your calculations. This has the added benefit of eliminating one of your giant if blocks and replacing it with a one-line loop. |