943,936 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1013
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 30th, 2009
0

Big Numbers Problem

Expand Post »
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
C# Syntax (Toggle Plain Text)
  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
Reputation Points: 37
Solved Threads: 0
Light Poster
Alicito is offline Offline
33 posts
since Apr 2009
Oct 30th, 2009
0
Re: Big Numbers Problem
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; Oct 30th, 2009 at 6:12 am.
Reputation Points: 37
Solved Threads: 0
Light Poster
Alicito is offline Offline
33 posts
since Apr 2009
Oct 30th, 2009
0
Re: Big Numbers Problem
What is the part you are having trouble with? its not workin right is a bit vague as a question.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 30th, 2009
0
Re: Big Numbers Problem
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
Reputation Points: 37
Solved Threads: 0
Light Poster
Alicito is offline Offline
33 posts
since Apr 2009
Oct 30th, 2009
2
Re: Big Numbers Problem
Hello.
The troubles are starting here:
c# Syntax (Toggle Plain Text)
  1. carry = 1;
You carry can be more than 1 .. so you should take the integer part of your holder:
c# Syntax (Toggle Plain Text)
  1. carry = holder / 10;

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

And one more mistake, or rather typo:
c# Syntax (Toggle Plain Text)
  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.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Oct 30th, 2009
0
Re: Big Numbers Problem
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
Reputation Points: 37
Solved Threads: 0
Light Poster
Alicito is offline Offline
33 posts
since Apr 2009
Oct 30th, 2009
1
Re: Big Numbers Problem
I understood you.
Quote 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:
Quote 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:
c# Syntax (Toggle Plain Text)
  1. for (int i = 0; i < sum.Length; i++)
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Oct 30th, 2009
0
Re: Big Numbers Problem
i updated the sum operation to this
C# Syntax (Toggle Plain Text)
  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
Reputation Points: 37
Solved Threads: 0
Light Poster
Alicito is offline Offline
33 posts
since Apr 2009
Oct 30th, 2009
0
Re: Big Numbers Problem
you're welcome. Good luck with your project!
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Oct 30th, 2009
0
Re: Big Numbers Problem
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
Reputation Points: 37
Solved Threads: 0
Light Poster
Alicito is offline Offline
33 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: What is designer.cs?
Next Thread in C# Forum Timeline: Seperating UI code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC