A project for C/C++ Programming by Josh Cavaluzzi during the Spring 2012.
This project was begun on Some Date and is anticipated to take a while to complete. Project was completed on May 7, 2012.
State the purpose of this project. What is the point of this project? What do we hope to accomplish by undertaking it?
In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
State the idea or purpose of the project. What are you attempting to pursue?
You'll want to fill this section out with more detailed background information. DO NOT JUST PROVIDE A LINK.
Providing any links to original source material, such as from a project page, is a good idea.
You'll want to give a general overview of what is going to be accomplished (for example, if your project is about installing a web server, do a little write-up on web servers. What is it, why do we need one, how does it work, etc.)
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:
Some helpful hints:
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, multiplication, division, modulus, and exponent.
Can you define multiplication in terms of addition?
Can you define division in terms of subtraction?
The Functions.h file:
GNU nano 2.2.4 File: Functions.h #ifndef _FUNCTION_H #define _FUNCTION_H #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> class Functions{ public: void printary( int, char* ); char* mkArray( int ); char* mkZero( int, char* ); char* Addition( int, char*, char* ); char* Subtraction( int, char*, char* ); char* Multiplication( int, char*, char* ); char* Exponents( int, char* ); int Modulous( int, char*, char* ); int Division( int, char*, char* ); int compare( char*, int ); int compare2( char*, char* ); }; #endif
The math.cc file (Holds all of the functions, not the prototypes):
#include "Functions.h" void Functions::printary( int size, char *array ) { char count; for( count = 0; count < size; count++ ) { printf("%hhu", *(array + count)); } printf("\n"); } char * Functions::mkArray( int size ) { char *array; array = (char *) malloc (sizeof(char) * size); return array; } char * Functions::mkZero( int size, char *array ) { char count; for( count = 0; count <= size; count++ ) { *( array + count ) = 0; } return array; } char * Functions::Addition( int size, char *array1, char *array2 ) { int count; int answer = 0; char *addition, *carry; // Allocating memory for the return array and the carry array addition = mkArray( size ); carry = mkArray( size ); // Setting the array elements equal to zero mkZero( size, addition ); mkZero( size, carry ); // For loop to perform the addition for( count = size; count >= 0; count-- ) { answer = (*(array1 + count) + *(array2 + count) + *(carry + count)); if(answer > 9) { answer = answer - 10; *(carry + (count - 1)) = 1; } *(addition + count) = answer; } return addition; } char * Functions::Subtraction( int size, char *array1, char *array2 ) { int count; int answer = 0; char *subtraction, *carry; // Allocating memory for the arrays subtraction = mkArray( size ); carry = mkArray( size ); // Making the values of the elements of each array zero mkZero( size, subtraction ); mkZero( size, carry ); // For loop to perform subtraction for( count = (size - 1); count >= 0; count-- ) { answer = (*(array1 + count) - *(array2 + count) - *(carry + count)); if( answer < 0 ) { answer = answer + 10; *(carry + (count - 1)) = 1; } *(subtraction + count) = answer; } return subtraction; } char * Functions::Multiplication( int size, char *array1, char *array2 ) { char *multiplication, *one, *holder, *appended1, *appended2; // Creating a new size variable to account for the larger numbered results, as well as $ // the program to print out the result correctly int size2, offset; offset = sizeof(char) * size ; size2 = size + size; // Allocating the memory for the new appended arrays appended1 = mkArray( size2 ); appended2 = mkArray( size2 ); // Setting each element equal to zero mkZero( size2, appended1 ); mkZero( size2, appended2 ); // Copying the memory of the arrays into the new appended arrays, and including the off$ // to make sure that the values are at the end of the memory, instead of in the middle memcpy( appended1 + offset, array1, offset ); memcpy( appended2 + offset, array2, offset ); // Allocating memory in the multiplication array and setting the elements equal to zero multiplication = mkArray( size2 ); multiplication = mkZero( size2, multiplication ); // The same thing as the multiplication array, except this array only holds the number $ // to decrement the second array one = mkArray( size2 ); one = mkZero( size2, one ); *( one + ( size2 - 1 ) ) = 1; // A holder array to keep the results in check holder = mkArray( size2 ); mkZero( size2, holder ); int x; // The while loop has the second appended array check with the number zero, and if all $ // function returns a 1, which then quits while( compare( appended2, size2 ) == 0 ) { x = compare( appended2, size2 ); holder = Addition( size2, holder, appended1 ); appended2 = Subtraction( size2, appended2, one ); } multiplication = holder; return multiplication; } int Functions::Division( int size, char *array1, char *array2 ) { int result = 1; while( compare2( array1, array2 ) == 1 ) { result++; array1 = Subtraction( size, array1, array2 ); } return result; } int Functions::Modulous( int size, char *array1, char *array2 ) { int result = 0; if( compare2( array1, array2 ) == 0 ) { result = 1; } else { while( compare2( array1, array2 ) == 1 ) { result++; array1 = Subtraction( size, array1, array2 ); } } printary( size, array1 ); return 0; } char* Functions::Exponents( int size, char *array1 ) { int power = 0, powersize = 0, size2 = 0; char *exponent, *one, *holder, *appended1; fprintf( stdout, "Please enter the value of the power that you are using: "); fscanf(stdin, "%d", &power); int offset; offset = sizeof(char) * size; size2 = size + size; appended1 = mkArray( size2 ); mkZero( size2, appended1 ); memcpy( appended1 + offset, array1, offset ); exponent = mkArray( size2 ); exponent = mkZero( size2, exponent ); one = mkArray( size2 ); one = mkZero( size2, one ); *( one + ( size2 - 1 ) ) = 1; holder = mkArray( size2 + size2 ); mkZero( size2 + size2, holder ); memcpy( holder + offset, array1, offset ); char *extra; extra = mkArray( size2 ); mkZero( size2, extra ); while( power > 1 ) { holder = Multiplication( size2, holder, appended1 ); memcpy( extra, holder + size2, size2 ); holder = extra; power--; } exponent = holder; return exponent; } // For multiplication only int Functions::compare( char *array2, int size ) { int count; for( count = 0; count < size; count++ ) { if( array2[count] != 0 && array2[count] != '0' ) { return 0; } } return 1; } // For division and modulus int Functions::compare2( char *array1, char *array2 ) { int count, returnVal = 0; for( count = 0; count < sizeof(array1); count++ ) { if( array1[ count ] > array2[ count ] ) { returnVal = 1; break; } else if (array1[count] < array2[count]) { returnVal = -1; break; } } return returnVal; }
THE MAIN FUNCTION FILE:
#include "Functions.h" int main() { Functions *useFunction = new Functions(); char *array1, *array2, *result, menuchoice; int size, size1, size2, i, intResult; fprintf(stdout, "How many digits is the first number (preferably the larger): "); fscanf(stdin, "%u", &size1); fprintf(stdout, "\nHow many digits is the second number: "); fscanf(stdin, "%u", &size2); if( size1 > size2 ) size = size1; else size = size2; size = size + 1; // Creating the arrays array1 = useFunction -> mkArray( size ); array2 = useFunction -> mkArray( size ); result = useFunction -> mkArray( size * 5 ); // Making the array values zero array1 = useFunction -> mkZero( size, array1 ); array2 = useFunction -> mkZero( size, array2 ); result = useFunction -> mkZero( size * 5, result ); fprintf(stdout, "Next, the program will ask you to input the numbers that you want to be stored.\n"); fprintf(stdout, "You MUST add one zero to the beginning of the first number, or else the program will not run correctly.\n"); fprintf(stdout, "Also, for the second number, add in enough zeros to the beginning so that the length matches that of the first number.\n"); fprintf(stdout, "If you would like to perform an operation with an exponent, just input the number that you are raising to a power, and then just follow the directions and choose exponents.\n\n"); fprintf(stdout, "Please enter the number that you want to be stored: "); fgetc(stdin); for(i = 0; i <= size; i++) { *(array1 + i) = fgetc(stdin) - 48; } useFunction -> printary( size, array1 ); fprintf(stdout, "\n\n"); // Next, I created a prompt to have the user input the next number. fprintf(stdout, "Please enter the number that you want to be stored: "); for(i = 0; i < size; i++) { *(array2 + i) = fgetc(stdin) - 48; } useFunction -> printary( size, array2 ); fprintf(stdout, "\n\n"); // Beginning the switch block allowing the user to choose what he/she wants to do with the numbers fprintf(stdout, "What would you like to do with these numbers? "); printf("Choose:\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Divison\n 5. Exponents\n 6. Modulus\n 7. Quit\nEnter now: "); fgetc(stdin); menuchoice = fgetc(stdin); switch( menuchoice ) { case'1': fprintf(stdout, "You have chosen Addition.\n"); result = useFunction -> Addition( size, array1, array2 ); fprintf(stdout, "The result is: "); useFunction -> printary( size, result ); fprintf(stdout, "\n"); break; case'2': fprintf(stdout, "You have chosen Subtraction.\n"); result = useFunction -> Subtraction( size, array1, array2 ); fprintf(stdout, "The result is: "); useFunction -> printary( size, result ); fprintf(stdout, "\n"); break; case'3': fprintf(stdout, "You have chosen Multiplication.\n"); result = useFunction -> Multiplication( size, array1, array2 ); fprintf(stdout, "The result is: "); useFunction -> printary( size + size, result ); fprintf(stdout, "\n"); break; case'4': fprintf(stdout, "You have chosen Division.\n"); intResult = useFunction -> Division( size, array1, array2 ); fprintf(stdout, "The result is: %d\n", intResult ); break; case'5': fprintf(stdout, "You have chosen Exponents.\n"); result = useFunction -> Exponents( size, array1 ); fprintf(stdout, "The result is: "); useFunction -> printary( size + size, result ); break; case'6': fprintf(stdout, "You have chosen Modulus.\n"); fprintf(stdout, "The result is: "); useFunction -> Modulous( size, array1, array2 ); break; case'7': fprintf(stdout, "Have a great day!\n"); break; default: fprintf(stdout, "You haven't chosen a correct option.\n"); break; } return(0); }
Addition:
lab46:~/src/cprog/Projects/project2$ ./project3 How many digits is the first number (preferably the larger): 6 How many digits is the second number: 4 Next, the program will ask you to input the numbers that you want to be stored. You MUST add one zero to the beginning of the first number, or else the program will not run correctly. Also, for the second number, add in enough zeros to the beginning so that the length matches that of the first number. If you would like to perform an operation with an exponent, just input the number that you are raising to a power, and then just follow the directions and choose exponents. Please enter the number that you want to be stored: 0567389 0567389 Please enter the number that you want to be stored: 0005873 0005873 What would you like to do with these numbers? Choose: 1. Addition 2. Subtraction 3. Multiplication 4. Divison 5. Exponents 6. Modulus 7. Quit Enter now: 1 You have chosen Addition. The result is: 0573262 lab46:~/src/cprog/Projects/project2$
Subtraction:
lab46:~/src/cprog/Projects/project2$ ./project3 How many digits is the first number (preferably the larger): 24 How many digits is the second number: 23 Next, the program will ask you to input the numbers that you want to be stored. You MUST add one zero to the beginning of the first number, or else the program will not run correctly. Also, for the second number, add in enough zeros to the beginning so that the length matches that of the first number. If you would like to perform an operation with an exponent, just input the number that you are raising to a power, and then just follow the directions and choose exponents. Please enter the number that you want to be stored: 0983762783761238473281987 0983762783761238473281987 Please enter the number that you want to be stored: 0091111112233445566778899 0091111112233445566778899 What would you like to do with these numbers? Choose: 1. Addition 2. Subtraction 3. Multiplication 4. Divison 5. Exponents 6. Modulus 7. Quit Enter now: 2 You have chosen Subtraction. The result is: 0892651671527792906503088 lab46:~/src/cprog/Projects/project2$
Multiplication:
lab46:~/src/cprog/Projects/project2$ ./project3 How many digits is the first number (preferably the larger): 6 How many digits is the second number: 5 Next, the program will ask you to input the numbers that you want to be stored. You MUST add one zero to the beginning of the first number, or else the program will not run correctly. Also, for the second number, add in enough zeros to the beginning so that the length matches that of the first number. If you would like to perform an operation with an exponent, just input the number that you are raising to a power, and then just follow the directions and choose exponents. Please enter the number that you want to be stored: 0123454 0123454 Please enter the number that you want to be stored: 0012345 0012345 What would you like to do with these numbers? Choose: 1. Addition 2. Subtraction 3. Multiplication 4. Divison 5. Exponents 6. Modulus 7. Quit Enter now: 3 You have chosen Multiplication. The result is: 00001524039630 lab46:~/src/cprog/Projects/project2$
Division:
lab46:~/src/cprog/Projects/project2$ ./project3 How many digits is the first number (preferably the larger): 8 How many digits is the second number: 2 Next, the program will ask you to input the numbers that you want to be stored. You MUST add one zero to the beginning of the first number, or else the program will not run correctly. Also, for the second number, add in enough zeros to the beginning so that the length matches that of the first number. If you would like to perform an operation with an exponent, just input the number that you are raising to a power, and then just follow the directions and choose exponents. Please enter the number that you want to be stored: 048828125 048828125 Please enter the number that you want to be stored: 000000025 000000025 What would you like to do with these numbers? Choose: 1. Addition 2. Subtraction 3. Multiplication 4. Divison 5. Exponents 6. Modulus 7. Quit Enter now: 4 You have chosen Division. The result is: 1953125 lab46:~/src/cprog/Projects/project2$
Exponents:
lab46:~/src/cprog/Projects/project2$ ./project3 How many digits is the first number (preferably the larger): 1 How many digits is the second number: 1 Next, the program will ask you to input the numbers that you want to be stored. You MUST add one zero to the beginning of the first number, or else the program will not run correctly. Also, for the second number, add in enough zeros to the beginning so that the length matches that of the first number. If you would like to perform an operation with an exponent, just input the number that you are raising to a power, and then just follow the directions and choose exponents. Please enter the number that you want to be stored: 06 06 Please enter the number that you want to be stored: 01 01 What would you like to do with these numbers? Choose: 1. Addition 2. Subtraction 3. Multiplication 4. Divison 5. Exponents 6. Modulus 7. Quit Enter now: 5 You have chosen Exponents. Please enter the value of the power that you are using: 5 The result is: 7776 lab46:~/src/cprog/Projects/project2$
Modulus:
lab46:~/src/cprog/Projects/project2$ ./project3 How many digits is the first number (preferably the larger): 4 How many digits is the second number: 3 Next, the program will ask you to input the numbers that you want to be stored. You MUST add one zero to the beginning of the first number, or else the program will not run correctly. Also, for the second number, add in enough zeros to the beginning so that the length matches that of the first number. If you would like to perform an operation with an exponent, just input the number that you are raising to a power, and then just follow the directions and choose exponents. Please enter the number that you want to be stored: 01234 01234 Please enter the number that you want to be stored: 00123 00123 What would you like to do with these numbers? Choose: 1. Addition 2. Subtraction 3. Multiplication 4. Divison 5. Exponents 6. Modulus 7. Quit Enter now: 6 You have chosen Modulus. The result is: 00004 lab46:~/src/cprog/Projects/project2$
Quit:
lab46:~/src/cprog/Projects/project2$ ./project3 How many digits is the first number (preferably the larger): 1 How many digits is the second number: 1 Next, the program will ask you to input the numbers that you want to be stored. You MUST add one zero to the beginning of the first number, or else the program will not run correctly. Also, for the second number, add in enough zeros to the beginning so that the length matches that of the first number. If you would like to perform an operation with an exponent, just input the number that you are raising to a power, and then just follow the directions and choose exponents. Please enter the number that you want to be stored: 01 01 Please enter the number that you want to be stored: 01 01 What would you like to do with these numbers? Choose: 1. Addition 2. Subtraction 3. Multiplication 4. Divison 5. Exponents 6. Modulus 7. Quit Enter now: 7 Have a great day! lab46:~/src/cprog/Projects/project2$
Default:
lab46:~/src/cprog/Projects/project2$ ./project3 How many digits is the first number (preferably the larger): 1 How many digits is the second number: 1 Next, the program will ask you to input the numbers that you want to be stored. You MUST add one zero to the beginning of the first number, or else the program will not run correctly. Also, for the second number, add in enough zeros to the beginning so that the length matches that of the first number. If you would like to perform an operation with an exponent, just input the number that you are raising to a power, and then just follow the directions and choose exponents. Please enter the number that you want to be stored: 01 01 Please enter the number that you want to be stored: 01 01 What would you like to do with these numbers? Choose: 1. Addition 2. Subtraction 3. Multiplication 4. Divison 5. Exponents 6. Modulus 7. Quit Enter now: 8 You haven't chosen a correct option. lab46:~/src/cprog/Projects/project2$
This project was very interesting. The Exponents function gave me a hard time, but I finally finished it, thank GREAT ODIN'S SPEAR!!! It helped me become reacquainted with C++, classes, and multiple file use. All-in-all, success!
In performing this project, the following resources were referenced: