-10

I have written a code that is supposed to count the following, here some examples:

x(2) = 1 * (1+2) = 3
x(4) = 1 * (1+2) * (1+2+3) * (1+2+3+4) = 180
x(5) = 1 * (1+2) * (1+2+3) * (1+2+3+4) * (1+2+3+4+5) = 2700

Edit:

Thx a lot so far to everyone!

I didn't expect to get help that fast and precise, really nice :D

Just modified my code now and I no longer get 0 as result (no matter what i typed) which is very good. But I think there is another mistake left somewhere, let's say I type into console 2, I will get 2 as result. If I type 4, I get 24 as result. For 5 I get 120 and 6 I get 720. From this I could realize one thing. If i divide let's say 720 by 6, I get 120 which is the previous result (of 5). And if I take result of 5 which is 120 and divide it by 4, I get 24.

public class CounIt 
{
    public static int i;
    public static int j;

    public static int b;
    public static int c;

    public static int a (int k)
    {
        j = 0;
        for (i = 1; i <= k; ++i)
            j = j + 1;
        return j;
    }

    public static int x (int k)
    {
        b = 1;
        for (c = 1; c <= k; ++c)
            b = b * a(c);
        return b;
    }

    public static void main (String[] args)
    {
        int k = Integer.parseInt(args[0]);
        System.out.println(x(k));
    }
}
5
  • 8
    0 times n = 0. You probably wanted b = 1;
    – Tunaki
    Jan 6, 2016 at 9:57
  • @kocko: Amusingly, in this case it doesn't matter. Jan 6, 2016 at 9:57
  • 5
    Whenever faced with something like this, use your debugger to step through the code and watch it run, looking at the values of the variables. See also How to debug small programs. Jan 6, 2016 at 9:59
  • Instead of initializing the variables locally, why not make a constructor and set the variables to 0 The problem is the second method will return return 0. b = 0 * a(c) = 0... Jan 6, 2016 at 10:00
  • 1
    You should not be changing the code in your question once you've started getting answers (thus invalidating them). You also should be posting follow-up questions in the answers section. If you have a new question, post it as a separate question.
    – BSMP
    Jan 8, 2016 at 0:27

5 Answers 5

3

Two problems:

  1. b remains 0 in the function x(int), so this function always returns 0. Shouldn't it be initialised to 1?

  2. The function a(int) returns the input parameter. Didn't you mean to return j?

Also, having single character function names makes your program tricky to read.

1

Because you initialized b to zero every multiplication of b will give you 0

    b = 0;   // Set b to zero
    for (c = 1; c <= k; ++c)
        b = b * a(c);    // b will stay to 0 because b = 0 * a(c);

Modify your code to

    b = 1;  // <---- Here the modification
    for (c = 1; c <= k; ++c)
        b = b * a(c); 

To be sure that your code works as expected you should unit test your functions. Take a look at TDD metodology, it will save you a lot of time for bigger projects. And here a link to a nice tutorial to unit tests.

1
  • Tyvm I haven't realized that! But there is another mistake I think. Let's say I type 2, then i get 2 as result. If I type 4 I get 24 as result. For 5 I get 120.
    – user5748960
    Jan 6, 2016 at 10:01
0

Try this:

public class CountIt 
{
    public static int i;
    public static int j;

    public static int b;
    public static int c;

    public static int a (int k)
    {
        j = 0;
        for (i = 1; i < k; ++i)
            j = j + 1;
        return j;
    }

    public static int x (int k)
    {
        b = 1;
        for (c = 1; c <= k; ++c)
            b = b * a(c);
        return b;
    }

    public static void main (String[] args)
    {
        int k = Integer.parseInt(args[0]);
        System.out.println(x(k));
    }
}
0

This function is very suitable for recursion:

public class SumPyramid {

    public static void main (String[] args) {
        System.out.println(sumPyramid(5));
    }

    public static int sumPyramid(int height) {
        if (height == 1) {
            return 1;
        }
        else
            return height + sumPyramid(height - 1);
        }
    }
}
0

Both functions a and x have errors. Modify functions as below.

Logically its working

public static int a (int k)
{
        j = 0;
        for (i = 1; i <= k; ++i) // changed consition to less than or equal to
            j = j + 1;
        return j; // returns j
}

public static int x (int k)
{
        b = 1; // changed initial value to 1
        for (c = 1; c <= k; ++c)
            b = b * a(c);
        return b;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.