Solved

# Passing arrays in java

Posted on 2005-03-24
Medium Priority
2,774 Views
Here is my code which calculates some values. I have  problem with passing arrays to function and constructor. can any one fix it.
code..........
import java.io.*;
public class correlate
{
double x[];
double y[];

public correlate (double a[],double b[],int n)
{
for (int i=0;i<n;i++)
{
x[i]=a[i];
y[i]=b[i];
}
}
public double SumOfProduct_X_Y();
{
double sumofproduct_x_y =0;
for (int i=0;i<n;i++)
{
sumofproduct_x_y = sumofproduct + ( x[i] * y[i]);
}

return (n * sumofproduct_x_y );
}

public double ProductOfSum_X_Y();
{
double sum_x = 0, sum_y =0, productofsum_x_y = 0;
for (int i=0;i<n;i++)
{
sum_x = sum_x + x[i];
sum_y = sum_y + y[i];
}

productofsum_x_y = (sum_x * sum_y);
return productofsum_x_y;
}

public double SumOf_X_Squares();
{
double sumof_x_squares = 0;
for (int i=0;i<n;i++)
{
sumof_x_squares = sumof_x_squares + (x[i]*x[i]) ;
}
return  (n * sumof_x_squares);
}
public double SumOf_Y_Squares();
{
double sumof_y_squares = 0;
for (int i=0;i<n;i++)
{
sumof_y_squares = sumof_y_squares + (y[i]*y[i]) ;
}

return (n * sumof_y_squares);
}
public double SquareOf_Sum_X();
{
double squareof_sum_x = 0;
for (int i=0;i<n;i++)
{
squareof_sum_x = squareof_sum_x + (x[i]) ;
}
return squareof_sum_x;
}
public double SquareOf_Sum_Y();
{
double squareof_sum_y = 0;
for (int i=0;i<n;i++)
{
squareof_sum_y = squareof_sum_y + (y[i]) ;
}
return squareof_sum_y;

}

public static void main(String args[])
{
double A,B,C,D,E,F;
correlate c1 = new correlate();
double a[] = { 12, 13, 14, 15};
double b[] = { 23, 24, 25, 26};

c1(a[],b[]);

A = c1.SumOfProduct_X_Y();
B = c1.ProductOfSum_X_Y();
C = c1.SumOf_X_Squares();
D = c1.SquareOf_Sum_X()
E = c1.SumOf_Y_Squares();
F = c1.SquareOf_Sum_Y()
System.out.println("  ",A+B+C+D+E+F);
}

}
0
Question by:sharath123in
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 15

Expert Comment

ID: 13627937
c1(a[],b[]) -> c1(a,b);
0

LVL 86

Accepted Solution

CEHJ earned 375 total points
ID: 13628499
Should be

c1(a, b, a.length);

But your ctor should be something like:

public correlate (double a[],double b[],int n)
{
x = new double[n];
y = new double[n];
for (int i=0;i<n;i++)
{
x[i]=a[i];
y[i]=b[i];
}
}

0

LVL 37

Expert Comment

ID: 13628848
CEHJ is right. This is why:

public class correlate
{
double x[];     // <<< Here you only define x as being a (yet undefined) reference to a double array (x = null)
double y[];     // <<< Here you only define x as being a (yet undefined) reference to a double array (y = null)

Before you start using x and y you'll have to specify what they reference:

public correlate (double a[],double b[],int n) {
x = new double[n];     // <<<< Here you (1) create a new double array of length n in memory and (2) let x reference it
y = new double[n];     // <<<< Here you (1) create a new double array of length n in memory and (2) let y reference it
// From now on, you can safely use x and y (since they are no longer null)
for (int i=0;i<n;i++) {
x[i]=a[i];
y[i]=b[i];
}
}

0

LVL 86

Expert Comment

ID: 13635387
:-)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
###### Suggested Courses
Course of the Month7 days, 20 hours left to enroll