User Tools

Site Tools


user:smalik2:portfolio:cprogproject3

Project: BIG NUM REDUX

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. Errors the cause of which is still unknown.

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/perform this project, the listed resources/experiences need to be consulted/achieved:

  • 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, and address calculation
  • familiarity with looking up C function parameters/information in the manual
  • 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, multiplication, and division functions that can easily and transparently work with numbers of any length
  • 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, multiplication, division, modulus, and exponent.

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 /home/smalik2/src/cprog/projects/Project2Monolithic.c

The seperated C code file is located at: cd /home/smalik2/src/cprog/projects/CProject2 _

The monolithic C++ code file is at: vim /home/smalik2/src/cprog/projects/Project2Monolithic.cc

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

#ifndef _FUNCTIONS_H                                                                                                                
#define _FUNCTIONS_H
 
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
 
class Functions{
    public:
        char* MakeArray(int);
        char* ZeroArray(char*, int);
        int CheckZero(char*, int);
        char* Addition(char*, char*, int);
        char* Subtraction(char*, char*, int);
        char* Multiplication(char*, char*, int);
        char* Exponent(char*, char*, int);
        void Modolus(char*, char*, int);
        void Division(char*, char*, int);
        int Comparison(char*, char*, int);
        void PrintArray(char*, int);
        void CopyArray(char*, char*, int);
};
 
#endif

MathFunctions.cc

#include "Functions.h"                                                                                                                                                                                                                                                  
 
