User Tools

Site Tools


user:brobbin4:portfolio:cprogproject3

Project: BIG NUM REDUX

A project for CSCS1320 by Brian Robbins during the Spring of 2012.

This project was begun on April 2, 2012 and is anticipated to take an unknown amount of time to complete. Project was completed on May 3, 2012.

Objectives

The purpose of this project is to build onto project 2 so we may continue learning about arrays and to further develop our programming skills.

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

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.)

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

bignum.c

 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
        char* num1;
        char* num2;
        char* num3;
        char* num4;
        char a=0, y=0;
        int z, x;

        printf("\n");

        do
        {

        printf("\nWhat would you like to do: ");
        printf("\nEnter '1' for addition. ");
        printf("\nEnter '2' for substraction. ");
        printf("\nEnter '3' for multiplication. ");
        printf("\nEnter '4' for division. ");
        printf("\nEnter '6' to quit. ");
        printf("\n");
        printf("\nPlease enter an option: ");
        scanf("%d", &x);
        if( x == 6 )
        {
                printf("\n");
                return(0);
        }

        printf("\nPlease enter the number of digits of your largest number: ");
        fscanf(stdin, "%hhd", &a);
        
        num1 = (char*) malloc(sizeof(char) * a);
        num2 = (char*) malloc(sizeof(char) * a);
        num3 = (char*) malloc(sizeof(char) * a);
        num4 = (char*) malloc(sizeof(char) * a);

        for(z=0; z<a; z++)
        {
                *(num1+z)=0;
                *(num2+z)=0;
                *(num3+z)=0;
                *(num4+z)=0;
        }

        *(num3+a-1)=1;

        fgetc(stdin);

        y=0;

        printf("\n");

        printf("Please enter your first number.");
        printf("\nNote, If this number is the smaller of the two numbers be sure to pad the number with zeros): ");
        z=fgetc(stdin);
        while (z !='\n')
        {
                *(num1+y)=z -48;
                y++;
                z = fgetc(stdin);
        }

        y=0;

        printf("\nPlease enter your second number.");
        printf("\nNote, if this number is the smaller of the two numbers be sure to pad the number with zeros): ");
        z=fgetc(stdin);
        while (z !='\n')
        {
                *(num2+y)=z -48;
                y++;
                z = fgetc(stdin);
        }

        y=0;

        z=0;

                switch(x)
                {
                        case 1:
                                        add(num1, num2, a);

                                        y=0;
                                        
                                        printf("\nThe sum of the two numbers is: ");
                                        for(y=0; y<a; y++)
                                        {
                                                printf("%hhd", *(num1+y));
                                        }
                                        printf("\n");
                                        break;

                        case 2:
                                        subtract(num1, num2, a);

                                        y=0;

                                        printf("\nThe remainder of the two numbers is: ");
                                        for(y=0; y<a; y++)
                                        {
                                                printf("%hhd", *(num1+y));

                                        }
                                        printf("\n");
                                        break;

                        case 2:
                                        subtract(num1, num2, a);

                                        y=0;

                                        printf("\nThe remainder of the two numbers is: ");
                                        for(y=0; y<a; y++)
                                        {
                                                printf("%hhd", *(num1+y));
                                        }
                                        printf("\n");
                                        break;

                        case 3:
                                        multiply(num1, num2, num3, num4, a);

                                        y=0;

                                        printf("\nThe product of the two numbers is: ");
                                        for(y=0; y<a; y++)
                                        {
                                                printf("%hhd", *(num1+y));
                                        }
                                        printf("\n");
                                        break;

                        case 4:
                                        divide(num1, num2, num3, num4, a);

                                        y=0;

                                        printf("\nThe quotient of the two numbers is: ");
                                        for(y=0; y<a; y++)
                                        {
                                                printf("%hhd", *(num4+y));
                                        }
                                        printf("\n");
                                        break;

                        }

        }

        while (x != 6);
        
        printf("\n");
        printf("\n");

        return(0);
}

function.h

#ifndef _FUNCTIONS_H
#define _FUNCTIONS_H
#include "stdlib.h"

void add(char* , char*, char);
void subtract(char*, char*, char);
void multiply(char*, char*, char*, char*, char);
void divide(char*, char*, char*, char*, char);
char compare(char*, char*, char);

#endif

add.c

#include "functions.h"

void add(char *num1, char *num2, char a)
{
        char y, c = 0;

        for(y=a-1; y>=0; y--)
        {
                c=*(num1+y)+*(num2+y);

                if(c >= 10)
                {
                        *(num1+y)=c-10;
                        if (y != 0)
                        {
                                *(num1+y-1)=*(num1+y-1)+1;
                        }
                }
                else
                {
                        *(num1+y)=c;
                }
        }

        return;
}

