Big Numbers Problem

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 19
Reputation: Alicito is an unknown quantity at this point 
Solved Threads: 0
Alicito Alicito is offline Offline
Newbie Poster

Big Numbers Problem

 
0
  #1
27 Days Ago
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
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. //inputting the two numbers .
  6. string number1, number2;
  7. int sumsize, p;
  8. number1 = Console.ReadLine();
  9. number2 = Console.ReadLine();
  10. //modifying the numbers to be from the same length .
  11. /*i added one to the sum array size(sumsize) to avoid
  12.   the over flow */
  13. if (number1.Length > number2.Length)
  14. {
  15. number2 = number2.PadLeft(number1.Length, '0');
  16. sumsize = number1.Length+1;
  17. p = number1.Length;
  18. }
  19. else
  20. {
  21. number1 = number1.PadLeft(number2.Length, '0');
  22. sumsize = number2.Length+1;
  23. p = number2.Length;
  24. }
  25. //declaring the two arrays and filling them with characters .
  26. /* the input is string so when converting it to int i found that
  27.   i should subtract 48 to get the right result */
  28. int[] arr1 = new int[number1.Length];
  29. for (int i = 0; i < number1.Length; i++)
  30. arr1[i] = Convert.ToInt32(number1[i] - 48);
  31. int[] arr2 = new int[number2.Length];
  32. for (int i = 0; i < number2.Length; i++)
  33. arr2[i] = Convert.ToInt32(number2[i] - 48);
  34. //displaying the two arrays .
  35. Console.WriteLine("\n1ST.Number is: \n");
  36. for (int i = 0; i < number1.Length; i++)
  37. Console.Write(arr1[i]);
  38. Console.WriteLine("\n\n2ND.Number is: \n");
  39. for (int i = 0; i < number2.Length; i++)
  40. Console.Write(arr2[i]);
  41. Console.WriteLine();
  42. // till here everything is workin properly .. then -->
  43. int[] sum = new int[sumsize];
  44. int carry = 0;
  45. int holder;
  46. // the sum
  47. for (int i = p-1; i >=0; i--)
  48. {
  49. holder=arr1[i] + arr2[i] + carry;
  50. if (holder < 10)
  51. {
  52. sum[sumsize-1] = arr1[i] + arr2[i] + carry;
  53. carry = 0;
  54. }
  55. else
  56. {
  57. sum[sumsize-1] = holder % 10;
  58. carry = 1;
  59.  
  60. }
  61. sumsize--;
  62. }
  63. Console.WriteLine("sum is=");
  64. for(int i=0;i<sumsize;i++)
  65. Console.Write(sum[i]);
  66. }
  67. }
  68. }

unfortunately its not workin right,
so please any help
él¡é
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 19
Reputation: Alicito is an unknown quantity at this point 
Solved Threads: 0
Alicito Alicito is offline Offline
Newbie Poster
 
0
  #2
27 Days Ago
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
Last edited by Alicito; 27 Days Ago at 6:12 am.
él¡é
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,909
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 274
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
0
  #3
27 Days Ago
What is the part you are having trouble with? its not workin right is a bit vague as a question.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 19
Reputation: Alicito is an unknown quantity at this point 
Solved Threads: 0
Alicito Alicito is offline Offline
Newbie Poster
 
0
  #4
27 Days Ago
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
él¡é
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training
 
2
  #5
27 Days Ago
Hello.
The troubles are starting here:
  1. carry = 1;
You carry can be more than 1 .. so you should take the integer part of your holder:
  1. carry = holder / 10;

Now, after summing all the numbers - you can still have something in your carry variable .. and shouldn't waste it:
  1. if (carry != 0)
  2. {
  3. sum[0] = carry;
  4. }

And one more mistake, or rather typo:
  1. 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.
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 19
Reputation: Alicito is an unknown quantity at this point 
Solved Threads: 0
Alicito Alicito is offline Offline
Newbie Poster
 
0
  #6
27 Days Ago
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
él¡é
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training
 
1
  #7
27 Days Ago
I understood you.
Originally Posted by Alicito
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:
Originally Posted by Alicito
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:
  1. for (int i = 0; i < sum.Length; i++)
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 19
Reputation: Alicito is an unknown quantity at this point 
Solved Threads: 0
Alicito Alicito is offline Offline
Newbie Poster
 
0
  #8
27 Days Ago
i updated the sum operation to this
  1. int u = p + 1;
  2. int[] sum = new int[u];
  3. int carry = 0;
  4. int holder;
  5. for (int i = p - 1; i >= 0; i--)
  6. {
  7. u -= 1;
  8. holder = arr1[i] + arr2[i] + carry;
  9. if (holder < 10)
  10. {
  11. sum[u] = arr1[i] + arr2[i] + carry;
  12. carry = 0;
  13.  
  14. }
  15. else
  16. {
  17. sum[u] += holder % 10;
  18. carry = holder / 10;
  19.  
  20. }
  21.  
  22. }
  23. if (carry != 0)
  24. sum[0] = carry;
  25. Console.WriteLine("sum is=");
  26. for (int i = 0; i < sum.Length; i++)
  27. Console.Write(sum[i]);
  28. Console.WriteLine();
  29. }
  30. }
  31. }
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
él¡é
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training
 
0
  #9
27 Days Ago
you're welcome. Good luck with your project!
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 19
Reputation: Alicito is an unknown quantity at this point 
Solved Threads: 0
Alicito Alicito is offline Offline
Newbie Poster
 
0
  #10
26 Days Ago
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
él¡é
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC