User Tools

Site Tools


user:swilli31:portfolio:cprogproject3

Project: BIG NUM REDUX

A project for C/C++ by Stephanie Williams during the Spring Semester 2012.

This project was begun on April 1 and is anticipated to take one week to complete. Project was completed on April 6, 2012.

Objectives

This project is to fix some problems with project 2 as well as play with classes.

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

The C code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main()
{
        long long int numb1;
        long long int numb2;
        long long int total;
        int menu;
 
        printf("Enter a number: ");
        scanf("%lld", &numb1);
        printf("Enter another number: ");
        scanf("%lld", &numb2);
        printf("Enter selection\n");
        printf("1=addition\n");
        printf("2=subtraction\n");
        printf("3=multiplication\n");
        printf("4=division\n");
        printf("0=exit\n");
        scanf("%d", &menu);
 
        switch(menu){
                case 1: total=numb1+numb2; break;
                case 2: total=numb1-numb2; break;
                case 3: total=numb1*numb2; break;
                case 4: total=numb1/numb2; break;
        };
 
 
        if(menu==1)
        {
                printf("%lld plus %lld is %lld\n", numb1, numb2, total);
        }
        if(menu==2)
        {
                printf("%lld minus %lld is %lld\n", numb1, numb2, total);
        }
        if(menu==3)
        {
                printf("%lld times %lld is %lld\n", numb1, numb2, total);
        }
        if(menu==4)
        {
                printf("%lld divided by %lld is %lld\n", numb1, numb2, total);
        }
        if(menu=0)
        {
                printf("Hope you enjoyed your experience\n");
        }
        return(0);
}
 
 
 

C++ code

include <cstdio>
#include <stdlib>

int main()
{
        long long int numb1;
        long long int numb2;
        long long int total;
        int menu;

        printf("Enter a number: ");
        scanf("%lld", &numb1);
        printf("Enter another number: ");
        scanf("%lld", &numb2);
        printf("Enter selection\n");
        printf("1=addition\n");
        printf("2=subtraction\n");
        printf("3=multiplication\n");
        printf("4=division\n");
        printf("0=exit\n");
        scanf("%d", &menu);

        switch(menu){
         case 1: total=numb1+numb2; break;
                case 2: total=numb1-numb2; break;
                case 3: total=numb1*numb2; break;
                case 4: total=numb1/numb2; break;
        };


        if(menu==1)
        {
                printf("%lld plus %lld is %lld\n", numb1, numb2, total);
        }
        if(menu==2)
        {
                printf("%lld minus %lld is %lld\n", numb1, numb2, total);
        }
        if(menu==3)
        {
                printf("%lld times %lld is %lld\n", numb1, numb2, total);
        }
        if(menu==4)
        {
                printf("%lld divided by %lld is %lld\n", numb1, numb2, total);
        }
        if(menu=0)
        {
                printf("Hope you enjoyed your experience\n");
        }
        return(0);
}

Although the codes are the same they function differently

Execution

An example run of your code (be sure to show off all operations):

lab46:~/src/cprog$ ./big_num
Enter a number: 89764512378645
Enter another number: 5126674513
Enter selection
1=addition
2=subtraction
3=multiplication
4=division
0=exit
4
89764512378645 divided by 5126674513 is 17509
lab46:~/src/cprog$

Reflection

This project wasn't bad. much of the groundwork had already been done.

References

In performing this project, the following resources were referenced:

  • book
  • class notes
user/swilli31/portfolio/cprogproject3.txt · Last modified: 2012/04/07 13:45 by swilli31