Go to Post Adam, when most of us look at your robots, we aren't thinking, "How Creative!" What we are thinking is more like, "Holy Crap!" - Richard Wallace [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
  #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:	44
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
 


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 05:44.

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