Hi,
i have a problem here , i have made a class which has three methods , first method is to get size of the array and assign a random number to every element of array. Second method is to just print the whole array , while the last method needs to add up all the elements in array. When i run the code i am not getting desired result and i instead get all zero's for my array element hence getting sum of zero. Here is my code , the actual class and main program, if anyone can help , i will b very tthankful. : )

/****Class****/
class ArrayTest {

public int[] ArraySize(int size)
{
	int[] Array1= new int[size];

	for(size = 0;size <=(size-1);size++)
	{
		Array1[size]=(int)Math.random();
	}
	
	return Array1;
}

public void printArray(int[] array)
{
	System.out.print("\n[");

	for(int size = 0; size<array.length; size++)
	{
		System.out.print(" "+array[size]+",");
	}

	System.out.print("]\n");
}

public int addArray (int[] array)
{
 int sum=0;
 
 for(int size =0;size<array.length;size++)
 {
 	sum = sum + array[size];
 }
 
 System.out.print("\nThe sum of the array is : "+sum);
 
 return sum;	
}
}


/******Main program in which i used class**************/

public class javalabsheets {
    
    public static void main(String[] args) {
    	int[] array;
        ArrayTest test= new ArrayTest();
        array = test.ArraySize(10);
        test.printArray(array);
        test.addArray(array);
        	
    }
}

And the output is :
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]

The sum of the array is : 0
Process completed.

Recommended Answers

All 7 Replies

Ok, 2 main problems with the method that creates your array - see comments in the code belowe

int[] Array1= new int[size];
        // you are changing 'size' in this loop
        // use a new variable in the loop
	[B]for(size = 0;size <=(size-1);size++)[/B]
	{
	    // casting Math.random() to an int will always yield zero
           [B]Array1[size]=(int)Math.random();[/B]
	}

If you need a random number between 0 and X, use Math.round(Math.random()*X).

hi,
i have changed my code but i get this error saying "loss of precision".

public int[] ArraySize(int size)
{
                
	int[] Array1= new int[size];
       int  i = size;
	for(i = 0;i <=(i-1);i++)
	{
		Array1[i]= Math.round (Math.random()*100);
	}
	
	return Array1;
}

Error :
possible loss of precision
found : long
required: int
Array1= Math.round (Math.random()*100);
^
any clue abt it?

round() returns a long if you pass it a double, which your calc is doing. You can just cast that to an int.

Array1[size]= (int)Math.round (Math.random()*100);

i have used another method here which by using Random function, and my array creation method looks like this:

public int[] ArraySize(int size)
{
	Random ran = new Random();
	int[] Array1= new int[size];
    int  i = size;
	for(i = 0;i <=(i-1);i++)
	{
		
		Array1[i]= ran.nextInt(100);
	}
	
	return Array1;
}

But still i am getting all zero's

round() returns a long if you pass it a double, which your calc is doing. You can just cast that to an int.

Array1[size]= (int)Math.round (Math.random()*100);

By using the above method i am still getting the same result

Look at this loop and walk through each iteration in your head or write down the values for one or two iterations and I think you will see the problem

for(i = 0;i <=(i-1);i++)

Look at this loop and walk through each iteration in your head or write down the values for one or two iterations and I think you will see the problem

for(i = 0;i <=(i-1);i++)

Oooooh a stupid mistake lol , Thanks for help its working ; )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.