char * Functions::Addition(char *ArrayZ, char *ArrayB, int length)
{
    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::Subtraction(char *Array, char *ArrayB, int length)
{
    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::Multiplication(char *ArrayZ, char *ArrayB, int length)
{
    int count = 0;
    char *ArrayC;
    ArrayC = MakeArray((2*length)+1);
    ArrayC = ZeroArray(ArrayC, (2*length)+1);
 
    char *ArrayD;
    ArrayD = MakeArray((length));       //This whole array is simply a one,
    ArrayD = ZeroArray(ArrayD, length); //We needed it to pass it to Subtraction,
    *(ArrayD+0)=1;                      //Because it takes an array argument.
 
    while(CheckZero(ArrayB, length) == 1)
    {
        ArrayC = Addition(ArrayC, ArrayZ, (2*length)+1);
        ArrayB = Subtraction(ArrayB, ArrayD, length);
    }
    return ArrayC;
}
 
char* Functions::Exponent(char *Array, char *ArrayB, int length)
{
    int count = 0;
    char* ArrayC;
    ArrayC = MakeArray(5*(2*length)+1);
    ArrayC = ZeroArray(ArrayC, 5*(2*length)+1);
 
    char* ArrayD;
    ArrayD = MakeArray((length));       //This whole array is simply a one,
    ArrayD = ZeroArray(ArrayD, length); //We needed it to pass it to Subtraction,
    *(ArrayD+0)=1;                      //Because it takes an array argument.
 
    ArrayB = Subtraction(ArrayB, ArrayD, length);
    CopyArray(Array, ArrayC, length);
 
    char* ArrayZ;
    ArrayZ = MakeArray(length);
    ArrayZ = ZeroArray(ArrayZ, length);
    CopyArray(Array, ArrayZ, length);
    while(CheckZero(ArrayB, length) == 1)
    {
        ArrayC = Multiplication(ArrayC, Array, (5*length));
        ArrayB = Subtraction(ArrayB, ArrayD, length);
        CopyArray(ArrayZ, Array, length);
    }
    return ArrayC;
}
 
void Functions::Division(char *Array, char *ArrayB, int length)
{
    int count = 0;
    int answer = 0;
 
    for(count=length; count>=0; --count)
    {
        while(*(Array+count) > 0)
        {
            ++answer;
            Array = Subtraction(Array, ArrayB, length);
        }
    }
    printf("%u", answer);
}
 
 
void Functions::Modolus(char *Array, char *ArrayB, int length)
{
    int count = 0;
    int answer = 0;
 
    while (Comparison(Array, ArrayB, length) == 1)
    {
        Array = Subtraction(Array, ArrayB, length);
    }
    PrintArray(Array, length);
}

ArrayFunctions.cc

#include "Functions.h"                                                                                                                                                                                
 
char * Functions::MakeArray(int length)
{
    char *Array;
 
    Array = (char*)malloc(sizeof(char)*length);
 
    return Array;
}
 
char * Functions::ZeroArray(char *Array, int length)
{
    for (length; length>=0; --length)
    {   
        *(Array+length) = 0;
    }   
 
    return Array;
}
 
int Functions::CheckZero(char *Array, int length)
{
    for (length; length>=0; --length)
    {   
        if (*(Array+length) != 0)
        {   
            return 1;
            break;
        }   
    }   
    return 0;
}
 
void Functions::PrintArray(char *Array, int length)
{
    for (length; length>=0; --length)
    {   
        printf("%hhu", *(Array+length));
    }   
}
 
void Functions::CopyArray(char *Array, char *ArrayB, int length)
{                                                                                                                                                                                                         
    for (length; length>=0; --length)
    {   
        *(ArrayB+length) = *(Array+length);
    }   
}
 
int Functions::Comparison(char *Array, char *ArrayB, int length)
{
    char count;
    for(count=length; count>=0; --count)
    {   
        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).

#include "Functions.h"                                                                                                                                                                                                                                                  
 
int main()
{
    Functions *UseFunction = new Functions();
    char *Array;
    char *ArrayB;
    int count;
    int length;
 
    printf("\nTake 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.  If, say, you wish to multiply 209 by 14 you will say the length will be 3, and then when you enter the 14, MAKE SURE you enter it as 014, else the program will get quite confused and you will not get the result you want.\n");
 
    printf("\nHow long will your numbers be?\n");
    scanf("%u", &length);
    length = (length)-1;
 
    Array = UseFunction->MakeArray(length);
    Array = UseFunction->ZeroArray(Array, length);
 
    printf("\nEnter the first number: ");
 
    fgetc(stdin);
    for(count=length; count>=0; --count)
    {   
        *(Array+count)=fgetc(stdin);
        *(Array+count) = *(Array+count)-48;
    }   
 
    UseFunction->PrintArray(Array, length);
 
    ArrayB = UseFunction->MakeArray(length);
    ArrayB = UseFunction->ZeroArray(ArrayB, length);
 
    printf("\nEnter the second number: ");
 
    fgetc(stdin);
    for(count=length; count>=0; --count)
    {   
        *(ArrayB+count) = fgetc(stdin);
        *(ArrayB+count) = *(ArrayB+count) - 48; 
    }   
 
    UseFunction->PrintArray(ArrayB, length);
 
    char menu;
    printf("\nWhat would you like to do to the two numbers?\n");
    printf("\n0: Add\n1: Subtract\n2: Multiply\n3: Divide\n4: Exponent\n5: Modolus\n6: Compare\n7: Quit\n");
    scanf("%hhu", &menu);
 
    switch(menu)
    {   
        case 0:
            Array = UseFunction->Addition(Array, ArrayB, length);
            UseFunction->PrintArray(Array, length);
            break;
        case 1:
            Array = UseFunction->Subtraction(Array, ArrayB, length);
            UseFunction->PrintArray(Array, length);
            break;
        case 2:
            {   
                length = length*2+1;
                char* ArrayC;
                ArrayC = UseFunction->MakeArray(length);
                ArrayC = UseFunction->ZeroArray(ArrayC, length);
                ArrayC = UseFunction->Multiplication(Array, ArrayB, length);
                UseFunction->PrintArray(ArrayC, length);
                break;
            }   
        case 3:
            UseFunction->Division(Array, ArrayB, length);
            break;
        case 4:
            {   
                length = 5*(2*length)+1;
                char* ArrayC;
                ArrayC = UseFunction->MakeArray(length);
                ArrayC = UseFunction->ZeroArray(ArrayC, length);
                ArrayC = UseFunction->Exponent(Array, ArrayB, (length-1)/10);
                UseFunction->PrintArray(ArrayC, length);
                break;
            }   
        case 5:
            UseFunction->Modolus(Array, ArrayB, length);    
            break;
        case 6:
            {   
                int answer; 
                answer = UseFunction->Comparison(Array, ArrayB, length);
 
                if (answer == 2)
                {   
                    printf("\nThe numbers are the same, yo.\n");
                }   
 
                if (answer == 1)
                {   
                    printf("\nThe first number is bigger, yo.\n");
                }   
 
                if (answer == 0)
                {   
                    printf("\nThe second number is bigger, yo.\n");
                }   
            }   
            break;
        case 7:
            printf("\nYou chose to quit, bye\n");
            break;
        default:
            printf("\nNo valid option chosen\n");
            break;
    }   
    printf("\n");
    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 'perfect' functions, that return the actual correct value, dynamically allocating array sizes. The downside is that it requires much more processing power.

lab46:~/src/cprog/projects/C++Project2$ g++ -c *.cc
lab46:~/src/cprog/projects/C++Project2$ g++ -o Project2 *.o
lab46:~/src/cprog/projects/C++Project2$ ./Project2 

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.  If, say, you wish to multiply 209 by 14 you will say the length will be 3, and then when you enter the 14, MAKE SURE you enter it as 014, else the program will get quite confused and you will not get the result you want.

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:~/src/cprog/projects/C++Project2$ ./Project2 

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.  If, say, you wish to multiply 209 by 14 you will say the length will be 3, and then when you enter the 14, MAKE SURE you enter it as 014, else the program will get quite confused and you will not get the result you want.

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:~/src/cprog/projects/C++Project2$ ./Project2 

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.  If, say, you wish to multiply 209 by 14 you will say the length will be 3, and then when you enter the 14, MAKE SURE you enter it as 014, else the program will get quite confused and you will not get the result you want.

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:~/src/cprog/projects/C++Project2$ ./Project2 

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.  If, say, you wish to multiply 209 by 14 you will say the length will be 3, and then when you enter the 14, MAKE SURE you enter it as 014, else the program will get quite confused and you will not get the result you want.

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:~/src/cprog/projects/C++Project2$ 

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.  If, say, you wish to multiply 209 by 14 you will say the length will be 3, and then when you enter the 14, MAKE SURE you enter it as 014, else the program will get quite confused and you will not get the result you want.

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:~/src/cprog/projects/C++Project2$ 

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.  If, say, you wish to multiply 209 by 14 you will say the length will be 3, and then when you enter the 14, MAKE SURE you enter it as 014, else the program will get quite confused and you will not get the result you want.

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:~/src/cprog/projects/C++Project2$ 

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.  If, say, you wish to multiply 209 by 14 you will say the length will be 3, and then when you enter the 14, MAKE SURE you enter it as 014, else the program will get quite confused and you will not get the result you want.

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:~/src/cprog/projects/C++Project2$ 

Reflection

I liked the logic that I used to create 'perfect' functions. It was fun. 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

user/smalik2/portfolio/cprogproject3.txt · Last modified: 2012/04/26 05:11 by smalik2