Go to Post Gosh, I wish that we would've named IndianaFIRST after a candy. - Andy Baker [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

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #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
 


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
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


All times are GMT -5. The time now is 18:09.

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