View Single Post
  #28   Spotlight this post!  
Unread 02-09-2007, 20:04
EricVanWyk EricVanWyk is offline
Registered User
no team
 
Join Date: Jan 2007
Rookie Year: 2000
Location: Boston
Posts: 1,597
EricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond repute
Send a message via AIM to EricVanWyk
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 !!}
I do like your use of the do while. However, I'd rather it set base_error=0.

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');
}
EDIT 2
__________________________
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