Results 1 to 6 of 6
  1. #1
    matin1234 is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default problem in the half of odd integer

    Hi
    this a an algorithm that I have found in the internet ,all the things are OK but just consider n=5 ,the last statement would be like this:

    Java Code:
    return BinarySum(A,0,[5/2])+BinarySum(A,0+[5/2],[5/2])
    so the return statement will sum just 4 first numbers ! how about the index 5?
    :mad:
    Java Code:
    Algorithm BinarySum(A,i,n)
    Input:An array A and integers i and n
    Output:The sum of integers in A starting  at index i
    if n=1 then
     return A[i]
    return BinarySum(A,i,[n/2])+BinarySum(A,i+[n/2],[n/2])

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    If you divide two integers, the operation will actually be integer division, ie. divide and forget the remainder:
    a = m*b + r;
    So, in the case of 5:
    5 = 2*2 + 1;
    So the return of the 5/2 operation is 2, the +1 remainder is discarded.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    If you divide n by two the first 'half' contains n/2 (integer division) elements so the other 'half' (those quotes again) contains n-n/2 elements.

    kind regards,

    Jos

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    @OP Write a program to demonstrate the problem.

  5. #5
    matin1234 is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default problem in the half of odd integer

    thanks for your help but please consider n=5 , I want to sum all 5 integers together but here will sum just those 4 first integers.
    A{1,4,6,8,9}

    BinarySum(A,0,2)+BinarySum(A,2,2) ----> So this statement will return 19 not 28 !!!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Can you post the program with an explanation of why you think it doesn't work?
    Also a console showing intput and output.

Similar Threads

  1. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  2. Hex to Integer Problem
    By Tempspec in forum New To Java
    Replies: 0
    Last Post: 04-01-2010, 11:14 AM
  3. loop in a half
    By Brain_Child in forum New To Java
    Replies: 4
    Last Post: 12-02-2009, 03:00 AM
  4. [SOLVED] Integer.parseInt() problem
    By eggmanpete in forum New To Java
    Replies: 18
    Last Post: 02-15-2009, 04:25 PM
  5. Problem with Integer.parseInt()
    By Hevonen in forum New To Java
    Replies: 2
    Last Post: 12-14-2008, 03:41 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •