Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

indexed and non-indexed input

Shanky Sohar
Ranch Hand
Posts: 1051
There is a requirement in my project which say i have to input the 3 arrays from the user.....
like a[n],b[n].c[n].....then

the input should be purely in array......

Like for a[n].......a[1],a[2],a[3],a[4],a[5],a[5]...then i have to add all of them to reach at the conclusion........

Is there any way to do.....this.........i am completely confused how to reach at the below ouput.....

output=={ (1+a[n] x (1-(1-b[n]) x Z) +
qx (1-b[n]) x Z)1/1024 -
(p+c[n] x (1-(1-b[n]) x Z)+
s x (1-b[n]) x Z)/1024 }1024/12

Where a[n],b[n],c[n] are indexed input
Z,q,p,s are non-indexed input........

Greg Charles
Sheriff
Posts: 2984
12
Wow. First, assignments are done with =, not ==, but the rest of that expression is completely baffling to me. Generally speaking it's best to do a job as simply as possible. In this case, that would be three for loops:

Once you get a little more comfortable, you can think about System.arraycopy() or something fancier. Now, as I say, I'm baffled by the expression you wrote out, so it's entirely possible that your assignment is calling for something more complex than just combining three arrays into one. Hopefully, this will get you started though.

Shanky Sohar
Ranch Hand
Posts: 1051
Greg Charles wrote:Wow. First, assignments are done with =, not ==, but the rest of that expression is completely baffling to me. Generally speaking it's best to do a job as simply as possible. In this case, that would be three for loops:

Once you get a little more comfortable, you can think about System.arraycopy() or something fancier. Now, as I say, I'm baffled by the expression you wrote out, so it's entirely possible that your assignment is calling for something more complex than just combining three arrays into one. Hopefully, this will get you started though.

yes the assignement is very complex...........i donot know how i am able to complete.........i m theonly person who is gng to write the code for this...............

Greg Charles
Sheriff
Posts: 2984
12
OK, deep breaths. You'll get through it. Representing formulas into code is actually one of the more straightforward things to do in programming. It's mainly a matter of knowing what the formula is and then getting the parentheses right. I'm not clear what you intend "output" to be here. Is it an array with n elements or a single value? Also, when you wrote "x" in your formula are you intending that to be a variable x, or multiplication? In any case, if you can state what you want to do in English, then it won't be that much harder to do it in Java.

Shanky Sohar
Ranch Hand
Posts: 1051
there is requirement to do the summation.............

i want to do it shorter,to reduce the complexity.....
so i was searching for a alternative to replace the no of for loops in my code.

I found a method that is called as sum().
if,it can be implemented in my code...then the code become so easy .....
for summation Refernce to the function is given below

http://science.kennesaw.edu/~plaval/applets/HelpFuncBI.html

sum(expr, index, start, end) finds the partial sum using expr, with the index running from start to end.

it tried it but there is an error which says

the method sum is un defined for math class but it is mentioned in the link above

here is my code

import java.lang.Math.*;
public class sumation {
public static void main(String args[])
{
int n=10;
int y;
y=Math.sum(n^n/n,n,0,10);
}

}

As there is so many For loops in my requirement so i want to reduce those loops and make the coding easier and simpler.....

if it is not possible please provide me an alternative for doing that.......

for example:-
If there is a series like "n / 2^(n +1)" for n=1 to 100....
is there any way to solve this using the direct fuction which is available in java.......

Greg Charles
Sheriff
Posts: 2984
12
Ah, I see. Well, there are several reasons that doesn't work. First, the "sum" method is not part of Java's Math class. I looked at that link, and I'm not sure exactly what's it's documenting, but it's not the Java API. Google "javadoc Math" and you'll get better results.

Another problem is that certain operators they use in their example, like "^" for exponents and "!" for factorials, just don't work that way in Java.

Finally, passing an expression as an argument to a method is something called a closure, which is simply not supported in the Java language at the moment. You can approximate the idea though with an interface and an anonymous inner class. I would definitely suggest working out the functionality first with for-loops, then think about introducing a slick pseudo-closure into the mix.