User Tools

Site Tools


user:dmckinn2:portfolio:cprogproject3

Project: BIG NUM REDUX

A project for C/C++ by Daniel McKinney during the spring 2012.

This project was begun on 4/4 and is anticipated to take 10 hrs to complete. Project was completed on 04/6/12

Objectives

purpose of this project is to, understand how the math functions work, and find a way to implement them. Also to get familiar with multi file programs.

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 monolithic code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void addition();
void subtraction();
void multiplication();
void division();
void expon();
void mod();
int main()
{
int temp = 0;
int function = 0;
 
    while (function == 0)
    {
        printf("What math function would you like to do (hit 0 to exit):\n");
        printf("1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit\n");
        printf(":");
        scanf("%d", &temp);
                  if(temp > 6 || temp < 0)
                { printf("You need to pick a number between 1 and 4!!\n");
                function=0;
                }
                else
                  {
                  function = temp;
                        }
        if(temp==0)
                {function = 9;
                }
    }
    if (function == 1)
    {
        addition();
    }
    if (function == 2)
    {
       subtraction();
    }
    if (function == 3)
    {
        multiplication();
    }
    if (function == 4)
    {
        division();
    }
    if (function == 5)
    {
        expon();
    }
    if (function == 6)
    {
        mod();
    }
 
    return(0);
}
 
void addition()
{
float a=0;
float b=0;
float x=0;
 
printf("Enter two numbers separated by commas to be added:");
scanf("%f,%f",&a,&b);
x=a+b;
printf("The sum is: %f\n\n\n",x);
main();
}
 
 
void subtraction()
{
float a=0;
float b=0;
float x=0;
 
printf("Enter two numbers separated by commas to be subtracted:");
scanf("%f,%f",&a,&b);
x=a-b;
printf("The difference is: %f\n\n\n",x);
main();
}
 
void multiplication()
{
float a=0;
float b=0;
float x=0;
 
printf("Enter two numbers separated by commas to be multiplied:");
scanf("%f,%f",&a,&b);
x=a*b;
printf("The product is: %f\n\n\n",x);
main();
}
 
void division()
{
float a=0;
float b=0;
float x=0;
 
printf("Enter two numbers separated by commas to be divided:");
scanf("%f,%f",&a,&b);
x=a/b;
printf("The quotient is: %f\n\n\n",x);
main();
}
 
 
 
void expon()
{
int a=0;
int b=0;
int x=1;
int index;
printf("Enter two numbers separated by commas( in a^b format):");
scanf("%d,%d",&a,&b);
for(index=1; index<=b; index++)
{x*=a;
}
printf("The answer is: %d\n\n\n",x);
main();
}
void mod()
{
    int num=0;
    printf("Enter a number, to see if it is odd or even:");
    scanf("%d", &num);
    if ( num % 2 == 0 )
    {
        printf("Your number is Even\n\n\n ",num);
    }
        if ( num % 2 != 0 )
    {
        printf("Your number is Odd\n\n\n ",num);
    }
 
    main();
}

the c code for the multi file program (with add.h and sub.h included for the add and subtraction functions)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include"add.h"
#include"sub.h"
void addition();
void subtraction();
void multiplication();
void division();
void expon();
void mod();
int main()
{
int temp = 0;
int function = 0;
 
    while (function == 0)
    {
        printf("What math function would you like to do (hit 0 to exit):\n");
        printf("1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit\n");
        printf(":");
        scanf("%d", &temp);
                  if(temp > 6 || temp < 0)
                { printf("You need to pick a number between 1 and 4!!\n");
                function=0;
                }
                else
                  {
                  function = temp;
                        }
        if(temp==0)
                {function = 9;
                }
    }
    if (function == 1)
    {
        addition();
    }
    if (function == 2)
    {
       subtraction();
    }
    if (function == 3)
    {
        multiplication();
    }
    if (function == 4)
    {
        division();
    }
    if (function == 5)
    {
        expon();
    }
    if (function == 6)
    {
        mod();
    }
  return(0);
}
 
void multiplication()
{
float a=0;
float b=0;
float x=0;
 
printf("Enter two numbers separated by commas to be multiplied:");
scanf("%f,%f",&a,&b);
x=a*b;
printf("The product is: %f\n\n\n",x);
main();
}
 
void division()
{
float a=0;
float b=0;
float x=0;
 
printf("Enter two numbers separated by commas to be divided:");
scanf("%f,%f",&a,&b);
x=a/b;
printf("The quotient is: %f\n\n\n",x);
main();
}
 
 
 
void expon()
{
int a=0;
int b=0;
int x=1;
int index;
printf("Enter two numbers separated by commas( in a^b format):");
scanf("%d,%d",&a,&b);
for(index=1; index<=b; index++)
{x*=a;
}
printf("The answer is: %d\n\n\n",x);
main();
}
 
 
void mod()
{
    int num=0;
    printf("Enter a number, to see if it is odd or even:");
    scanf("%d", &num);
    if ( num % 2 == 0 )
    {
        printf("Your number is Even\n\n\n ",num);
    }
        if ( num % 2 != 0 )
    {
        printf("Your number is Odd\n\n\n ",num);
    }
 
    main();
}

The class-based monolithic program in C++

#include<iostream>
#include <stdio.h>
#include <cstdlib>
#include <cmath>
 
class math{
 public:
       math();
 
void addition();
void subtraction();
void multiplication();
void division();
void expon();
void mod();
 
};
int main()
{
void addition();
void subtraction();
void multiplication();
void division();
void expon();
void mod();
 
int temp = 0;
int function = 0;
 
    while (function == 0)
    {
        printf("What math function would you like to do (hit 0 to exit):\n");
        printf("1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit\n");
        printf(":");
        scanf("%d", &temp);
                  if(temp > 6 || temp < 0)
                { printf("You need to pick a number between 1 and 4!!\n");
                function=0;
                }
                else
                  {
                  function = temp;
                        }
        if(temp==0)
                {function = 9;
                }
    }
    if (function == 1)
    {
        addition();
    }
    if (function == 2)
    {
       subtraction();
    }
    if (function == 3)
    {
      multiplication();
    }
    if (function == 4)
    {
        division();
    }
    if (function == 5)
    {
        expon();
    }
    if (function == 6)
    {
        mod();
    }
 
    return(0);
}
void addition()
{
float a=0;
float b=0;
float x=0;
 
printf("Enter two numbers separated by commas to be added:");
scanf("%f,%f",&a,&b);
x=a+b;
printf("The sum is: %f\n\n\n",x);
main();
}
 
void subtraction()
{
float a=0;
float b=0;
float x=0;
 
printf("Enter two numbers separated by commas to be subtracted:");
scanf("%f,%f",&a,&b);
x=a-b;
printf("The difference is: %f\n\n\n",x);
main();
}
 
void multiplication()
{
float a=0;
float b=0;
float x=0;
 
printf("Enter two numbers separated by commas to be multiplied:");
scanf("%f,%f",&a,&b);
x=a*b;
printf("The product is: %f\n\n\n",x);
main();
}
void division()
{
float a=0;
float b=0;
float x=0;
 
printf("Enter two numbers separated by commas to be divided:");
scanf("%f,%f",&a,&b);
x=a/b;
printf("The quotient is: %f\n\n\n",x);
main();
}
 
 
 
void expon()
{
int a=0;
int b=0;
int x=1;
int index;
printf("Enter two numbers separated by commas( in a^b format):");
scanf("%d,%d",&a,&b);
for(index=1; index<=b; index++)
{x*=a;
}
printf("The answer is: %d\n\n\n",x);
main();
}
 
 
void mod()
{
    int num=0;
    printf("Enter a number, to see if it is odd or even:");
    scanf("%d", &num);
    if ( num % 2 == 0 )
    {
        printf("Your number is Even\n\n\n ",num);
    }
        if ( num % 2 != 0 )
    {
        printf("Your number is Odd\n\n\n ",num);
    }
 
    main();
}

The multi file, class-based program in C++(with add.h and sub.h included for the add and subtraction functions)

#include<iostream>
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#include"add.h"
#include"sub.h"
class math{
 public:
       math();
 
void addition();
void subtraction();
void multiplication();
void division();
void expon();
void mod();
 
};
int main()
{
void addition();
void subtraction();
void multiplication();
void division();
void expon();
void mod();
 
int temp = 0;
int function = 0;
 
    while (function == 0)
    {
        printf("What math function would you like to do (hit 0 to exit):\n");
        printf("1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit\n");
        printf(":");
        scanf("%d", &temp);
                  if(temp > 6 || temp < 0)
                { printf("You need to pick a number between 1 and 4!!\n");
                function=0;
                }
                else
                  {
                  function = temp;
                        }
        if(temp==0)
                {function = 9;
                }
    }
    if (function == 1)
    {
        addition();
    }
    if (function == 2)
    {
       subtraction();
    }
    if (function == 3)
     {
        multiplication();
    }
    if (function == 4)
    {
        division();
    }
    if (function == 5)
    {
        expon();
    }
    if (function == 6)
    {
        mod();
    }
 
    return(0);
}
 
void multiplication()
{
float a=0;
float b=0;
float x=0;
 
printf("Enter two numbers separated by commas to be multiplied:");
scanf("%f,%f",&a,&b);
x=a*b;
printf("The product is: %f\n\n\n",x);
main();
}
 
void division()
{
float a=0;
float b=0;
float x=0;
 
printf("Enter two numbers separated by commas to be divided:");
scanf("%f,%f",&a,&b);
x=a/b;
printf("The quotient is: %f\n\n\n",x);
main();
}
 
 
 
void expon()
{
int a=0;
int b=0;
int x=1;
int index;
printf("Enter two numbers separated by commas( in a^b format):");
scanf("%d,%d",&a,&b);
for(index=1; index<=b; index++)
{x*=a;
}
printf("The answer is: %d\n\n\n",x);
main();
}
 
 
void mod()
{
    int num=0;
    printf("Enter a number, to see if it is odd or even:");
    scanf("%d", &num);
    if ( num % 2 == 0 )
    {
        printf("Your number is Even\n\n\n ",num);
    }
        if ( num % 2 != 0 )
    {
        printf("Your number is Odd\n\n\n ",num);
    }
 
    main();
}

Execution

An example run of the monolithic c code:

lab46:~/src/cprog$ gcc -o project3 project3.c
lab46:~/src/cprog$ ./project3
What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:1
Enter two numbers separated by commas to be added:20,10
The sum is: 30.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:2
Enter two numbers separated by commas to be subtracted:34,12
The difference is: 22.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:3
Enter two numbers separated by commas to be multiplied:6,7
The product is: 42.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:4
Enter two numbers separated by commas to be divided:45,3
The quotient is: 15.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:5
Enter two numbers separated by commas( in a^b format):23,4
The answer is: 279841


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:6
Enter a number, to see if it is odd or even:23345
Your number is Odd


 What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:0
lab46:~/src/cprog$

An example run of the multi file c code(with add.h and sub.h included for the add and subtract function)

lab46:~/src/cprog$ gcc -o main main.c
lab46:~/src/cprog$ ./main
What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:1
Enter two numbers separated by commas to be added:4432,46344
The sum is: 50776.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:2
Enter two numbers separated by commas to be subtracted:425,322
The difference is: 103.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:3
Enter two numbers separated by commas to be multiplied:456,32
The product is: 14592.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:4
Enter two numbers separated by commas to be divided:45,3
The quotient is: 15.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:5
Enter two numbers separated by commas( in a^b format):45,4
The answer is: 4100625


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:6
Enter a number, to see if it is odd or even:345533442
Your number is Even


 What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:0
lab46:~/src/cprog$

An example run of the C++ class based monolithic program.

lab46:~/src/cprog$ g++ -o project3++ project3++.cc
lab46:~/src/cprog$ ./project3++
What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:1
Enter two numbers separated by commas to be added:7,8
The sum is: 15.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:2
Enter two numbers separated by commas to be subtracted:90,21
The difference is: 69.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:3
Enter two numbers separated by commas to be multiplied:7,7
The product is: 49.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:4
Enter two numbers separated by commas to be divided:67,4
The quotient is: 16.750000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:5
Enter two numbers separated by commas( in a^b format):56,2
The answer is: 3136


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:6
Enter a number, to see if it is odd or even:56373
Your number is Odd


 What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:0
lab46:~/src/cprog$

An example run of the multi file class- based program in C++(with add.h and sub.h included for the add and subtraction functions)

lab46:~/src/cprog$ g++ -o main++ main++.cc
lab46:~/src/cprog$ ./main++
What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:1
Enter two numbers separated by commas to be added:4,5
The sum is: 9.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:2
Enter two numbers separated by commas to be subtracted:77,54
The difference is: 23.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:3
Enter two numbers separated by commas to be multiplied:43,65
The product is: 2795.000000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:4
Enter two numbers separated by commas to be divided:45,4
The quotient is: 11.250000


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:5
Enter two numbers separated by commas( in a^b format):6,5
The answer is: 7776


What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:6
Enter a number, to see if it is odd or even:123456789
Your number is Odd


 What math function would you like to do (hit 0 to exit):
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division, 5:Exponentiation 6:Modulus 0:Exit
:0
lab46:~/src/cprog$

Reflection

this project took a long time to do, and was at times very annoying trying to get the math functions to work. I learned how to make a multi file program, and how to implement math functions, and making header files.

References

In performing this project, the following resources were referenced:

  • class
  • teacher
  • wiki
user/dmckinn2/portfolio/cprogproject3.txt · Last modified: 2012/04/06 21:55 by dmckinn2