Go to Post Measure it with a micrometer, mark it with chalk, and cut it with an axe. This is not an exact science. - Jim Meyer [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
  #16   Spotlight this post!  
Unread 01-07-2007, 15:39
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

The function you are looking for is called "sprintf".

Use it just like you would use printf to print a float. The trick is that it throws it into a string, rather than at the console.

Code:
char * buffer = new char[10];
float f = 12.4;
sprintf(buffer,"%f",f);
I think... I don't have a C compiler in front of me.
  #17   Spotlight this post!  
Unread 01-07-2007, 20:48
Pat McCarthy Pat McCarthy is offline
FiM Volunteer
FRC #0067 (HOT Team)
Team Role: Alumni
 
Join Date: May 2003
Rookie Year: 2003
Location: Grand Rapids, MI
Posts: 593
Pat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond repute
Send a message via AIM to Pat McCarthy
Re: C Programming Homework Help Needed

Is there a header file you need to include?
I'm getting a 'no prototype for function' error for the sprintf function.
__________________
2003-2006: FRC Team 67 - Drafter, Driver, Student Leader
2006 Volunteering: Kettering Kickoff Ref, Team Ford FIRST Robotics Invitational Ref
2007 Volunteering: GLR Field Reset/Repair; WMR Robot Inspector and Scorekeeper; MARC Referee
2008 Volunteering: WMR Planning Committee, WMR Scorekeeper, MARC Ref, IRI Scorekeeping, WMRI Scorekeeping & DJ
2009 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2010 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2011 Volunteering: Waterford District, West Michigan District, MARC, & IRI DJ
2012 Volunteering: Waterford District, West Michigan District DJ
  #18   Spotlight this post!  
Unread 01-07-2007, 21:05
chris31 chris31 is offline
Team 2021 Captain
AKA: Chris Davidson
FRC #2021 (FA Robotics)
Team Role: Mentor
 
Join Date: Nov 2005
Rookie Year: 2006
Location: Atlanta, GA/ Fredericksburg,VA
Posts: 949
chris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond repute
Send a message via AIM to chris31
Re: C Programming Homework Help Needed

Quote:
Originally Posted by Pat McCarthy View Post
Is there a header file you need to include?
I'm getting a 'no prototype for function' error for the sprintf function.
I believe you need to include stdio.h.
  #19   Spotlight this post!  
Unread 01-07-2007, 21:25
Pat McCarthy Pat McCarthy is offline
FiM Volunteer
FRC #0067 (HOT Team)
Team Role: Alumni
 
Join Date: May 2003
Rookie Year: 2003
Location: Grand Rapids, MI
Posts: 593
Pat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond repute
Send a message via AIM to Pat McCarthy
Re: C Programming Homework Help Needed

Quote:
Originally Posted by chris31 View Post
I believe you need to include stdio.h.
That made that error go away but I still don't really understand how to use the function.
The first line:

char * buffer = new char[10];

is confusing me.
__________________
2003-2006: FRC Team 67 - Drafter, Driver, Student Leader
2006 Volunteering: Kettering Kickoff Ref, Team Ford FIRST Robotics Invitational Ref
2007 Volunteering: GLR Field Reset/Repair; WMR Robot Inspector and Scorekeeper; MARC Referee
2008 Volunteering: WMR Planning Committee, WMR Scorekeeper, MARC Ref, IRI Scorekeeping, WMRI Scorekeeping & DJ
2009 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2010 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2011 Volunteering: Waterford District, West Michigan District, MARC, & IRI DJ
2012 Volunteering: Waterford District, West Michigan District DJ
  #20   Spotlight this post!  
Unread 01-07-2007, 22:28
eugenebrooks eugenebrooks is offline
Team Role: Engineer
AKA: Dr. Brooks
no team (WRRF)
 
Join Date: Jan 2004
Rookie Year: 2001
Location: Livermore, CA
Posts: 601
eugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond repute
Re: C Programming Homework Help Needed

Quote:
Originally Posted by Pat McCarthy View Post
That made that error go away but I still don't really understand how to use the function.
The first line:

char * buffer = new char[10];

is confusing me.
The use of the "new" operator is C++ speak, not C speak.

char buffer[10]; /* bigger than 10 is a good idea */

If a static or automatic array (depending on the location
of the declaration) is good enough, or

char *buffer;

buffer = malloc(10); /* again, bigger than 10 is a good idea */

to allocate the storage using the heap allocator in C.

Eugene
  #21   Spotlight this post!  
Unread 01-07-2007, 22:40
Pat McCarthy Pat McCarthy is offline
FiM Volunteer
FRC #0067 (HOT Team)
Team Role: Alumni
 
Join Date: May 2003
Rookie Year: 2003
Location: Grand Rapids, MI
Posts: 593
Pat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond repute
Send a message via AIM to Pat McCarthy
Re: C Programming Homework Help Needed

I am using this code:
Code:
#include <stdio.h>

int main(void)
{
	char *buffer;

	buffer = malloc(10);
	float f = 12.4;
	sprintf(buffer,"%f",f);

	return 0;
}
Attached is the compile errors I am getting.
Attached Thumbnails
Click image for larger version

Name:	sprintf.jpg
Views:	78
Size:	51.4 KB
ID:	5611  
__________________
2003-2006: FRC Team 67 - Drafter, Driver, Student Leader
2006 Volunteering: Kettering Kickoff Ref, Team Ford FIRST Robotics Invitational Ref
2007 Volunteering: GLR Field Reset/Repair; WMR Robot Inspector and Scorekeeper; MARC Referee
2008 Volunteering: WMR Planning Committee, WMR Scorekeeper, MARC Ref, IRI Scorekeeping, WMRI Scorekeeping & DJ
2009 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2010 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2011 Volunteering: Waterford District, West Michigan District, MARC, & IRI DJ
2012 Volunteering: Waterford District, West Michigan District DJ
  #22   Spotlight this post!  
Unread 01-07-2007, 22:46
eugenebrooks eugenebrooks is offline
Team Role: Engineer
AKA: Dr. Brooks
no team (WRRF)
 
Join Date: Jan 2004
Rookie Year: 2001
Location: Livermore, CA
Posts: 601
eugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond repute
Re: C Programming Homework Help Needed

Quote:
Originally Posted by Pat McCarthy View Post
I am using this code:
Code:
#include <stdio.h>

int main(void)
{
	char *buffer;

	buffer = malloc(10);
	float f = 12.4;
	sprintf(buffer,"%f",f);

	return 0;
}
Attached is the compile errors I am getting.
malloc() is likely declared in stdlib.h
  #23   Spotlight this post!  
Unread 01-07-2007, 23:01
Pat McCarthy Pat McCarthy is offline
FiM Volunteer
FRC #0067 (HOT Team)
Team Role: Alumni
 
Join Date: May 2003
Rookie Year: 2003
Location: Grand Rapids, MI
Posts: 593
Pat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond repute
Send a message via AIM to Pat McCarthy
Re: C Programming Homework Help Needed

Quote:
Originally Posted by eugenebrooks View Post
malloc() is likely declared in stdlib.h
It was. Thanks.

Now how can I use the char value?

I tried:
printf("%c", buffer);

But that printed out an an arrow pointing up.
__________________
2003-2006: FRC Team 67 - Drafter, Driver, Student Leader
2006 Volunteering: Kettering Kickoff Ref, Team Ford FIRST Robotics Invitational Ref
2007 Volunteering: GLR Field Reset/Repair; WMR Robot Inspector and Scorekeeper; MARC Referee
2008 Volunteering: WMR Planning Committee, WMR Scorekeeper, MARC Ref, IRI Scorekeeping, WMRI Scorekeeping & DJ
2009 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2010 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2011 Volunteering: Waterford District, West Michigan District, MARC, & IRI DJ
2012 Volunteering: Waterford District, West Michigan District DJ
  #24   Spotlight this post!  
Unread 01-07-2007, 23:11
eugenebrooks eugenebrooks is offline
Team Role: Engineer
AKA: Dr. Brooks
no team (WRRF)
 
Join Date: Jan 2004
Rookie Year: 2001
Location: Livermore, CA
Posts: 601
eugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond reputeeugenebrooks has a reputation beyond repute
Re: C Programming Homework Help Needed

Quote:
Originally Posted by Pat McCarthy View Post
It was. Thanks.

Now how can I use the char value?

I tried:
printf("%c", buffer);

But that printed out an an arrow pointing up.
The result of sprintf is a null, '\0', terminated string.

printf("%s", buffer);


printf("%c", buffer[0]);
would print the first char

Eugene
  #25   Spotlight this post!  
Unread 01-07-2007, 23:14
Pat McCarthy Pat McCarthy is offline
FiM Volunteer
FRC #0067 (HOT Team)
Team Role: Alumni
 
Join Date: May 2003
Rookie Year: 2003
Location: Grand Rapids, MI
Posts: 593
Pat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond repute
Send a message via AIM to Pat McCarthy
Re: C Programming Homework Help Needed

Quote:
Originally Posted by eugenebrooks View Post
The result of sprintf is a null, '\0', terminated string.

printf("%s", buffer);


printf("%c", buffer[0]);
would print the first char

Eugene
Thanks! I think that should do it for this week!
__________________
2003-2006: FRC Team 67 - Drafter, Driver, Student Leader
2006 Volunteering: Kettering Kickoff Ref, Team Ford FIRST Robotics Invitational Ref
2007 Volunteering: GLR Field Reset/Repair; WMR Robot Inspector and Scorekeeper; MARC Referee
2008 Volunteering: WMR Planning Committee, WMR Scorekeeper, MARC Ref, IRI Scorekeeping, WMRI Scorekeeping & DJ
2009 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2010 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2011 Volunteering: Waterford District, West Michigan District, MARC, & IRI DJ
2012 Volunteering: Waterford District, West Michigan District DJ
  #26   Spotlight this post!  
Unread 03-07-2007, 13:02
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: C Programming Homework Help Needed

Quote:
Originally Posted by Pat McCarthy View Post
It was. Thanks.

Now how can I use the char value?

I tried:
printf("%c", buffer);

But that printed out an an arrow pointing up.
If you just want to print the value of the float, skip the sprintf() call and go straight to printf():
Code:
float f = 12.4;
printf("%f",f);
The *printf() family of functions (sprintf(), fprintf(), printf(), etc.) all work similarly. Becoming familiar with them will help.

If you ever have a simple question about a function, Google is your friend. (Just put in the function name or "C functionName".) Even Wikipedia has an article about most standard functions.
  #27   Spotlight this post!  
Unread 02-09-2007, 19:41
Pat McCarthy Pat McCarthy is offline
FiM Volunteer
FRC #0067 (HOT Team)
Team Role: Alumni
 
Join Date: May 2003
Rookie Year: 2003
Location: Grand Rapids, MI
Posts: 593
Pat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond repute
Send a message via AIM to Pat McCarthy
Re: C Programming Homework Help Needed

This code is supposed to convert a base 10 number, 123, into binary and display the value. Somewhere in the conversion, the division loop stops before it should, for a reason that I cannot identify.
Output with some diagnostic print statements attached.
Code:
/* Patrick McCarthy */

#include <stdio.h>
#include <math.h>

void base_convert(int base);
void convert_to_binary(int base, int number);
void divide(int D, int N, int *pq, int *pr);

int main(void)
{
	int base;
	int base_error;

	/* Gets the base from user and validates the value */
	do{
		base_error = 0;

		/* Collects base value from user */
		printf("Please enter an integer between 2 and 10 =>");
		fflush(stdin);
		scanf("%d",&base);

		/* Validate entry */
		switch (base)
		{
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
			case 10:
				break;
			default:
				base_error = 1;
				printf("Invalid entry\n");
		}
	}
	while(base_error);

	/* Finds base N value of NUMBER */
//	base_convert(base);

	/* Converts number to Binary */
	convert_to_binary(base, 123);

	return 0;
}

void base_convert(int base)
{
	int i;
	int new[3];
	int NUMBER[3];
	int answer;

	//The base 10 number to be converted
	NUMBER[0] = 3;
	NUMBER[1] = 2;
	NUMBER[2] = 1;

	for(i = 0; i < 3; i++)
	{
		new[i] = ( NUMBER[i] * pow(base,i) );

		// Diagnostic
//		printf("i = %d; NUMBER[i] = %d; new[i] = %d\n", i, NUMBER[i], new[i]);
	};

	answer = new[0] + new[1] + new[2];

	printf("%d%d%d(Base 10) = %d(Base %d)", NUMBER[2],NUMBER[1],NUMBER[0],answer, base);

	return;
}

void convert_to_binary(int base, int number)
{
	int j,h;
	int q[16];
	int r[16];

	q[0] = number;

	for(j = 1; q[j] >= 0; j++)
	{
		printf("j=%d\n",j);
		divide(base, q[j-1], &q[j], &r[j]);
	}

	for(h = j; h <= 0; h--)
	{
		printf("%d", r[h]);
	}
}

void divide(int D, int N, int *pq, int *pr)
{
	int q,r;

	q = N / D;
	r = N % D;
	
	printf("Quotient: %d\nRemainder: %d\n\n", q,r);

	*pq = q;
	*pr = r;
}
Attached Thumbnails
Click image for larger version

Name:	output.jpg
Views:	43
Size:	23.5 KB
ID:	5682  
__________________
2003-2006: FRC Team 67 - Drafter, Driver, Student Leader
2006 Volunteering: Kettering Kickoff Ref, Team Ford FIRST Robotics Invitational Ref
2007 Volunteering: GLR Field Reset/Repair; WMR Robot Inspector and Scorekeeper; MARC Referee
2008 Volunteering: WMR Planning Committee, WMR Scorekeeper, MARC Ref, IRI Scorekeeping, WMRI Scorekeeping & DJ
2009 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2010 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2011 Volunteering: Waterford District, West Michigan District, MARC, & IRI DJ
2012 Volunteering: Waterford District, West Michigan District DJ
  #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
  #29   Spotlight this post!  
Unread 02-09-2007, 20:43
Pat McCarthy Pat McCarthy is offline
FiM Volunteer
FRC #0067 (HOT Team)
Team Role: Alumni
 
Join Date: May 2003
Rookie Year: 2003
Location: Grand Rapids, MI
Posts: 593
Pat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond reputePat McCarthy has a reputation beyond repute
Send a message via AIM to Pat McCarthy
Re: C Programming Homework Help Needed

I'm having a little bit of trouble now. See output.
Code:
/* Patrick McCarthy */

#include <stdio.h>
#include <math.h>

void base_convert(int base);
void convert_to_binary(int base, int number);
void divide(int D, int N, int &q, int &r);

int main(void)
{
	int base;
	int base_error;

	/* Gets the base from user and validates the value */
	do{
		base_error = 0;

		/* Collects base value from user */
		printf("Please enter the base of the number (between 2 and 10) =>");
		fflush(stdin);
		scanf("%d",&base);

		/* Validate entry */
		if(base <2 || base > 10)
		{
			base_error = 1;
			printf("Invalid entry\n");
		}
		
	}
	while(base_error);

	/* Finds base N value of NUMBER */
//	base_convert(base);

	/* Converts number to Binary */
	convert_to_binary(base, 123);

	return 0;
}

void base_convert(int base)
{
	int i;
	int new[3];
	int NUMBER[3];
	int answer;

	//The base 10 number to be converted
	NUMBER[0] = 3;
	NUMBER[1] = 2;
	NUMBER[2] = 1;

	for(i = 0; i < 3; i++)
	{
		new[i] = ( NUMBER[i] * pow(base,i) );

		// Diagnostic
//		printf("i = %d; NUMBER[i] = %d; new[i] = %d\n", i, NUMBER[i], new[i]);
	};

	answer = new[0] + new[1] + new[2];

	printf("%d%d%d(Base 10) = %d(Base %d)", NUMBER[2],NUMBER[1],NUMBER[0],answer, base);

	return;
}

void convert_to_binary(int base, int number)
{
	int j,h;
	int q[16];
	int r[16];

	q[0] = number;

	for(j = 1; q[j]>0; j++)
	{
		printf("j=%d\n",j);
		divide(base, q[j-1], &q[j], &r[j]);
	}

	for(h = j; h <= 0; h--)
	{
		printf("%d", r[h]);
	}
}

void divide(int D, int N, int &q, int &r)
{
	int q,r;

	/* q is the quotient and r is the remainder */

	q = N / D;
	r = N % D;
	
	printf("%d/%d :: Quotient: %d\nRemainder: %d\n\n", N,D,q,r);
}
Attached Thumbnails
Click image for larger version

Name:	output1.jpg
Views:	48
Size:	30.6 KB
ID:	5683  
__________________
2003-2006: FRC Team 67 - Drafter, Driver, Student Leader
2006 Volunteering: Kettering Kickoff Ref, Team Ford FIRST Robotics Invitational Ref
2007 Volunteering: GLR Field Reset/Repair; WMR Robot Inspector and Scorekeeper; MARC Referee
2008 Volunteering: WMR Planning Committee, WMR Scorekeeper, MARC Ref, IRI Scorekeeping, WMRI Scorekeeping & DJ
2009 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2010 Volunteering: Kettering District, MARC, IRI, and WMRI Scorekeeper/Field Power Controller
2011 Volunteering: Waterford District, West Michigan District, MARC, & IRI DJ
2012 Volunteering: Waterford District, West Michigan District DJ
  #30   Spotlight this post!  
Unread 02-09-2007, 20:54
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

You only half integrated my changes to divide.

q and r are coming in as references. You do not need to declare them again.

Also,
divide(base, q[j-1], &q[j], &r[j]);
should read as
divide(base, q[j-1], q[j], r[j]);

I'm going to be offline for a while. Good luck!
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
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 13:21.

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