|
|
|
![]() |
|
|||||||
|
||||||||
|
|
Thread Tools | Rate Thread | Display Modes |
|
#28
|
|||
|
|||
|
Re: C Programming Homework Help Needed
A few notes to make your life easier:
The switch(base) structure is confusing. I suggest replacing it with something that more clearly states what you want. I'd recommend something like Code:
if(base <2 || base > 10) { Error !!}
Your printf statement doesn't really tell the user what you are going to do with the number. Put a more informative request in there. "Please enter the base of the number (between 2 and 10)" Please comment what q and r are. They are probably quotient and remainder, but explicit = maintainable. Okay, enough with the picky details. Pain in the patookus, but trust me, you'll thank me later. To clarify divide, I've used pass by reference, rather than by pointer. Also, I've added some more debuggging! Code:
void divide(int D, int N, int &q, int &r)
{
q = N / D;
r = N % D;
printf("%d/%d :: Quotient: %d\nRemainder: %d\n\n", N,D,q,r);
}
_____________________________________ Thought of a few more helper functions. Code:
int convertFromBaseToBinary(int base, char * number_string)
{
int num=0; //number converted from string
for(;number_string;number_string++) //Scroll through the letters, starting with the most significant digit
num = num*base+charToNumber(*number_string)
//num now holds the correct number
return num;
}
int charToNumber(char a){
return int(a-'0');
}
char numberToChar(int i){
return char(i+'0');
}
__________________________ Just noticed another bug. for(j = 1; q[j] >= 0; j++) will never terminate. I think you want q[j]>0 or maybe even q[j-1]>0 Edit 3 _________________________ And for more giggles, here is some Python Code:
def convertToBase(num, base): s=[] while num is not 0: s=[num%base]+s num/=base return s Last edited by EricVanWyk : 02-09-2007 at 20:27. Reason: More infos |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| programming help needed | krunal | Programming | 3 | 29-01-2007 00:18 |
| Help on a Homework Assignment. | Pavan Dave | General Forum | 1 | 13-09-2006 14:20 |
| HELP-URGENT PROGRAMMING HELP NEEDED | Rohith Surampudi | Lego Mindstorm Discussion | 1 | 24-03-2006 23:05 |
| URgent Programming Help needed | rcubes85 | Programming | 4 | 15-02-2005 23:21 |