hello my friends I need a little help over here :
im workin on a calculator that should provide the user with these services:
• Read a big integer number up to 300 ( use arrays ) .
• Sum two big integer numbers (with using the carry).
• Product two big integer numbers (with using the carry).
• Write a big Integer number.

so this is wat i did

class Program
   {
      static void Main(string[] args)
      {
         //inputting the two numbers .
         string number1, number2;
         int sumsize, p;
         number1 = Console.ReadLine();
         number2 = Console.ReadLine();
         //modifying the numbers to be from the same length .
         /*i added one to the sum array size(sumsize) to avoid 
           the over flow */
         if (number1.Length > number2.Length)
         {
            number2 = number2.PadLeft(number1.Length, '0');
            sumsize = number1.Length+1;
            p = number1.Length;
         }
         else
         {
            number1 = number1.PadLeft(number2.Length, '0');
            sumsize = number2.Length+1;
            p = number2.Length;
         }
         //declaring the two arrays and filling them with characters .
        /* the input is string so when converting it to int i found that 
            i should subtract 48 to get the right result */
         int[] arr1 = new int[number1.Length];
         for (int i = 0; i < number1.Length; i++)
            arr1[i] = Convert.ToInt32(number1[i] - 48);
         int[] arr2 = new int[number2.Length];
         for (int i = 0; i < number2.Length; i++)
            arr2[i] = Convert.ToInt32(number2[i] - 48);
         //displaying the two arrays .
         Console.WriteLine("\n1ST.Number is: \n");
         for (int i = 0; i < number1.Length; i++)
            Console.Write(arr1[i]);
         Console.WriteLine("\n\n2ND.Number is: \n");
         for (int i = 0; i < number2.Length;    i++)
            Console.Write(arr2[i]);
         Console.WriteLine();
         // till here everything is workin properly .. then -->
         int[] sum = new int[sumsize];
         int carry = 0;
         int holder;
        // the sum 
         for (int i = p-1; i >=0; i--)
         {
            holder=arr1[i] + arr2[i] + carry;
            if (holder < 10)
            {
               sum[sumsize-1] = arr1[i] + arr2[i] + carry;
               carry = 0;
            }
            else
            {
               sum[sumsize-1] = holder % 10;
               carry = 1;

            }
            sumsize--;
         }
         Console.WriteLine("sum is=");
         for(int i=0;i<sumsize;i++)
            Console.Write(sum[i]);
      }
   }
}

unfortunately its not workin right,
so please any help

btw i didnt post the product yet because i want to fix the sum first
and to be more clear , the method is the normal one
I mean something like this :
(1)<--(carry)
9 9
2 3 +
______
122

What is the part you are having trouble with? its not workin right is a bit vague as a question.

lines from 46->65 ( the sum operations )
something is wrong and it is in the size of the sum array
if u tried to put the size -> ( i ) in 52 and 57 it will work
but the over flow will still there
i need to get rid of it

Hello.
The troubles are starting here:

carry = 1;

You carry can be more than 1 .. so you should take the integer part of your holder:

carry = holder / 10;

Now, after summing all the numbers - you can still have something in your carry variable .. and shouldn't waste it:

if (carry != 0)
            {
                sum[0] = carry;
            }

And one more mistake, or rather typo:

for(int i=0;i<sumsize;i++)

You just were increasing sumsize in cycle... So it won't be the actual size of the sum.

commented: excellent +6

well my friend Antenka, thank you so much for helping but ...
about the carry you are right , i forgot to type it here , but i used it in my program as holder/10 . but the mistake is not here .. ..
what im trying to do is to make an array to hold the sum of the two integers ( from the different dynamic arrays )
so i named it as ( sum ) and i made its size as ( sumsize ) which is the length of (the longer number + 1) , i did that because of the over flow
or the carry as u said ,
so in the loop i set the size of sum as ( sumsize -1 ) because in adding we add from right to left and the first added number will located in the most significant location of the array
and then at the end of the loop i decrement the sumzie to move left to the next location of the array, and because that the sum array is bigger that the longer number by ( 1 ) location , there will be a place to the carry at the least significant location.
and about the last point ( 1.
for(int i=0;i<sumsize;i++)
)i cant see anything wrong in it
i did this because i want to display the elements of the sum array which is still wrong :S

I understood you.

so in the loop i set the size of sum as ( sumsize -1 ) because in adding we add from right to left and the first added number will located in the most significant location of the array
and then at the end of the loop i decrement the sumzie to move left to the next location of the array, and because that the sum array is bigger that the longer number by ( 1 ) location , there will be a place to the carry at the least significant location.

That's pretty clear, but also, from your words: you go through arrays and their length is 1 less than sum array has. So the first element of this array would never be changed. So, that's why I gave you a piece of code, that assigns that first element (if necessary).

About this:

about the last point ( 1.
for(int i=0;i<sumsize;i++)
)i cant see anything wrong in it
i did this because i want to display the elements of the sum array which is still wrong

after previous cycle (where you sum your numbers) the value of sumsize would be 1 (in any case). So you would should do something like this:

for (int i = 0; i < sum.Length; i++)

i updated the sum operation to this

int u = p + 1;
         int[] sum = new int[u];
         int carry = 0;
         int holder;
         for (int i = p - 1; i >= 0; i--)
         {
            u -= 1;
            holder = arr1[i] + arr2[i] + carry;
            if (holder < 10)
            {
               sum[u] = arr1[i] + arr2[i] + carry;
               carry = 0;

            }
            else
            {
               sum[u] += holder % 10;
               carry = holder / 10;

            }

         }
            if (carry != 0)
               sum[0] = carry;   
         Console.WriteLine("sum is=");
         for (int i = 0; i < sum.Length; i++)
            Console.Write(sum[i]);
         Console.WriteLine();
      }
   }
}

and its working properly
dear Antenka you're my hero :)
ok now I will work on the product operation and inform u with the result
thank you my friend :)

:) you're welcome. Good luck with your project!

my friend can anyone suggest some ideas for me about doing the product
im workin on it .. i did some nested loops .. still workin
I wish if anyone could assist me
i also used some 2-D arrays
it should be like this :
carry----------->1-1
0 0 0 0 0 0 0 0 2 8 7 4
0 0 0 0 0 0 0 0 0 1 1 2
__________________
carry------>1-2-1
0 0 0 0 0 0 0 0 5 7 4 8
0 0 0 0 0 0 0 2 8 7 4 0
0 0 0 0 0 0 2 8 7 4 0 0
__________________
0 0 0 0 0 0 3 2 1 8 8 8
thank you

Ok, let's take an example:
137*1456

We can factorize it:
137*(1000 + 400 + 50 + 6)

To see the connection, we can convert this in form like this:
137*(1*10^3 + 4*10^2 + 5*10^1 + 6*10^0)
(where ^ means powering)

So, depending on the position of the digit in the number, we can represent it in such view (see above).

so, what we have:
137*1456=137*1*10^3 + 137*4*10^2 + 137*5*10^1 + 137*6*10^0

All you have to do now - is extract algorithm from this and code it :)

commented: Clear explanation:) +5

dear Antenka i forgot to check for new replies :S but anyway thank you so much for your efforts i am done , i solved it by multiplying the two arrays and putting the results in two D dimensional arrays
then adding each column elements and puttin the result in anew array
its workin properly
thank you anyway again :) :*

:) You're welcome and good luck with your studies!

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.