Fryguy8
09-20-2004, 10:10 PM
Yes this is a homework assignment. Does anyone see any obvious mistakes or inefficiencies? any improvements?
This is my first C program, so be gentle ;)
/**************************************************
Bryan Alves
Assignment 1 - Manual Long Division
Takes 4 values:
-dividend and divisor: The division to do
-start and end: Will print the numbers from the division in this precision
**************************************************/
#include <stdio.h>
int main()
{
/* Declare the 4 main variables */
unsigned long int dividend, divisor, start, end;
/* Declare variables used for division */
unsigned long int quotient = 0, remainder = 0;
unsigned short int intLength = 0;
unsigned long int i;
/* Read Values */
printf("Dividend: ");
scanf("%d", ÷nd);
printf("Divisor: ");
scanf("%d", &divisor);
printf("Start: ");
scanf("%d", &start);
printf("End: ");
scanf("%d", &end);
/* Verify validity of entered parameters */
if (start > end)
{
printf("Error: Start must be before end\n");
return 1;
}
if (start == 0)
{
printf("Error: Cannot start before the first digit\n");
return 1;
}
/*
The largest number of digits you can have after doing the first step
of division is something like 4 billion divided by 3, which yields
10 digits, plus one for the decimal, plus one for the null terminator.
The longest length you therefore need is 12.
*/
/* Wasting a couple bytes of memory is probably more efficient than
manually doing the first divison using a loop. */
char firstDivide[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
char *p = firstDivide;
/* Calculate the quotient's integer part. */
quotient = dividend / divisor;
remainder = dividend % divisor;
/* Fill firstDivide and return the length actually used. */
intLength = sprintf(firstDivide, "%d", quotient);
/* Initialize the loop variable to begin after the decimal point. */
i = intLength + 2;
if (start <= intLength + 1) /* Start printing from within the first divison. */
{
/* Walk the array to the start position. */
p += start - 1;
if (end < intLength) /* We end before the decimal point. */
firstDivide[end] = '\0';
else if(end > intLength) /* We end after the decimal point. */
firstDivide[intLength] = '.';
printf(p);
}
else /* Skip numbers that are before the start of printing */
{
for (; i < start; i++)
{
/* No need to track quotient; it just gets thrown out. */
dividend = remainder * 10;
remainder = dividend % divisor;
}
}
/* These values we print, so do the division and print the numbers one by one. */
for (; i <= end; i++)
{
dividend = remainder * 10;
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("%d", quotient);
}
return 0;
}
This is my first C program, so be gentle ;)
/**************************************************
Bryan Alves
Assignment 1 - Manual Long Division
Takes 4 values:
-dividend and divisor: The division to do
-start and end: Will print the numbers from the division in this precision
**************************************************/
#include <stdio.h>
int main()
{
/* Declare the 4 main variables */
unsigned long int dividend, divisor, start, end;
/* Declare variables used for division */
unsigned long int quotient = 0, remainder = 0;
unsigned short int intLength = 0;
unsigned long int i;
/* Read Values */
printf("Dividend: ");
scanf("%d", ÷nd);
printf("Divisor: ");
scanf("%d", &divisor);
printf("Start: ");
scanf("%d", &start);
printf("End: ");
scanf("%d", &end);
/* Verify validity of entered parameters */
if (start > end)
{
printf("Error: Start must be before end\n");
return 1;
}
if (start == 0)
{
printf("Error: Cannot start before the first digit\n");
return 1;
}
/*
The largest number of digits you can have after doing the first step
of division is something like 4 billion divided by 3, which yields
10 digits, plus one for the decimal, plus one for the null terminator.
The longest length you therefore need is 12.
*/
/* Wasting a couple bytes of memory is probably more efficient than
manually doing the first divison using a loop. */
char firstDivide[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
char *p = firstDivide;
/* Calculate the quotient's integer part. */
quotient = dividend / divisor;
remainder = dividend % divisor;
/* Fill firstDivide and return the length actually used. */
intLength = sprintf(firstDivide, "%d", quotient);
/* Initialize the loop variable to begin after the decimal point. */
i = intLength + 2;
if (start <= intLength + 1) /* Start printing from within the first divison. */
{
/* Walk the array to the start position. */
p += start - 1;
if (end < intLength) /* We end before the decimal point. */
firstDivide[end] = '\0';
else if(end > intLength) /* We end after the decimal point. */
firstDivide[intLength] = '.';
printf(p);
}
else /* Skip numbers that are before the start of printing */
{
for (; i < start; i++)
{
/* No need to track quotient; it just gets thrown out. */
dividend = remainder * 10;
remainder = dividend % divisor;
}
}
/* These values we print, so do the division and print the numbers one by one. */
for (; i <= end; i++)
{
dividend = remainder * 10;
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("%d", quotient);
}
return 0;
}