This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
user:smalik2:portfolio:cprogproject3 [2012/03/18 17:01] – external edit 127.0.0.1 | user:smalik2:portfolio:cprogproject3 [2012/04/26 05:11] (current) – [Code] smalik2 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | A project for C/C++ Programming by Saad Malik during the Spring 2012. | ||
+ | |||
+ | This project was begun on 4/22 and is anticipated to take a couple hours to complete (I already have most of it done because I did project 2 correctly). Project was completed on April 26, 2012. | ||
+ | |||
+ | Only reason I didn't finish in a few hours was I ran into some VERY interesting errors. | ||
+ | =====Objectives===== | ||
+ | State the purpose of this project. What is the point of this project? What do we hope to accomplish by undertaking it? | ||
+ | |||
+ | A working program that will perform multiple math operations on numbers without bounds. | ||
+ | So that we can develop an appreciation for the inbuilt math function in C - and to understand their bounds. | ||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * successful completion of project #1 and solid understanding of pertinent topics | ||
+ | * successful implementation of addition and subtraction from project #2 in working functions | ||
+ | * familiarity with memory allocation via **malloc(3)** | ||
+ | * familiarity with memory, accessing data via pointer dereferencing, | ||
+ | * familiarity with looking up C function parameters/ | ||
+ | * familiarity with C++ classes | ||
+ | * familiarity with functions, their parameters and return types | ||
+ | * familiarity with multi-file programs, how to make and build them | ||
+ | |||
+ | =====Background===== | ||
+ | The purpose is to get a math program that will perform math operations on numbers without limiting the size of the numbers. | ||
+ | |||
+ | You'll need to use arrays and much more complex algorithms to perform the math, since we are trying to make the numbers boundless in this program. | ||
+ | |||
+ | |||
+ | =====Scope===== | ||
+ | Project #2 was to be an awesome exploration of array manipulation and functions, built atop a comfortable yet easy foundation of simple mathematics. | ||
+ | |||
+ | As it turns out, procrastination and refusal to work out ideas on paper are killer obstacles. | ||
+ | |||
+ | This project will therefore ebrace and extend upon project #2, where you will finish implementing code to support the storage and manipulation of numbers outside of the established data types. And once you have that, we'll do some additional modifications to reflect concepts covered is class. | ||
+ | |||
+ | So, for this project I'd like for you to: | ||
+ | |||
+ | * have working addition, subtraction, | ||
+ | * definitely get multiplication and division working | ||
+ | * also implement a **modulus** and **exponent** function | ||
+ | * verify successful operation with numbers of length 8, 16, 24, and 32 | ||
+ | * split your code up into **multiple files** (have at least one header file, a main.c, and two additional C files with various functions in them) | ||
+ | * have these **multiple files** successfully compile and operate just as your monolithic code would | ||
+ | * ALSO (aka "in addition to" your C solution, I'd like you to also implement a **class-based solution** in monolithic and multiple files in **C++**). So you will have a pure C implementation AND a class-based C++ implementation. | ||
+ | |||
+ | Some helpful hints: | ||
+ | |||
+ | * WORK IT OUT ON PAPER. | ||
+ | * WORK IT OUT ON PAPER. | ||
+ | * WORK IT OUT ON PAPER. | ||
+ | * WORK IT OUT ON PAPER. | ||
+ | * get the C version working before you even start on the C++ (it'll make more sense) | ||
+ | * when you get to coding, be sure to use the debugger to see what is actually happening | ||
+ | * chars are just numbers | ||
+ | * strings can make things complicated | ||
+ | * just focus on chars being numbers | ||
+ | * if confused, WORK IT OUT ON PAPER. | ||
+ | |||
+ | If you don't understand what "WORK IT OUT ON PAPER" means, it means to go through several STEP-BY-STEP iterations BY HAND of some of the very math operations you'd expect your program to ultimately perform. | ||
+ | |||
+ | Try it out for yourself- pick two arbitrary 8-digit numbers, and ADD them together. BY HAND. Note how you calculate the individual sums and carries. Watch how the carries propagate from right to left. | ||
+ | |||
+ | Do the some for subtraction, | ||
+ | |||
+ | Can you define multiplication in terms of addition? | ||
+ | |||
+ | Can you define division in terms of subtraction? | ||
+ | |||
+ | =====Code===== | ||
+ | |||
+ | The C code: | ||
+ | |||
+ | The monolithic C code file is located at: | ||
+ | vim / | ||
+ | |||
+ | The seperated C code file is located at: | ||
+ | cd / | ||
+ | _____________________________________________________________________ | ||
+ | |||
+ | The monolithic C++ code file is at: | ||
+ | vim / | ||
+ | |||
+ | The separated C++ code: (This is almost exactly the same as the others, it just has some C++ class stuff built on top of it. The logic behind the functions is exactly the same as the others). | ||
+ | |||
+ | Functions.h | ||
+ | <code c> | ||
+ | #ifndef _FUNCTIONS_H | ||
+ | #define _FUNCTIONS_H | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | class Functions{ | ||
+ | public: | ||
+ | char* MakeArray(int); | ||
+ | char* ZeroArray(char*, | ||
+ | int CheckZero(char*, | ||
+ | char* Addition(char*, | ||
+ | char* Subtraction(char*, | ||
+ | char* Multiplication(char*, | ||
+ | char* Exponent(char*, | ||
+ | void Modolus(char*, | ||
+ | void Division(char*, | ||
+ | int Comparison(char*, | ||
+ | void PrintArray(char*, | ||
+ | void CopyArray(char*, | ||
+ | }; | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | MathFunctions.cc | ||
+ | <code c> | ||
+ | #include " | ||
+ | |||
+ | char * Functions:: | ||
+ | { | ||
+ | char count=0; | ||
+ | char carry=0; | ||
+ | |||
+ | for(count; count <= length; ++count) | ||
+ | { | ||
+ | int holder = (*(ArrayZ+count) + *(ArrayB + count))+carry; | ||
+ | if (holder > 9) | ||
+ | { | ||
+ | *(ArrayZ+count) = holder-10; | ||
+ | carry=1; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | *(ArrayZ+count) = holder; | ||
+ | carry=0; | ||
+ | } | ||
+ | } | ||
+ | return ArrayZ; | ||
+ | } | ||
+ | |||
+ | char * Functions:: | ||
+ | { | ||
+ | char count = 0; | ||
+ | |||
+ | for(count; count <= length; ++count) | ||
+ | { | ||
+ | if (*(Array+count) < *(ArrayB+count)) | ||
+ | { | ||
+ | *(Array+count+1) = *(Array+count+1) - 1; | ||
+ | *(Array+count) = *(Array+count) + 10; | ||
+ | } | ||
+ | |||
+ | *(Array+count) = *(Array+count) - *(ArrayB+count); | ||
+ | } | ||
+ | return Array; | ||
+ | } | ||
+ | |||
+ | char* Functions:: | ||
+ | { | ||
+ | int count = 0; | ||
+ | char *ArrayC; | ||
+ | ArrayC = MakeArray((2*length)+1); | ||
+ | ArrayC = ZeroArray(ArrayC, | ||
+ | |||
+ | char *ArrayD; | ||
+ | ArrayD = MakeArray((length)); | ||
+ | ArrayD = ZeroArray(ArrayD, | ||
+ | *(ArrayD+0)=1; | ||
+ | |||
+ | while(CheckZero(ArrayB, | ||
+ | { | ||
+ | ArrayC = Addition(ArrayC, | ||
+ | ArrayB = Subtraction(ArrayB, | ||
+ | } | ||
+ | return ArrayC; | ||
+ | } | ||
+ | |||
+ | char* Functions:: | ||
+ | { | ||
+ | int count = 0; | ||
+ | char* ArrayC; | ||
+ | ArrayC = MakeArray(5*(2*length)+1); | ||
+ | ArrayC = ZeroArray(ArrayC, | ||
+ | |||
+ | char* ArrayD; | ||
+ | ArrayD = MakeArray((length)); | ||
+ | ArrayD = ZeroArray(ArrayD, | ||
+ | *(ArrayD+0)=1; | ||
+ | |||
+ | ArrayB = Subtraction(ArrayB, | ||
+ | CopyArray(Array, | ||
+ | | ||
+ | char* ArrayZ; | ||
+ | ArrayZ = MakeArray(length); | ||
+ | ArrayZ = ZeroArray(ArrayZ, | ||
+ | CopyArray(Array, | ||
+ | while(CheckZero(ArrayB, | ||
+ | { | ||
+ | ArrayC = Multiplication(ArrayC, | ||
+ | ArrayB = Subtraction(ArrayB, | ||
+ | CopyArray(ArrayZ, | ||
+ | } | ||
+ | return ArrayC; | ||
+ | } | ||
+ | |||
+ | void Functions:: | ||
+ | { | ||
+ | int count = 0; | ||
+ | int answer = 0; | ||
+ | | ||
+ | for(count=length; | ||
+ | { | ||
+ | while(*(Array+count) > 0) | ||
+ | { | ||
+ | ++answer; | ||
+ | Array = Subtraction(Array, | ||
+ | } | ||
+ | } | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | |||
+ | void Functions:: | ||
+ | { | ||
+ | int count = 0; | ||
+ | int answer = 0; | ||
+ | |||
+ | while (Comparison(Array, | ||
+ | { | ||
+ | Array = Subtraction(Array, | ||
+ | } | ||
+ | PrintArray(Array, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ArrayFunctions.cc | ||
+ | <code c> | ||
+ | #include " | ||
+ | |||
+ | char * Functions:: | ||
+ | { | ||
+ | char *Array; | ||
+ | |||
+ | Array = (char*)malloc(sizeof(char)*length); | ||
+ | |||
+ | return Array; | ||
+ | } | ||
+ | |||
+ | char * Functions:: | ||
+ | { | ||
+ | for (length; length> | ||
+ | { | ||
+ | *(Array+length) = 0; | ||
+ | } | ||
+ | |||
+ | return Array; | ||
+ | } | ||
+ | |||
+ | int Functions:: | ||
+ | { | ||
+ | for (length; length> | ||
+ | { | ||
+ | if (*(Array+length) != 0) | ||
+ | { | ||
+ | return 1; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | void Functions:: | ||
+ | { | ||
+ | for (length; length> | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void Functions:: | ||
+ | { | ||
+ | for (length; length> | ||
+ | { | ||
+ | *(ArrayB+length) = *(Array+length); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | int Functions:: | ||
+ | { | ||
+ | char count; | ||
+ | for(count=length; | ||
+ | { | ||
+ | if(*(Array+count) > *(ArrayB+count)) | ||
+ | { | ||
+ | return 1; | ||
+ | } | ||
+ | if(*(Array+count) < *(ArrayB+count)) | ||
+ | { | ||
+ | return 0; | ||
+ | } | ||
+ | } | ||
+ | return 2; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Project2Monolithic.cc (name is slightly off, can't be bothered to go through and make them all perfect). | ||
+ | <code c> | ||
+ | #include " | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | Functions *UseFunction = new Functions(); | ||
+ | char *Array; | ||
+ | char *ArrayB; | ||
+ | int count; | ||
+ | int length; | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | printf(" | ||
+ | scanf(" | ||
+ | length = (length)-1; | ||
+ | |||
+ | Array = UseFunction-> | ||
+ | Array = UseFunction-> | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | fgetc(stdin); | ||
+ | for(count=length; | ||
+ | { | ||
+ | *(Array+count)=fgetc(stdin); | ||
+ | *(Array+count) = *(Array+count)-48; | ||
+ | } | ||
+ | |||
+ | UseFunction-> | ||
+ | |||
+ | ArrayB = UseFunction-> | ||
+ | ArrayB = UseFunction-> | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | fgetc(stdin); | ||
+ | for(count=length; | ||
+ | { | ||
+ | *(ArrayB+count) = fgetc(stdin); | ||
+ | *(ArrayB+count) = *(ArrayB+count) - 48; | ||
+ | } | ||
+ | |||
+ | UseFunction-> | ||
+ | |||
+ | char menu; | ||
+ | printf(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | switch(menu) | ||
+ | { | ||
+ | case 0: | ||
+ | Array = UseFunction-> | ||
+ | UseFunction-> | ||
+ | break; | ||
+ | case 1: | ||
+ | Array = UseFunction-> | ||
+ | UseFunction-> | ||
+ | break; | ||
+ | case 2: | ||
+ | { | ||
+ | length = length*2+1; | ||
+ | char* ArrayC; | ||
+ | ArrayC = UseFunction-> | ||
+ | ArrayC = UseFunction-> | ||
+ | ArrayC = UseFunction-> | ||
+ | UseFunction-> | ||
+ | break; | ||
+ | } | ||
+ | case 3: | ||
+ | UseFunction-> | ||
+ | break; | ||
+ | case 4: | ||
+ | { | ||
+ | length = 5*(2*length)+1; | ||
+ | char* ArrayC; | ||
+ | ArrayC = UseFunction-> | ||
+ | ArrayC = UseFunction-> | ||
+ | ArrayC = UseFunction-> | ||
+ | UseFunction-> | ||
+ | break; | ||
+ | } | ||
+ | case 5: | ||
+ | UseFunction-> | ||
+ | break; | ||
+ | case 6: | ||
+ | { | ||
+ | int answer; | ||
+ | answer = UseFunction-> | ||
+ | | ||
+ | if (answer == 2) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | | ||
+ | if (answer == 1) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | if (answer == 0) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | break; | ||
+ | case 7: | ||
+ | printf(" | ||
+ | break; | ||
+ | default: | ||
+ | printf(" | ||
+ | break; | ||
+ | } | ||
+ | printf(" | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | =====Execution===== | ||
+ | |||
+ | Note: All 4 variations of this program run the exact same way. My example will be showing the multifile class based C++ version (The one that, in my opinion, you need the others in order to make this one). | ||
+ | Note: I didn't use 24 or 32 digit numbers in some examples because we would be waiting massive amounts of time for the processing to finish. But I spent a great deal of time creating ' | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | lab46: | ||
+ | lab46: | ||
+ | |||
+ | Take note, you will be prompted to enter the length of the numbers you want this program to deal with. Enter the length of the longest number. | ||
+ | |||
+ | How long will your numbers be? | ||
+ | 24 | ||
+ | |||
+ | Enter the first number: 111111111111111111111111 | ||
+ | 111111111111111111111111 | ||
+ | Enter the second number: 222222222222111111111111 | ||
+ | 222222222222111111111111 | ||
+ | What would you like to do to the two numbers? | ||
+ | |||
+ | 0: Add | ||
+ | 1: Subtract | ||
+ | 2: Multiply | ||
+ | 3: Divide | ||
+ | 4: Exponent | ||
+ | 5: Modolus | ||
+ | 6: Compare | ||
+ | 7: Quit | ||
+ | 0 | ||
+ | 333333333333222222222222 | ||
+ | lab46: | ||
+ | |||
+ | Take note, you will be prompted to enter the length of the numbers you want this program to deal with. Enter the length of the longest number. | ||
+ | |||
+ | How long will your numbers be? | ||
+ | 24 | ||
+ | |||
+ | Enter the first number: 333333333333333333333333 | ||
+ | 333333333333333333333333 | ||
+ | Enter the second number: 111111111111222222223333 | ||
+ | 111111111111222222223333 | ||
+ | What would you like to do to the two numbers? | ||
+ | |||
+ | 0: Add | ||
+ | 1: Subtract | ||
+ | 2: Multiply | ||
+ | 3: Divide | ||
+ | 4: Exponent | ||
+ | 5: Modolus | ||
+ | 6: Compare | ||
+ | 7: Quit | ||
+ | 1 | ||
+ | 222222222222111111110000 | ||
+ | lab46: | ||
+ | |||
+ | Take note, you will be prompted to enter the length of the numbers you want this program to deal with. Enter the length of the longest number. | ||
+ | |||
+ | How long will your numbers be? | ||
+ | 6 | ||
+ | |||
+ | Enter the first number: 222222 | ||
+ | 222222 | ||
+ | Enter the second number: 222222 | ||
+ | 222222 | ||
+ | What would you like to do to the two numbers? | ||
+ | |||
+ | 0: Add | ||
+ | 1: Subtract | ||
+ | 2: Multiply | ||
+ | 3: Divide | ||
+ | 4: Exponent | ||
+ | 5: Modolus | ||
+ | 6: Compare | ||
+ | 7: Quit | ||
+ | 2 | ||
+ | 049382617284 | ||
+ | lab46: | ||
+ | |||
+ | Take note, you will be prompted to enter the length of the numbers you want this program to deal with. Enter the length of the longest number. | ||
+ | |||
+ | How long will your numbers be? | ||
+ | 6 | ||
+ | |||
+ | Enter the first number: 999999 | ||
+ | 999999 | ||
+ | Enter the second number: 000111 | ||
+ | 000111 | ||
+ | What would you like to do to the two numbers? | ||
+ | |||
+ | 0: Add | ||
+ | 1: Subtract | ||
+ | 2: Multiply | ||
+ | 3: Divide | ||
+ | 4: Exponent | ||
+ | 5: Modolus | ||
+ | 6: Compare | ||
+ | 7: Quit | ||
+ | 3 | ||
+ | 9009 | ||
+ | lab46: | ||
+ | |||
+ | Take note, you will be prompted to enter the length of the numbers you want this program to deal with. Enter the length of the longest number. | ||
+ | |||
+ | How long will your numbers be? | ||
+ | 2 | ||
+ | |||
+ | Enter the first number: 02 | ||
+ | 02 | ||
+ | Enter the second number: 64 | ||
+ | 64 | ||
+ | What would you like to do to the two numbers? | ||
+ | |||
+ | 0: Add | ||
+ | 1: Subtract | ||
+ | 2: Multiply | ||
+ | 3: Divide | ||
+ | 4: Exponent | ||
+ | 5: Modolus | ||
+ | 6: Compare | ||
+ | 7: Quit | ||
+ | 4 | ||
+ | 073709551616 | ||
+ | lab46: | ||
+ | |||
+ | Take note, you will be prompted to enter the length of the numbers you want this program to deal with. Enter the length of the longest number. | ||
+ | |||
+ | How long will your numbers be? | ||
+ | 3 | ||
+ | |||
+ | Enter the first number: 194 | ||
+ | 194 | ||
+ | Enter the second number: 100 | ||
+ | 100 | ||
+ | What would you like to do to the two numbers? | ||
+ | |||
+ | 0: Add | ||
+ | 1: Subtract | ||
+ | 2: Multiply | ||
+ | 3: Divide | ||
+ | 4: Exponent | ||
+ | 5: Modolus | ||
+ | 6: Compare | ||
+ | 7: Quit | ||
+ | 5 | ||
+ | 094 | ||
+ | lab46: | ||
+ | |||
+ | Take note, you will be prompted to enter the length of the numbers you want this program to deal with. Enter the length of the longest number. | ||
+ | |||
+ | How long will your numbers be? | ||
+ | 32 | ||
+ | |||
+ | Enter the first number: 11111111111111111111111111111111 | ||
+ | 11111111111111111111111111111111 | ||
+ | Enter the second number: 12345678932345345443653434365423 | ||
+ | 12345678932345345443653434365423 | ||
+ | What would you like to do to the two numbers? | ||
+ | |||
+ | 0: Add | ||
+ | 1: Subtract | ||
+ | 2: Multiply | ||
+ | 3: Divide | ||
+ | 4: Exponent | ||
+ | 5: Modolus | ||
+ | 6: Compare | ||
+ | 7: Quit | ||
+ | 6 | ||
+ | |||
+ | The second number is bigger, yo. | ||
+ | |||
+ | lab46: | ||
+ | </ | ||
+ | =====Reflection===== | ||
+ | I liked the logic that I used to create ' | ||
+ | There was the mysterious problem where my function calls were nuking the arrays sent to them.. the only way around that was to create copies of arrays and then transpose the copies back into the originals. More processing power needed, but it did fix the problem. | ||
+ | =====References===== | ||
+ | In performing this project, the following resources were referenced: | ||
+ | Matt Haas. | ||
+ | wedge | ||
+ | Matt Haas | ||
+ | Matt Haas |