Public feed #527455

LordBoBCUP

public c v1 · immutable
Raw Edit
·published 2007-06-01 09:45 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>    int main() {    char strInputDividend[15], strInputDivider[15], strInputFloatingPoint[15]; //arrays of characters read in from the keyboard and validated against the requirements for the input.    int c; //this is the character being read of the keyboard    int loop, error, temp2; //temp, temp1, temp2 are just variables used to hold the result of functions. error is used to test if there is an error in the input. loop is used in forloops    error = 0;	void division(int x, int y, int z[], int count);//----------------------------------------------//// This loop and the following loops are the input and validation of the input gained from the user.// Basic tests are done on the data to see it is integers being passed, and also the messages are printed to the terminal.////----------------------------------------------	while (error != 1){		error = 0;		printf("Dividend?\n");		for (loop = 0; (c = getchar()) != EOF && c != '\n' && loop < 15; loop++){			if (c >= '0' && c <= '9'){				strInputDividend[loop] = c;			}			else			{				error = 1;			}		}		if (error == 1){			printf("Wrong Entry\n");			error = 0;		} else {			break;		}	}	while (error != 1){		error = 0;		printf("Divider?\n");		for (loop = 0; (c = getchar()) != EOF && c != '\n' && loop < 15; loop++){			if (c >= '0' && c <= '9'){				strInputDivider[loop] = c;			}			else			{				error = 1;			}		}		if (error == 1 || strInputDivider[0] == '0'){			printf("Wrong Entry\n");			error = 0;		} else {			break;		}	}	while (error != 1){		error = 0;		printf("Number of floating digits required?\n");		for (loop = 0; (c = getchar()) != EOF && c != '\n' && loop < 2; loop++){			if (c >= '0' && c <= '9'){				strInputFloatingPoint[loop] = c;			}			else			{				error = 1;			}		}		if (error == 1){			printf("Wrong Entry\n");			error = 0;		} else {			break;		}	}	temp2 = atoi(strInputFloatingPoint);	temp2++;	int floatingPointArray[temp2];	int intInputDividend, intInputDivider;	intInputDividend = atoi(strInputDividend);	intInputDivider = atoi(strInputDivider);	division(intInputDividend, intInputDivider, floatingPointArray, temp2);	//---------------------------------	// Loop to print out the results	// from the array	//---------------------------------	for (loop = 0; loop < temp2; loop++){		if (loop == 1){			printf(".");		}		printf("%d", floatingPointArray[loop]);	}	printf("\n");	printf("%d", floatingPointArray[1]);}void division(int intInputDividend, int intInputDivider, int floatingPointArray[], int count){	int remainder = 0;	int result;													// standard variable initalisation - pretty self explanatory	int divider;	int dividend;	int loop;	int count1 = 0;	dividend = intInputDividend;	divider = intInputDivider;	count = count + 1;	while ((result = dividend - divider) != 0 && count != 0 ){	//checks to see if the remainder isn't equal to zero, and it has not reached the length of required floating points		if (result < 0){			floatingPointArray[count1] = 0;			while (count != 0){				floatingPointArray[count1] = 0;				count = count - 1;				++count1;			}			break;		}		for (loop = 1; (result >= divider); loop++){			result = result - divider;							// calculates the actual remainder from each time round the loop		}		remainder = result;		floatingPointArray[count1] = loop;						// stores the result into the array		count = count - 1;										// decrement count		++count1;												// increment second counter		if (remainder == 0){			while (count != 0){				floatingPointArray[count1] = 0;				count = count -1;				++count1;			}			break;		}		dividend = remainder * 10;								//prepare the dividend for the next time around the loop	}}