| | |
Big Numbers Problem
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 19
Reputation:
Solved Threads: 0
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
unfortunately its not workin right,
so please any help
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)
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
él¡é
2
#5 27 Days Ago
Hello.
The troubles are starting here:
You carry can be more than 1 .. so you should take the integer part of your holder:
Now, after summing all the numbers - you can still have something in your carry variable .. and shouldn't waste it:
And one more mistake, or rather typo:
You just were increasing
The troubles are starting here:
c# Syntax (Toggle Plain Text)
carry = 1;
c# Syntax (Toggle Plain Text)
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)
if (carry != 0) { sum[0] = carry; }
And one more mistake, or rather typo:
c# Syntax (Toggle Plain Text)
for(int i=0;i<sumsize;i++)
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
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
•
•
Join Date: Apr 2009
Posts: 19
Reputation:
Solved Threads: 0
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
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¡é
1
#7 27 Days Ago
I understood you.
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:
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:
•
•
•
•
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.
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
c# Syntax (Toggle Plain Text)
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
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
•
•
Join Date: Apr 2009
Posts: 19
Reputation:
Solved Threads: 0
0
#8 27 Days Ago
i updated the sum operation to this
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
C# Syntax (Toggle Plain Text)
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(); } } }
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¡é
•
•
Join Date: Apr 2009
Posts: 19
Reputation:
Solved Threads: 0
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
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¡é
![]() |
Other Threads in the C# Forum
- Previous Thread: What is designer.cs?
- Next Thread: Seperating UI code
| Thread Tools | Search this Thread |
.net access activedirectory ado.net algorithm array barchart bitmap box broadcast c# check checkbox client combobox contorl control conversion csharp custom database datagrid datagridview dataset datetime degrees deployment development disabled displayingopenforms draganddrop drawing editing editor encryption enum event excel file form format forms ftp function gdi+ httpwebrequest i18n image imageprocessing index index-error input install java label list listbox mandelbrot math mathematics mouseclick mysql operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remoting richtextbox rows server setup sleep socket sql statistics stream string table text textbox thread time timer update user usercontrol validation visualstudio webbrowser windows winforms wpf xml






