User Tools

Site Tools


user:mowens3:portfolio:cprogproject2

Project: BIG NUM

A project for COURSENAME by YOUR NAME during the SEMESTER YEAR.

This project was begun on DATE and is anticipated to take TIME UNIT to complete. Project was completed on MONTH DAY, YEAR.

Objectives

State the purpose of this project. What is the point of this project? What do we hope to accomplish by undertaking it?

In this project we are using arrays to an extreme and trying to understand how powerful arrays can be.

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

State the idea or purpose of the project. What are you attempting to pursue?

Upon approval, 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

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

The multiplication and division don't work, I can't figure them out at all.

/*
I need to make a selection statment with a function in each one
*/
#include<stdio.h>
#include<stdlib.h>
 
/*
function prototypes here
*/
 
void addition();
void subtraction();
void multiplication();
void division();
char selection = 0;
char correct = 0;
char junk;
int main()
{
	while (correct == 0)
	{
		printf("What would you like to do;\n");
		printf("1: Addition, 2: Subtraction, 3: Multiplication, 4:Division 5: to quit: ");
		scanf("%c", &selection);
		scanf("%c", &junk);
// check for valid input
		if ((selection < '1') && (selection > '5'))
		{
			printf("Invalid selection try again \n");
		}
		else if (selection == '5')
		{
			printf("Thank you, goodbye \n");
			exit(0);
		}
		else if ((selection >= '1') || (selection <= '5'))
		{
			correct = 1;
		}
	}
	if (selection == '1')
	{
		addition();
	}
	if (selection == '2')
	{
		subtraction();
	}
	if (selection == '3')
	{
		multiplication();
	}
	if (selection == '4')
	{
		division();
	}
 
	return(0);
}
 
void addition()
{
	int spaces = 0;
	char carry = 0;
	int firstvar = 0;
	int secondvar = 0;
	int holder = 0;
	int counter1 = 0;
	int counter2 = 0;
	int endcounter = 0;
	printf("What is the maximum number of spaces you need for this addition: ");
	scanf("%d", &spaces);
	printf("You put %d\n", spaces);
	char *variable1, *variable2, *variableholder;
	variable1 = (char *)malloc(sizeof(char)* spaces);
	variable2 = (char *)malloc(sizeof(char)* spaces);
	variableholder = (char *)malloc(sizeof(char)* spaces);
	endcounter = spaces - 1;
	printf("Please enter the first number: ");
	holder = getchar();
	holder = getchar();
//storing them into a temp holder below
	while (holder != '\n')
	{
		*(variableholder + firstvar) = holder;
		firstvar = firstvar + 1;
		holder = getchar();
	}
//pulling them out of the temp holder
	counter1 = 0;
	firstvar = firstvar - 1;
	while (firstvar >= 0)
	{
		*(variable1+((spaces-1)-counter1)) = (*(variableholder+firstvar)-48);
		firstvar = firstvar - 1;
		counter1 = counter1 + 1;
	}
	printf("Please enter the second number: ");
	holder = getchar();
	while (holder != '\n')
	{
		*(variableholder + secondvar) = holder;
		secondvar = secondvar + 1;
		holder = getchar();
	}
	counter2 = 0;
	secondvar = secondvar - 1;
	while (secondvar >= 0)
	{
		*(variable2 + ((spaces -1) - counter2)) = (*(variableholder + secondvar) - 48);
		secondvar = secondvar - 1;
		counter2 = counter2 + 1;
	}
//Adding the variables
	printf("Adding the variables now\n");
	counter1 = counter1 - 1;
	counter2 = counter2 - 1;
	while((counter1 >= 0) || (counter2 >=0))
	{
		if (counter2 < 0)
		{
			*(variableholder + endcounter) = *(variable1 + endcounter) + carry;
			counter1 = counter1 - 1;
			carry = 0;
		}
		else if (counter1 < 0)
		{
			*(variableholder + endcounter) = *(variable2 + endcounter) + carry;
			counter2 = counter2 - 1;
			carry = 0;
		}
		else
		{
			holder = *(variable1 + endcounter) + *(variable2 + endcounter);
			if (holder >= 10)
			{
				holder = holder - 10;
				*(variableholder + endcounter) = holder + carry;
				carry = 1;
			}
			else
			{
				*(variableholder + endcounter) = holder + carry;
				carry = 0;
			}
			counter1 = counter1 - 1;
			counter2 = counter2 - 1;
		}
		endcounter = endcounter - 1;
	}
	if (carry > 0)
	{
		printf("%c", carry+48);
	}
	for (holder = 0; holder < spaces; holder ++)
	{
		printf("%c", *(variableholder + holder)+48);
	}
	printf("\n");
	printf("Done\n");
}
 
void subtraction()
{
	int spaces = 0;
	char carry = 0;
	int firstvar = 0;
	int secondvar = 0;
	signed int holder = 0;
	int counter1 = 0;
	int counter2 = 0;
	int endcounter = 0;
	char resultnegitive = 0; //Used to print a - sign
	printf("What is the maximum number of spaces you need for this addition: ");
	scanf("%d", &spaces);
	printf("You put %d\n", spaces);
	char *variable1, *variable2, *variableholder;
	variable1 = (char *)malloc(sizeof(char)* spaces);
	variable2 = (char *)malloc(sizeof(char)* spaces);
	variableholder = (char *)malloc(sizeof(char)* spaces);
	endcounter = spaces - 1;
	printf("Please enter the first number: ");
	holder = getchar();
	holder = getchar();
//storing them into a temp holder below
	while (holder != '\n')
	{
		*(variableholder + firstvar) = holder;
		firstvar = firstvar + 1;
		holder = getchar();
	}
//pulling them out of the temp holder
	counter1 = 0;
	firstvar = firstvar - 1;
	while (firstvar >= 0)
	{
		*(variable1+((spaces-1)-counter1)) = (*(variableholder+firstvar)-48);
		firstvar = firstvar - 1;
		counter1 = counter1 + 1;
	}
	printf("Please enter the second number: ");
	holder = getchar();
	while (holder != '\n')
	{
		*(variableholder + secondvar) = holder;
		secondvar = secondvar + 1;
		holder = getchar();
	}
	counter2 = 0;
	secondvar = secondvar - 1;
	while (secondvar >= 0)
	{
		*(variable2 + ((spaces -1) - counter2)) = (*(variableholder + secondvar) - 48);
		secondvar = secondvar - 1;
		counter2 = counter2 + 1;
	}
//Subtracting the variables
	printf("Subtracting the variables now\n");
	counter1 = counter1 - 1;
	counter2 = counter2 - 1;
	while((counter1 >= 0) || (counter2 >=0))
	{
		if (carry == 1)
		{
			*(variable1 + endcounter) = *(variable1 + endcounter) - carry;
			carry = 0;
		}
		if (counter2 < 0)
		{
			*(variableholder + endcounter) = *(variable1 + endcounter);
			counter1 = counter1 - 1;
			carry = 0;
		}
		else if (counter1 < 0)
		{
			*(variableholder + endcounter) = *(variable2 + endcounter);
			counter2 = counter2 - 1;
			carry = 0;
			resultnegitive = 1;
		}
		else
		{
			holder = *(variable1 + endcounter) - *(variable2 + endcounter);
			if (holder < 0)
			{
				holder = holder + 10;
				*(variableholder + endcounter) = holder;
				carry = 1;
			}
			else
			{
				*(variableholder + endcounter) = holder;
				carry = 0;
			}
			counter1 = counter1 - 1;
			counter2 = counter2 - 1;
		}
		endcounter = endcounter - 1;
	}
	if (resultnegitive == 1)
	{
		printf("-");
	}
	for (holder = 0; holder < spaces; holder ++)
	{
		printf("%c", *(variableholder + holder)+48);
	}
	printf("\n");
	printf("Done\n");
}
void multiplication()
{
	int spaces = 0;
	char carry = 0;
	int firstvar = 0;
	int secondvar = 0;
	int holder = 0;
	int counter1 = 0;
	int counter2 = 0;
	int counter3 = 0; //used to reset counter1 later in code
	int loopnumber = 0;
	int endcounter = 0;
	printf("What is the maximum number of spaces you need for this multiplication: ");
	scanf("%d", &spaces);
	printf("You put %d\n", spaces);
	char *variable1, *variable2, *variableholder;
	variable1 = (char *)malloc(sizeof(char)* spaces);
	variable2 = (char *)malloc(sizeof(char)* spaces);
	variableholder = (char *)malloc(sizeof(char)* spaces);
	endcounter = spaces - 1;
	printf("Please enter the first number: ");
	holder = getchar();
	holder = getchar();
//storing them into a temp holder below
	while (holder != '\n')
	{
		*(variableholder + firstvar) = holder;
		firstvar = firstvar + 1;
		holder = getchar();
	}
//pulling them out of the temp holder
	counter1 = 0;
	firstvar = firstvar - 1;
	while (firstvar >= 0)
	{
		*(variable1+((spaces-1)-counter1)) = (*(variableholder+firstvar)-48);
		firstvar = firstvar - 1;
		counter1 = counter1 + 1;
	}
	printf("Please enter the second number: ");
	holder = getchar();
	while (holder != '\n')
	{
		*(variableholder + secondvar) = holder;
		secondvar = secondvar + 1;
		holder = getchar();
	}
	counter2 = 0;
	secondvar = secondvar - 1;
	while (secondvar >= 0)
	{
		*(variable2 + ((spaces -1) - counter2)) = (*(variableholder + secondvar) - 48);
		secondvar = secondvar - 1;
		counter2 = counter2 + 1;
	}
//Multiplying the variables
	printf("Multiplying the variables now\n");
	counter1 = counter1 - 1;
	counter2 = counter2 - 1;
	counter3 = counter1;
	while (counter1 >= 0)
	{
		*(variableholder + counter1) = 0;
		counter1 = counter1 - 1;
	}
	counter1 = counter3;
	while(counter2 >=0)
	{
		holder = *(variable1 + counter1) * *(variable2 + counter2);
		if ((counter1 != 0) && (counter2 != 0))
		{
 			*(variableholder + endcounter) = (holder % 10) + *(variableholder + endcounter - loopnumber) + carry;
			carry = holder / 10;
		}
		else
		{
			*(variableholder + endcounter) = (holder) + *(variableholder + endcounter - loopnumber) + carry;
		}
		if (counter1 == 0)
		{
			*(variableholder + counter1) = *(variableholder + counter1) + carry;
		}
		if (counter1 > 0)
		{
			counter1 = counter1 - 1;
		}
		else
		{
			counter2 = counter2 - 1;
			counter1 = counter3;
			loopnumber = loopnumber + 1;
		}
	} 
	for (holder = 0; holder < spaces; holder ++)
	{
		printf("%c", *(variableholder + holder)+48);
	}
	printf("\n");
	printf("Done\n");
}
 
void division()
{
	int spaces = 0;
	char carry = 0;
	printf("What is the maximum number of spaces you need for this division: ");
	scanf("%d", &spaces);
	printf("You put %d\n", spaces);
}

Execution

Here is an example of the Addition running.

lab46:~/src/cprog/projects.folder/project2$ ./project2
What would you like to do;
1: Addition, 2: Subtraction, 3: Multiplication, 4:Division 5: to quit: 1
What is the maximum number of spaces you need for this addition: 3
You put 3
Please enter the first number: 123
Please enter the second number: 23
Adding the variables now
146
Done

Reflection

I learned that messing with arrays like this can be a lot of typing and thoughts. I don't like all of this work, but its doable. I kinda wish we didn't have to do the multiplication and division though, I can not think of how to do it AT ALL. It is making me feel bad.

References

In performing this project, the following resources were referenced: I used what we were taught in class and my own general knowledge, plus some help from the awesome teacher Matt.

user/mowens3/portfolio/cprogproject2.txt · Last modified: 2012/03/16 17:25 by mowens3