User Tools

Site Tools


user:smalik2:portfolio:cprogproject2

Project: BIG NUM

A project for CPROG by Saad Malik during Spring 2012.

This project was begun on 3/14/12 and is anticipated to take 2 days to complete. Project was completed on March 18, 2012.

Objectives

The purpose of this project is to create a program that will be able to deal with numbers that exceed the containment of long long int.

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
  • 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 functions, their parameters and return types

Background

Handle and manipulate numbers larger than what a long long int can contain.

This program will be able to add, subtract, multiply, divide, and compare numbers that contain massive amounts of digits.

Scope

This project will have you implementing code to support the storage and manipulation of numbers outside of the established data types.

In C, from our first project (Project #0), we explored the various established data types, and determined their various sizes and representational ranges.

From that, we should know the largest value we can store in a variable using the biggest data type size (unsigned long long int), which is: 18,446,744,073,709,551,615

That's a 20-digit number.

But this project will have us creating the ability to store and manipulate largers much larger than that. We'll start with a target of 4 and 24 digits (if you write your code effectively, the number of digits should ultimately not matter).

Why 4? Can't we already easily store values of 4 digits?

Yes, but looking to implement the ability to store and manipulate a 4 digit number will help us to better realize the logic and code necessary to scale our solution to support any number of digits.

While there are many approaches to this problem, follow through this example to get some insight. You don't have to take this approach, but it will cover some important concepts you will need to implement in your solution, whether or not you take this approach.

Let's look at a 4 digit number not as a side-effect of being able to be stored in a quantity of appropriate size, but as 4 literal stored digits in memory. To wit:

    unsigned char *value;
    value = (unsigned char *) malloc (sizeof(unsigned char) * 4);
    *(value+0) = *(value+1) = *(value+2) = *(value+3) = 0;

What just happened here? Make sure you understand, or ask questions and get clarification before attempting to continue.

Essentially, we have just allocated 4 bytes of memory (of type unsigned char), which are located consecutively in memory. To draw a picture, we'd have this:

0 0 0 0
*(value+0) *(value+1) *(value+2) *(value+3)

4 bytes of memory, each containing a single digit of our 4 digit number. Let's assume we are attacking this as a decimal (base 10) value, and we'll maintain our assumption that the left-most value is the most significant digit, and the right-most value is the least significant digit.

For example, let's say we wanted to store the 4-digit number 8192 in memory using this scheme. The code and resulting “picture” would be as follows:

    *(value+0) = 8;
    *(value+1) = 1;
    *(value+2) = 9;
    *(value+3) = 2;
8 1 9 2
*(value+0) *(value+1) *(value+2) *(value+3)

Make sense?

Be aware that *(value+0), the first memory address of our sequence, is at the left side of our value… therefore it stores the most significant digit. You are free to do it the other way, just make sure that whatever approach you take, you maintain your logic.

Now, what if we wanted to perform an addition?

8192+4 = 8196

Pretty easy right?

4 in our memory scheme would be represented as “0004”, and we'd accomplish the addition as follows:

    *(value+0) = *(value+0) + 0;
    *(value+1) = *(value+1) + 0;
    *(value+2) = *(value+2) + 0;
    *(value+3) = *(value+3) + 4;

As you can see, the value of “4” was added only to the last (least significant) digit stored in our value. Displaying it should should the expected answer:

8 1 9 6
*(value+0) *(value+1) *(value+2) *(value+3)

There's actually two situations that occur with adding… what we just saw was the straight “sum”. In this case, the sum was the only meaningful result generated.

But there's also another situation we can have, and that is a carry. A carry is when the result is too big to be stored in a single digit (ie a 2 digit number). So we react by storing the least significant digit and carrying the most significant digit to the next placevalue.

Let's take our 8196 and add 1024 to it. What do we get? 9220

Illustrated, we have:

Carry: 0 1 1 0
Value: 8 1 9 6
Addend: 1 0 2 4
Sum: 9 2 2 0
*(value+0) *(value+1) *(value+2) *(value+3)

So, for this project I'd like for you to write a set of functions and a test program that:

  • have a function that will allocate space to store a value of desired length (at least 4 and 24, but feel free to test it with larger numbers: 32, 40, 64, etc.) and return the address (so we can assign it to one of our pointers).
  • have a function that will zero your value, running through each position and setting it to 0.
  • have a function that will accept as a parameter the original number and number to add, perform the operation, and place the result in the original number
  • implement a function to tackle subtraction being mindful of the carry
  • implement a function to perform multiplication
  • implement a function to perform division
  • implement a function that accepts as two arguments two of our dynamically allocated “numbers”, compares them, and returns a -1 if the left parameter is greater, 0 if they are equal, and 1 if the right parameter is greater.
  • implement a sample program that:
    • prompts the user to enter a the number length (4 digits, 24 digits, 32 digits, etc.)
    • prompts the user for actual values (you'll have to rig up a way to convert the user's input into the appropriate values to place in your managed data type
    • gives the user a choice (perhaps via a menu) that lets them select from all the available functions (even resetting and starting over with new digit-lengths).

Code

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
 
 
char* MakeArray(int);
char* ZeroArray(char*, int);
char* Addition(char*, char*, int);
char* Subtraction(char*, char*, int);
void Multiplication(char*, char*, int);
void Division(char*, char*, int);
void Comparison(char*, char*, int);
void PrintArray(char*, int);
 
int main()
{
        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 = MakeArray(length);
        Array = 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;
        }
 
        PrintArray(Array, length);
 
        ArrayB = MakeArray(length);
        ArrayB = 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;
        }
 
        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: Compare\n5: Quit\n");
        scanf("%hhu", &menu);
 
        switch(menu)
        {
                case 0:
                        Array = Addition(Array, ArrayB, length);
                        PrintArray(Array, length);
                        break;
                case 1:
                        Array = Subtraction(Array, ArrayB, length);
                        PrintArray(Array, length);
                        break;
                case 2:
                        Multiplication(Array, ArrayB, length);
                        break;
                case 3:
                        Division(Array, ArrayB, length);
                        break;
                case 4:
                        Comparison(Array, ArrayB, length);
                        break;
                case 5:
                        printf("\nGood-Bye.\n");
                        break;
                default:
                        printf("\nNo valid option chosen\n");
                        break;
        }
 
 
        return(0);
}
 
