i'm really having a problem on how to start my homework.

this is regarding to my first post (http://www.daniweb.com/forums/thread132760.html)
thanks to the one who answered, i realized that i needed to use arrays on this. i realized that i needed to solve the problems manually, meaning that i have to do operations in longhand.

the problem is i really don't know how i can code the array arithmetic part(+, -, /, *, sqrt)
the only thing i managed to do first is how i can input.

this is the part that i have started yet. i believe its still just a little 1% of the total program here.

#include <stdio.h>

int main(void)
{
    int array1[41], array2[41], counter;
    char oper;
    
    printf("This program is a manual calculator.\n");
    printf("It operates addition, subtraction, multiplication, division, and square root.\n");
    printf("Use symbols (+, -, *, /, #) to add, subtract, multiply, divide, and to find square root of a number.\n");
    
    printf("Enter first number(up to 40 digits): \n");
    gets()
    printf("Enter operation (+, -, *, /, #): \n");
    scanf("%c", &oper);
    switch (oper)
    {
           case '+':
                {
                    printf("Enter second number (up to 40 digits): \n");
                    gets()

please, anyone help me how i'll get started? thanks very much!

Member Avatar for iamthwee

In order to get in accurate to 40 digits I suspect you have to create your own datatypes.

In order to get in accurate to 40 digits I suspect you have to create your own datatypes.

how can i create my own data type? thanks

i just realized that i can't create my own data type. this is because the output must have the non-significant zeroes but not the leading zeroes truncated.

e.g., if the answer is 0.000753890000, then the output must be 0.00075389

i am on programmer's mental block.
i already know how to do but i can't put it into code.

for example, you have input two numbers
num1 = 123456.12
num2 = 234567.23

therefore, the sum should be something like this

1x10^5 + 2x10^5
2x10^4 + 3x10^4
3x10^3 + 4x10^3
4x10^2 + 5x10^2
5x10^1 + 6x10^1
6x10^0 + 7x10^0
1x10^-1 + 2x10^-1
2x10^-2 + 3x10^-2

if i need to subtract then i just had to change the + with a -.
i still don't have an idea on how to multiply and divide and find the square root of the number.

can anyone please help me get started on my program? thanks a lot

Member Avatar for iamthwee

I would approach this as such:

Get a piece of paper and go through the step you need to do long muliplication, long division, addition and subtraction.

You need to think about carries, for example 10 + 1 is 11, so you put a 1 at the end and then carry over the 1. etc.

here is a revised code of what i've done so far. i don't know if it's right. It compiles but it does not do anything right.

#include <stdio.h>
#define MAXDIG 40
#define DIGITS "01"
#define BASE 10

int main(void)
{
    char oper;
    char input1[MAXDIG + 1];
    char input2[MAXDIG + 1];
    char digits[] = DIGITS;
    int o;
    unsigned int value1, value2, index, digit;
    
    printf("This program is a manual calculator.\n");
    printf("It operates addition, subtraction, multiplication, division, and square root.\n");
    printf("Use symbols (+, -, *, /, #) to add, subtract, multiply, divide,\n");
    printf("and to find square root of a number.\n");
    
    printf("Enter first number(up to 40 digits):");
    gets(input1);
    printf("Enter operation (+, -, *, /, #): \n");
    scanf("%c",&oper);
    printf("Enter second number (up to 40 digits):");
    gets(input2);
    
    for (value1 = index = 0; input1[index] != '\0'; index ++)
		{
			for (digit = 0; input1[index] != digits[digit]; digit ++)
				if (digits[digit] == '\0') break;
			if (digits[digit] == '\0') break;
			value1 = (value1 * BASE) + digit;
		}
    for (value2 = index = 0; input2[index] != '\0'; index ++)
        {
            for (digit = 0; input2[index] != digits[digit]; digit ++)
                if (digits[digit] == '\0') break;
            if (digits[digit] == '\0') break;
            value2 = (value2 * BASE) + digit;
        }
    switch(oper)
        {case '+':printf("Sum = %i" ,(value1+value2));}
       
    scanf("%i" ,&o);
    return 0;
}

can anyone tell me what i've done wrong? i'm still on the addition part.

I would approach this as such:

Get a piece of paper and go through the step you need to do long muliplication, long division, addition and subtraction.

You need to think about carries, for example 10 + 1 is 11, so you put a 1 at the end and then carry over the 1. etc.

i realized this is the more perfect approach. thanks very much

First, I would change the variable "int o;" to some other name. Never use "o" or "O" for variable names, because they look too much like 0.

Second, in your description of the problem, you are moving in the wrong direction. You start with the *smallest* column's number, and work up to the *biggest* column numbers. That way you know how to manage the carries.

Third, I would initiate your arrays with all zero's, and that's you're starting place as you add up your numbers.

It looks to me like using char's is confusing you. Switch your arrays to int's, instead. For simple adding, I don't see why you need nested for loops. Not at all.

while(digit >= 0)   {
   j = i; 
   num1 = number1[i];
   num2 = number2[i++];
   digit = num1 + num2;

          //"carry" could very well be it's own little function
   if(digit > 9) {  //it has a carry
      j++;
      number1[j]++;
carry:
;
      while(number[j++] > 9)   {
         number[j]++;
         goto carry;
      }
   }
  i++;
}

The above is all untested, and just for a springboard for your own code. It's not probably ready to compile & run. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.