Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes sum of two int arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sum of two int arrays" Watch "sum of two int arrays" New topic
Author

sum of two int arrays

mike cool
Greenhorn

Joined: Oct 21, 2005
Posts: 24
hey all
how can i sum two int arrays into other int array
thx
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343


?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
What do you mean by "sum two int arrays"? Does this mean to sum each corresponding entry? This seems the logical choice since it mimics vector arithmetic from mathematics and physics. What if the two arrays are different lengths?

So the first step to answering "How do I..." questions is defining what the operations mean. In this case, a simple for loop as shown above may be sufficient. In fact, for loops often go hand-in-hand with manipulating arrays, so you should always keep them in mind.

Layne


Java API Documentation
The Java Tutorial
mike cool
Greenhorn

Joined: Oct 21, 2005
Posts: 24
hey i forgot to say
each element of the 2 arrays has only one digit
the 2 arrays have different sizes 30 and 40
and in the array where i will sum the 2 arrays each element must contain only one digit

it starts like this user will input string of numbers then this string will be added into char array then this char array wil be converted to int array , (this for each of the 2 arrays) then i need to compare them ...etc
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by mike cool:
hey i forgot to say
each element of the 2 arrays has only one digit
the 2 arrays have different sizes 30 and 40
and in the array where i will sum the 2 arrays each element must contain only one digit

it starts like this user will input string of numbers then this string will be added into char array then this char array wil be converted to int array , (this for each of the 2 arrays) then i need to compare them ...etc

That raises more questions...
  • How is the char array being converted to an int array? (Are the ints actually char values? Or are they int representations of the chars?)
  • When you add two ints, how exactly do you ensure that the result is only one digit? For example, what's 7 + 5?
  • mike cool
    Greenhorn

    Joined: Oct 21, 2005
    Posts: 24
    first i will convert the char to int with this function
    getNumericValue(char c);
    after we test if the string contain only numric characters
    then i have to add the 2 int arrays like this

    example
    int [] array1 = {2,5,9,4,7,2};
    int [] array2 = {8,3,4,0,5,7};
    the sum of the two arrays should be like this :
    arraySum = { 1,0,9,3,5,2,9};

    each digit in an element
    Seb Mathe
    Ranch Hand

    Joined: Sep 28, 2005
    Posts: 225
    OK. I don't think you have to pass by your getNumericValue method :

    assuming you have 2 char arrays :

    int i1 = Integer.parseInt(new String(array1));
    int i2 = Integer.parseInt(new String(array2));
    char[] result = String.valueOf(i1+i2).toCharArray();


    Regards,<br />Seb<br /> <br />SCJP 1.4
    mike cool
    Greenhorn

    Joined: Oct 21, 2005
    Posts: 24
    ok
    i can't convert string to in directly coz this numiric string can be up to 40 digits that's why i canverted first to char array
    Seb Mathe
    Ranch Hand

    Joined: Sep 28, 2005
    Posts: 225
    So I think that java.math.BigInteger will solve your problem.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Seb: Very elegant approach!
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    It sounds like the OP may be implementing his/her own BigInteger-type class. So, the basic idea is still the same, you need a for loop to go through the array. What do you do if the result is over 10? Think about what you do when you add two numbers by hand. You may want to use the % and / operators for parts of this.

    I hope this helps.

    Layne
    mike cool
    Greenhorn

    Joined: Oct 21, 2005
    Posts: 24
    ok here is it step by step

    1- creat two int array the size of each array must be 40
    2- ask the user to input 2 strings (big numbers )
    3- check each character of ths string if it's numeric characters
    4- add each string in one int array
    5-print out the sum of the 2 arrays
    6-compare between the two arrays.


    in step 1 i've created 2 int array with size 40 for each..
    instep 4 i will read 2 strings from the user i will convert each character
    of the string to int and add each digit to one elment of the int array

    what i did is the following
    1- read first string
    2- add this string to char array using toCharArray();
    3-check each character if it numeric charcter using isDigit(char c);
    4-if it's ok i convert this char character to int using getNumericValue (char c);
    5-add each character after i converted to one element of the first int array..
    so it means that each elment in the int array will contain one digit..
    6-do the same with the second string to the second int array.

    now each element of each int array contain one digit (step 5)
    1-how can i sum these 2 int array to new int array and this new int array
    the new int array must contain in each elment only one digit
    i can use %10 to do that but how can i add the remain to the next element is the sum is bigger than 9
    2- to sum i need to read from right to left ,,do i need to start add to the last index?
    3- the array size is 40 so if i use less elments the rest will contain zero's
    how can i remove these zero's when i print the result
    i have to use toString(); to print


    i have to compare between the first int and the second int
    not the array size but the number itself
    are they equal ?
    number 1 is greater than number 2
    number (is the all digit in the array as one number)
    Seb Mathe
    Ranch Hand

    Joined: Sep 28, 2005
    Posts: 225
    Mike,

    I don't understand why you don't want to use BigIntegers...

    With BigInteger, if s1 and s2 are the user inputs :

    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    Originally posted by Seb Mathe:
    Mike,

    I don't understand why you don't want to use BigIntegers...


    The OP isn't using BigIntegers because this probably is a school assignment to basically implement their own class that is similar. I think this is a GREAT learning exercise.

    Layne
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    Originally posted by mike cool:
    ok here is it step by step

    1- creat two int array the size of each array must be 40
    2- ask the user to input 2 strings (big numbers )
    3- check each character of ths string if it's numeric characters
    4- add each string in one int array
    5-print out the sum of the 2 arrays
    6-compare between the two arrays.


    in step 1 i've created 2 int array with size 40 for each..
    instep 4 i will read 2 strings from the user i will convert each character
    of the string to int and add each digit to one elment of the int array


    Are you sure that the user will only type in a maximum of 40 digits? Personally, I think you should wait to create the int array until after you read in the Strings and verify that all the characters are digits. That way you can create int arrays that are the size of the String that was entered. This will be much more flexible since the user is not limited to entering 40 digits or less.

    what i did is the following
    1- read first string
    2- add this string to char array using toCharArray();
    3-check each character if it numeric charcter using isDigit(char c);
    4-if it's ok i convert this char character to int using getNumericValue (char c);
    5-add each character after i converted to one element of the first int array..
    so it means that each elment in the int array will contain one digit..
    6-do the same with the second string to the second int array.

    now each element of each int array contain one digit (step 5)
    1-how can i sum these 2 int array to new int array and this new int array
    the new int array must contain in each elment only one digit
    i can use %10 to do that but how can i add the remain to the next element is the sum is bigger than 9


    So what do you do when you add two numbers by hand? You have to "carry" when the result of adding two digits is more than 10. How can you simulate this in your computer program? In other words, how can you calculate how much to carry to the next place? And how to you add the "carry" into the result?


    2- to sum i need to read from right to left ,,do i need to start add to the last index?


    That depends. Which index represents the ones digit? You should probably start there?


    3- the array size is 40 so if i use less elments the rest will contain zero's
    how can i remove these zero's when i print the result
    i have to use toString(); to print


    Do you mean that you are writing a class that represents large numbers and that you need to write your own toString() method for this class? If so, then you can easily make it so it only prints a digit if the element is not zero. You need to be carful since you should print out zero digits after you see the first non-zero digit. Perhaps it would be better to put -1 in each element that is not being used since this is obviously not a single digit.


    i have to compare between the first int and the second int
    not the array size but the number itself
    are they equal ?
    number 1 is greater than number 2
    number (is the all digit in the array as one number)


    What have you tried here? How can you tell if one number is equal to another one if you can only look at individual digits? Also, do you need to compare greater than or less than? If so, you should probably get "equal to" working first.

    I hope this helps. Please come back with more questions if you need more help.

    Layne

    p.s. We are at the point where it will help if you post short snippets of code to illustrate what you are doing. Code is often much more clear and precise than an equivalent description in English.
    [ October 22, 2005: Message edited by: Layne Lund ]
    mike cool
    Greenhorn

    Joined: Oct 21, 2005
    Posts: 24
    ok guys i solve to sum to int arrays without using BigInteger
    here is the code
    pls tell me if there is any thing wrong


    now i need your help guys to subtract array2 from array1
    Kenneth Albertson
    Ranch Hand

    Joined: Sep 18, 2005
    Posts: 59
    Originally posted by mike cool:
    ok guys i solve to sum to int arrays without using BigInteger
    here is the code
    pls tell me if there is any thing wrong
    Come on, mike, what happened to testing? What answer does this code give for 15 + 6 ? It looks to me that it will return 75. Is that correct?

    This code will only work correctly when array1 and array2 are both the same length. It will also fail if they are both 40 elements long, and there is a "remainder" (that is a misleading variable name) of 1. But one of your earlier posts said that "the 2 arrays have different sizes 30 and 40".
    [ October 22, 2005: Message edited by: Kym Thompson ]
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    You should come up with some tests to check if your code is correct. For exmaple, what happens when you do this:

    In particular, does the values in b here represent the number 24 or the number 42? It could be either one depending on how you define what the order of the elements in the array represents. You should come up with other test cases to see what happens in your code. It's best if you can come up with tests that cause something unexpected to happen. Finding a bug is a good thing because then you can fix it.

    Layne
    [ October 23, 2005: Message edited by: Layne Lund ]
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    Originally posted by Kym Thompson:
    Come on, mike, what happened to testing? What answer does this code give for 15 + 6 ? It looks to me that it will return 75. Is that correct?
    [ October 22, 2005: Message edited by: Kym Thompson ]

    How did you come to this conclusion? You cannot pass parameters directly as 15 and 6 since each element in the array is a single digit. If you correctly "encode" 15 as {5, 1} like the method seems to expect, then it will add the ones digits correctly.

    Of course, you point out a more serious error that occurs when the two arrays are different lengths. But that is a separate issue, in my opinion.

    Layne
    mike cool
    Greenhorn

    Joined: Oct 21, 2005
    Posts: 24

    Come on, mike, what happened to testing? What answer does this code give for 15 + 6 ? It looks to me that it will return 75. Is that correct?

    15 + 6 will never happen as long as there is only one digit in each element..


    This code will only work correctly when array1 and array2 are both the same length

    yes this what i did i use size of 39 of each array and size of 40 for the sum array..


    You should come up with some tests to check if your code is correct. For exmaple, what happens when you do this:


    as you see in my code i read from right to left that means
    array will be


    zero will be on the left side if we don't fill the same amount of elements in both arrays
    as i tsaid bith arrays have fix size of 39


    In particular, does the values in b here represent the number 24 or the number 42?

    it will be 42 but it will be added in the array like this:
    000000000000000000000000000000000000042(each digit in one element )

    what do you think ?
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    First of all, I think you should document your method. Just by looking at it, it is not clear that you expect both paramter arrays are the same length. I suggest you look into the Javadoc syntax.

    Also, I strongly encourage you to write your own test cases. You can easily do this in a separate class that is not part of the main project. You probabl won't turn in this test class with your assignment, but it is a good exercise to convince yourself that it works. The test class can use some if statements to make sure the returned result is what you expect. If you want any help with setting this up, show us what you have tried and we will help you from there.

    Layne
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    I also have a question: What if you want to add three or more numbers together? You can easily create two int arrays for the first two numbers. The problem is that when you get the result, it will have 40 digits. Then when you go to add it to the third number, your method will throw an exception. This is something to consider in deciding if your method works. Of course, you need to decide if this is allowed or not.

    Layne
    mike cool
    Greenhorn

    Joined: Oct 21, 2005
    Posts: 24

    First of all, I think you should document your method. Just by looking at it, it is not clear that you expect both paramter arrays are the same length. I suggest you look into the Javadoc syntax.


    you are right but ,, this not all the assignment this is only one part of it... the idea of this part to creat 2 int arrays has the same size ,, each element of each array can hold one digit only

    i wrote aclass only to test this part and it looks like it's ok
    note that i start only 1 month ago with programming and i don't have experiance like you guys that's why maybe you see some mistakes that i don't see ...
    I also have a question: What if you want to add three or more numbers together? You can easily create two int arrays for the first two numbers


    it will be pnly two arrays so it will never happen that adding 3 numbers
    digit array1 + digit array2 = digit arraysum;
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    You are doing great so far. I still think you should write the documentation for this method before you go on with the rest of the assignment. That way if you need to come back to it, you will more easily remember what this method does.

    Layne
    nimz
    Greenhorn

    Joined: Nov 03, 2005
    Posts: 1
    hey guys
    im having the same problem too
    i have two numbers which are up to 30 digits and i have add and multiply them. can anybody actually help me with the program code.
    and what i think you have to do about the different sizes is to actually reverse both numbers, i think that will help
    thank you
    [ November 03, 2005: Message edited by: nimz ]
    Ashu Sindhu
    Greenhorn

    Joined: Nov 04, 2005
    Posts: 5
    We already have seen the reply for addition (atleast part of the solution). Please try to build on that.
    Its better to work up to the solution than to get the solution. That will help you.

    Thanks,
    Ashu Sindhu
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: sum of two int arrays
     
    Similar Threads
    Adding Arrays
    one computation to be printed
    Performing math equations within arrays
    Array in Method Two Questions? Any one know how to do them?
    Multi-dimensional Array