char * MakeArray(int length)
{
        char *Array;
 
        Array = (char*)malloc(sizeof(char)*length);
 
        return Array;
}
 
char * ZeroArray(char *Array, int length)
{
        for (length; length>=0; --length)
        {
                *(Array+length) = 0;
        }
 
        return Array;
}
 
void PrintArray(char *Array, int length)
{
        for (length; length>=0; --length)
        {
                printf("%hhu", *(Array+length));
        }
}
 
char * Addition(char *Array, char *ArrayB, int length)
{
        char count=0;
        char carry=0;
 
        for(count; count <= length; ++count)
        {
                int holder = (*(Array+count) + *(ArrayB + count))+carry;
                if (holder > 9)
                {
                *(Array+count) = holder-10;
                carry=1;
                }
                else
                {
                *(Array+count) = holder;
                carry=0;
                }
        }
        return Array;
}
 
char * 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;
}
 
void Multiplication(char *Array, char *ArrayB, int length)
{
        int count = 0;
        int holder = 0;
        char *ArrayC;
        ArrayC = MakeArray(length);
        ArrayC = ZeroArray(ArrayC, length);
 
        for (count; count <= length; ++count)
        {
                *(ArrayC+count) = *(Array+count);
        }
        for(count=0; count <= length; ++count)
        {
                int holder = (*(ArrayB+count)* pow(10, count));
                for(holder; holder > 0; --holder)
                {
                        Array = Addition(Array, ArrayC, length);
                }
        }
        Array = Subtraction(Array, ArrayC, length);
        PrintArray(Array, length);
}
 
void Division(char *Array, char *ArrayB, int length)
{
        int count = 0;
        int answer = 0;
        char *ArrayC;
        ArrayC = MakeArray(length);
        ArrayC = ZeroArray(ArrayC, length);
 
        for (count; count <= length; ++count)
        {
                *(ArrayC+count) = *(Array+count);
        }
 
        for(count=length; count>=0; --count)
        {
                while(*(Array+count) > 0)
                {
                        ++answer;
                        Array = Subtraction(Array, ArrayB, length);
                }
        }
        printf("%u", answer);
}
 
void Comparison(char *Array, char *ArrayB, int length)
{
        signed char answer;
        char count;
        for(count=length; count>=0; --count)
        {
                if(*(Array+count) > *(ArrayB+count))
                {
                        printf("\nThe first number is bigger, yo.\n");
                        break;
                }
                if(*(Array+count) < *(ArrayB+count))
                {
                        printf("\nThe second number is bigger, yo.\n");
                        break;
                }
                if(*(Array+count) == *(ArrayB+count))
                {
                        answer++;
                }
        }
 
        if(answer == length)
        {
                printf("\nThe numbers are the same, yo.\n");
        }
}

Execution

Addition
lab46:~/src/cprog/projects$ ./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: 918304928472840192847285
918304928472840192847285
Enter the second number: 001940284739281920482930
001940284739281920482930
What would you like to do to the two numbers?

0: Add
1: Subtract
2: Multiply
3: Divide
4: Compare
5: Quit
0
920245213212122113330215lab46:~/src/cprog/projects$ 
Subtraction
 Enter the first number: 928465489876123498764562
928465489876123498764562
Enter the second number: 123654789654132644648453
123654789654132644648453
What would you like to do to the two numbers?

0: Add
1: Subtract
2: Multiply
3: Divide
4: Compare
5: Quit
1
804810700221990854116109lab46:~/src/cprog/projects$ 
Multiplication
Enter the first number: 1920394827394
1920394827394
Enter the second number: 1920394829384
1920394829384
What would you like to do to the two numbers?

0: Add
1: Subtract
2: Multiply
3: Divide
4: Compare
5: Quit
2
6736759345296lab46:~/src/cprog/projects$ 
Division
How long will your numbers be?
24

Enter the first number: 999999999999999999999999
999999999999999999999999
Enter the second number: 111111111111111111111111
111111111111111111111111
What would you like to do to the two numbers?

0: Add
1: Subtract
2: Multiply
3: Divide
4: Compare
5: Quit
3
9lab46:~/src/cprog/projects$ 
Comparison
How long will your numbers be?
24

Enter the first number: 999999999999999999999999
999999999999999999999999
Enter the second number: 102390384985723180342098
102390384985723180342098
What would you like to do to the two numbers?

0: Add
1: Subtract
2: Multiply
3: Divide
4: Compare
5: Quit
4

The first number is bigger, yo.
lab46:~/src/cprog/projects$ 

Reflection

This project wasn't difficult, as long as you thought out the logic before jumping in. I already had an advantage, due to it being a little similar to an experiment I had done.

It has taught me a good deal about chars and fgetc. Manipulating arrays as well. This is also the first project that I've had to use paper to write out logic, as opposed to just thinking it out in my head beforehand.

References

In performing this project, the following resources were referenced:

http://www.asciitable.com/ Matt Haas And Jacob.

user/smalik2/portfolio/cprogproject2.txt · Last modified: 2012/03/18 15:53 by smalik2