• Pada Algoritma pencarian banyak yang bisa kita temukan atau gunakan. Ada Merge Sort, Quick Sort, Bubble Sort, dan...
  • Pada dasarnya algoritma searching banyak kita jumpai. Apalagi hanya untuk mencari nilai minimum dan nilai maximum...
  • Kalian pasti sudah tahu apa itu Deret Fibonacci, ya benar. (Padahal gak jawab). Tetapi pada pembahasan kali ini kita akan membuar program...
  • Metoda Pencarian Biner ( Binary Search) hanya bisa diterapkan jika data array sudah terurut. Pengurutan Array bisa menggunakan jenis sorting ...
  • Salah satu contoh tipe algoritma brute force lainnya adalah linear search (pencarian berurutan), Dikatakan demikian karena algoritma ini menggunakan ...

Friday, May 13, 2011

Big Integer : Many Digit Addition with Integer Data Type (Penjumlahan Banyak Digit dengan Tipe Data Integer)

You all know that the numbers are integer data types must be limited to 10 digits only. And what if kitaa want to add a lot of digits. Suppose we have a 30-digit numbers. Surely if we create a program using only integer types aka error results. Therefore, we can use arrays. Each digit aka stored in a single array element. Example :

We take the first number 57575757, the second number 12345678
In the program number will be stored in each array element.

The first number
5
7
5
7
5
7
5
7


The second number
1
2
3
4
5
6
7
8

 

Having obtained such that every sum of digits performed live. But here happened the outcome of the sum is also stored role in every element of the array. As the above example will occur 7 + 8 = 15. Number 15 aka the last element stored in the array. So we must be smart - smart play with the computer. We use modulo to get the number 5. So that is not stored in the memory array 15. The next smaller digit we add 1. Because 10 of 15 had been added in front of him. As usual summation in primary schools.

Based on that logic could be a program that can count up the number of big integers. That is the number of type integer, but has plenty of appropriate digits we want.

Writing can be made ​​like this :
void aritmatika::penjumlahan(){
     for(int i=(digit-1);i>=0;i--){
             jumlah[i]=jumlah[i]+input1[i]+input2[i];
             if (jumlah[i] >9 && i !=0){
                               jumlah[i]=jumlah[i]%10;
                               jumlah[i-1]=jumlah[i-1]+1;
                               }
             }
     }

Easy right opponent? Next let us work. For all the code themselves can be downloaded here

Jadilah yang pertama mengomentari

Post a Comment

Tutorial Algorithm and Programming ©Template Blogger Green by Dicas Blogger.

To Up