//========================================================================= #define PROGRAMMER "SOLUTIONS, NoFrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2003) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "HW #4A" #define REVISION (0) #define TITLE "Digitwise Math" #define SUBTITLE "Problem 6.12" #define EMAIL "wbahn@eas.uccs.edu" #define FILENAME "04a0soln.c" //========================================================================= // PROBLEM: // // Write a program to take two five digit integers that are stored as // individual digits in an integer array and add them together. The // values can be positive or negative, with a negative value being // indicated by having an additional digit be a one (zero if positive). // SOLUTION: // // The algorithm development and pseudocode is in file 04a0soln.txt // The differences between the pseudocode in that file and the code are: // // 1) The code has a PrintInstructions() function added in main() // 2) All functions have been generalized to n-digit integers // 3) Some of the loops lacked the increment/decrement housekeeping // //== INCLUDE FILES ======================================================== #include // printf(), scanf() //== MACRO DEFINITIONS ==================================================== #define DIGITS (5) //== TOP LEVEL SUPPORE FUNCTION PROTOTYPES ================================ void PrintHeader(void); void PrintInstructions(void); void GetNDigitSignedInt(int a[], int n); void SumNDigitSignedInt(int sum[], int a[], int b[], int n); void DisplayNDigitSignedInt(int a[], int n); //== MAIN FUNCTION ======================================================== int main(void) { int a[DIGITS+1], b[DIGITS+1], sum[DIGITS+2]; PrintHeader(); printf("\n"); // Print a blank line PrintInstructions(); // 1) TASK: Get the first value from the user. printf("Enter 1st number: "); GetNDigitSignedInt(a, 5); // 2) TASK: Get the second value from the user. printf("Enter 2nd number: "); GetNDigitSignedInt(b, 5); // 3) TASK: Add the two values together. SumNDigitSignedInt(sum, a, b, 5); // 4) TASK: Output the results. printf("\n"); printf("RESULTS: ( "); DisplayNDigitSignedInt(a, 5); printf(")\n"); printf(" + ( "); DisplayNDigitSignedInt(b, 5); printf(")\n"); printf(" -----------\n"); printf(" = ("); DisplayNDigitSignedInt(sum, 6); printf(")\n"); return(0); } //== SUPPORT FUNCTIONS ==================================================== void PrintHeader(void) { printf("========================================" "=======================================\n"); printf("Course....... %s-%i (%s %i)\n", COURSE, SECTION, TERM, YEAR); printf("Programmer... %s (%s)\n", PROGRAMMER, PROG_CODE); printf("Assignment... %s (Rev %i) (Source Code in %s)\n", ASSIGNMENT, REVISION, FILENAME); printf("Description.. %s\n", TITLE); printf(" %s\n", SUBTITLE); printf("========================================" "=======================================\n"); return; } //== LOWER LEVEL FUNCTION PROTOTYPES ====================================== void SumNDigitUnsignedInt(int sum[], int a[], int b[], int n); void SubNDigitUnsignedInt(int sub[], int a[], int b[], int n); int CompareNDigitUnsignedInt(int a[], int b[], int n); //== TOP LEVEL SUPPORT FUNCTION DEFINITIONS =============================== // FUNCTION: PrintInstructions() // // This function provides the user with the information needed to enter // the data properly. // void PrintInstructions(void) { printf("\n"); printf("Enter the digits for two 5 digit signed integers.\n"); printf("The sign is indicated by a 1 (negative) or 0 (positive)\n"); printf("prior to the first digit. There must be a space between\n"); printf("each digit.\n"); printf("\n"); printf("Examples: -63544 would be entered as 1 6 3 5 4 4\n"); printf(" and: 8463 would be entered as 0 0 8 4 6 3\n"); printf("\n"); return; } //------------------------------------------------------------------------ // FUNCTION: GetNDigitSignedInt(int d[], int n) // // The user is required and expected to enter all of the digits, including // the sign digit. So there is no need to initilize the elements of the // array. We will assume that the user entered all values as expected. // void GetNDigitSignedInt(int d[], int n) { int i; for(i = 0; i < n+1; i++) { scanf("%i", &d[i]); } return; } //------------------------------------------------------------------------ // FUNCTION: SumNDigitSignedInt(int sum[], int a[], int b[], int n) // // The above work would allow us to add two unsigned 5-digit arrays // without any problems (i.e., do Problem 6.11). To be able to add // two signed five digit numbers, we can use one of several approaches. // The most basic one is to do it the same way you would do it by hand: // void SumNDigitSignedInt(int sum[], int a[], int b[], int n) { if( 1 == (a[0]+b[0]) ) // one positive and the other negative { if (1 == CompareNDigitUnsignedInt(&a[1], &b[1], n)) { SubNDigitUnsignedInt(&sum[1], &a[1], &b[1], n); sum[0] = a[0]; } else { SubNDigitUnsignedInt(&sum[1], &b[1], &a[1], n); sum[0] = b[0]; } } else // both positive or both negative { SumNDigitUnsignedInt(&sum[1], &a[1], &b[1], n); sum[0] = a[0]; } return; } //------------------------------------------------------------------------ // FUNCTION: DisplayNDigitSignedInt(int d[], int n) // // Here we simply test the first digit to see what sign to print and then // print out the elements in turn. // void DisplayNDigitSignedInt(int d[], int n) { int i; i = 0; if(1 == d[0]) printf("-"); else printf("+"); do { i++; printf("%1i", d[i]); } while (i < n); return; } //== LOWER LEVEL SUPPORT FUNCTION DEFINITIONS ============================= // // FUNCTION: SumNDigitUnsignedInt(int sum[], int a[], int b[], int n) // // To keep things incremental, we'll first write a function that // can add two five digit unsigned arrays. // // The key is to note that we can simply do it the same way we would do // it with pencil and paper - start with the rightmost digits, add them // together, store the units digit as the result for that place and // carry the one if needed, which merely means to add it to the two // digits in the next column to the left. We can use the sum[] array // to hold the carry digits. // void SumNDigitUnsignedInt(int sum[], int a[], int b[], int n) { int carry; carry = 0; while(n > 0) { sum[n] = carry + a[n-1] + b[n-1]; if( sum[n] > 9) { sum[n] -=10; carry = 1; } else { carry = 0; } n--; } sum[n] = carry; return; } //------------------------------------------------------------------------ void SubNDigitUnsignedInt(int sub[], int a[], int b[], int n) { int borrow; borrow = 0; while(n > 0) { sub[n] = (a[n-1] - borrow) - b[n-1]; if( sub[n] < 0 ) { sub[n] +=10; borrow = 1; } else { borrow = 0; } n--; } sub[n] = borrow; return; } //------------------------------------------------------------------------ // // FUNCTION: CompareNDigitUnsignedInt(int a[], int b[], int n); // // a[] and b[] are two unsigned n-digit integers // Return value: -1 if a[] < b[] // 0 if equal // +1 if a[] > b[] // int CompareNDigitUnsignedInt(int a[], int b[], int n) { int i; i = 0; // Find first digit that doesn't match while( (i < n) && (a[i] == b[i]) ) i++; // Then return result based on that digit if( i == n ) // exact equality (i.e., no digits failed to match) return(0); if( a[i] < b[i] ) return(-1); else return(1); }