subtract.c

#include "functions.h"

void subtract(char *num1, char *num2, char a)
{
        char y, c = 0;
        for (y=a-1; y>=0; y--)
        {
                c=*(num1+y)-*(num2+y);

                if(c < 0)
                {
                        *(num1+y)=c+10;
                        if(y != 0)
                        {
                                *(num1+y-1)=*(num1+y-1)-1;
                        }
                }
                else
                {
                        *(num1+y)=c;
                }

        }

        return;
}

multiply.c

#include "functions.h"

void multiply(char *num1, char *num2, char *num3, char *num4, char a)
{
        char y;

        char *numLocal=(char*)malloc(sizeof(char)*a);

        for(y=0; y<a; y++)
        {
                *(numLocal+y)=*(num1+y);
                *(num1+y)=0;
        }

        while(compare(num2, num4, a) !=0)
        {
                add(num1, numLocal, a);
                subtract(num2, num3, a);
        }

        return;

}

divide.c

include "functions.h"

void divide(char *num1, char *num2, char *num3, char *num4, char a)
{
        char y;

        char *numLocal=(char*)malloc(sizeof(char)*a);

        for(y=0; y<a; y++)
        {
                *(numLocal+y)=*(num2+y);
        }

        while(compare(num1, numLocal, a) !=-1)
        {
                subtract(num1, numLocal, a);
                add(num4, num3, a);
        }

        return;
}

exponet.c

#include "functions.h"
#include "math.h"

void exponet(long long int numA, long long int numB, long long int *answer)
{

        *answer=pow(numA,numB);

        return;
}

modulus.c

#include "functions.h"

void modulus(char *num1, char *num2, char *num3, char *num4, char a)
{
        char y;

        char *numLocal=(char*)malloc(sizeof(char)*a);

        for(y=0; y<a; y++)
        {
                *(numLocal+y)=*(num2+y);
        }

        while(compare(num1, numLocal, a) !=-1)
        {
                subtract(num1, numLocal, a);
        }

        return;
}

compare.c

include "functions.h"

char compare(char *num1, char *num2, char a)
{
        char y, flag=1;

        for(y=0; y<a; y++) 
        {
                if(*(num1+y)==*(num2+y))
                {
                        flag=0;
                }
                else
                {
                        if(*(num1+y)>*(num2+y))
                        {
                                flag=1;
                        }
                        else
                        {
                                flag=-1;
                        }
                        break;
                }
        }
        return(flag);
}

Execution

Example of the add function

lab46:~/src/cprog/project3$ ./bignum


What would you like to do: 
Enter '1' for addition. 
Enter '2' for substraction. 
Enter '3' for multiplication. 
Enter '4' for division. 
Enter '5' for exponets. 
Enter '6' for modulus. 
Enter '7' to quit.. 

Please enter an option: 1

Please enter the number of digits of your largest number: 2

Please enter your first number.
Note, If this number is the smaller of the two numbers be sure to pad the number with zeros): 05

Please enter your second number.
Note, if this number is the smaller of the two numbers be sure to pad the number with zeros): 04

The sum of the two numbers is: 09

What would you like to do: 
Enter '1' for addition. 
Enter '2' for substraction. 
Enter '3' for multiplication. 
Enter '4' for division. 
Enter '5' for exponets. 
Enter '6' for modulus. 
Enter '7' to quit.

Please enter an option: 7

lab46:~/src/cprog/project3$ 

Example of the subtract function

lab46:~/src/cprog/project3$ ./bignum


What would you like to do: 
Enter '1' for addition. 
Enter '2' for substraction. 
Enter '3' for multiplication. 
Enter '4' for division. 
Enter '5' for exponets. 
Enter '6' for modulus. 
Enter '7' to quit.. 

Please enter an option: 2

Please enter the number of digits of your largest number: 2

Please enter your first number.
Note, If this number is the smaller of the two numbers be sure to pad the number with zeros): 05

Please enter your second number.
Note, if this number is the smaller of the two numbers be sure to pad the number with zeros): 04

The remainder of the two numbers is: 01

What would you like to do: 
Enter '1' for addition. 
Enter '2' for substraction. 
Enter '3' for multiplication. 
Enter '4' for division. 
Enter '5' for exponets. 
Enter '6' for modulus. 
Enter '7' to quit. 

Please enter an option: 7

lab46:~/src/cprog/project3$ 

Example of multiplication function

lab46:~/src/cprog/project3$ ./bignum


What would you like to do: 
Enter '1' for addition. 
Enter '2' for substraction. 
Enter '3' for multiplication. 
Enter '4' for division. 
Enter '5' for exponets. 
Enter '6' for modulus. 
Enter '7' to quit. 

Please enter an option: 3

Please enter the number of digits of your largest number: 2

Please enter your first number.
Note, If this number is the smaller of the two numbers be sure to pad the number with zeros): 05

Please enter your second number.
Note, if this number is the smaller of the two numbers be sure to pad the number with zeros): 04

The product of the two numbers is: 20

What would you like to do: 
Enter '1' for addition. 
Enter '2' for substraction. 
Enter '3' for multiplication. 
Enter '4' for division. 
Enter '5' for exponets. 
Enter '6' for modulus. 
Enter '7' to quit.

Please enter an option: 7

lab46:~/src/cprog/project3$ 

Example of division function

lab46:~/src/cprog/project3$ ./bignum 


What would you like to do: 
Enter '1' for addition. 
Enter '2' for substraction. 
Enter '3' for multiplication. 
Enter '4' for division. 
Enter '5' for exponets. 
Enter '6' for modulus. 
Enter '7' to quit.

Please enter an option: 4

Please enter the number of digits of your largest number: 2   

Please enter your first number.
Note, If this number is the smaller of the two numbers be sure to pad the number with zeros): 06

Please enter your second number.
Note, if this number is the smaller of the two numbers be sure to pad the number with zeros): 02

The quotient of the two numbers is: 03

What would you like to do: 
Enter '1' for addition. 
Enter '2' for substraction. 
Enter '3' for multiplication. 
Enter '4' for division. 
Enter '5' for exponets. 
Enter '6' for modulus. 
Enter '7' to quit.

Please enter an option: 7

lab46:~/src/cprog/project3$ 

Example of the exponent function

lab46:~/src/cprog/project3.2/multi$ ./bignum


What would you like to do: 
Enter '1' for addition. 
Enter '2' for substraction. 
Enter '3' for multiplication. 
Enter '4' for division. 
Enter '5' for exponets. 
Enter '6' for modulus. 
Enter '7' to quit. 

Please enter an option: 5

Please enter the base value: 8

Please enter the exponet value: 5

The exponential value of the numbers is: 32768

What would you like to do: 
Enter '1' for addition. 
Enter '2' for substraction. 
Enter '3' for multiplication. 
Enter '4' for division. 
Enter '5' for exponets. 
Enter '6' for modulus. 
Enter '7' to quit. 

Please enter an option: 7

lab46:~/src/cprog/project3.2/multi$ 

Example of the modulus function

lab46:~/src/cprog/project3.2/multi$ ./bignum


What would you like to do: 
Enter '1' for addition. 
Enter '2' for substraction. 
Enter '3' for multiplication. 
Enter '4' for division. 
Enter '5' for exponets. 
Enter '6' for modulus. 
Enter '7' to quit. 

Please enter an option: 6

Please enter the number of digits of your largest number: 4

Please enter your first number.
Note, If this number is the smaller of the two numbers be sure to pad the number with zeros: 0013 

Please enter your second.
Note, if this number is the smaller of the two numbers be sure to pad the number with zeros: 0005

The modulus of the two numbers is: 0003

What would you like to do: 
Enter '1' for addition. 
Enter '2' for substraction. 
Enter '3' for multiplication. 
Enter '4' for division. 
Enter '5' for exponets. 
Enter '6' for modulus. 
Enter '7' to quit. 

Please enter an option: 7

lab46:~/src/cprog/project3.2/multi$ 

Reflection

Conversion to functions was relatively easy. Many sleepless nights were spent trying to get an exponent function working but due to time constraints in the course I have been forced to abandon the remaining two functions to work on completing other projects.

!UPDATE!

Since the original post I have been able to successfully implement both and exponent and modulus function into the program. The only exception is the exponent function currently does not handle really large numbers due to the fact its written using long long int as the data type.

I have had a difficult time grasping classes in C++ and therefore I have not translated the program into C++. I will however continue learning about classes so that I can convert the program in the future.

References

In performing this project, the following resources were referenced:

  • C Pocket Reference from O'REILLY
  • The C Programming Language SECOND EDITION from Brian W. Kernighan and Dennis M. Ritchie
  • The all mighty wedge
  • The lab46 IRC Computer Sciences Channel
user/brobbin4/portfolio/cprogproject3.txt · Last modified: 2012/05/03 23:20 by brobbin4