Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Problem with arrays

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Unhappy Problem with arrays

    Good evening everyone ,
    I am writing this post to ask you guys if you can help me on this very basic program .
    I am still a student in programming and a beginner with arrays and i am asked to create a program that creates random elements of arrays between 0 and 9 , and inside every box of these arrays a random number between 0 and 100 and finally to calculate the sum of these numbers , example
    6 random elements of an array are generated , inside each one of these elements a certain value is created , then i shall add these value and print the value.
    Now as i said i am still a beginner in java so i know this program is messed up but at least i tried i hope you guys can help me , thank you :
    public class arrays {
    	public static void main(String[] args) {
     
    	double x = 0 ;
    	double [] numbers = {x*(Math.random()*10)};
    	double result  = sumLessThanAverage(numbers);
    	System.out.println(result);
    	}
    	public static double sumLessThanAverage(double[]a){
    		double x  = 0;
    		double sum = 0;
    		double count=0;
    		x = x * Math.random()*101;
    		for ( int i = 0 ; i<= a.length-1 ; i++){
    			a[i] = x;
    			sum += x;
    			x = x * Math.random()*101;
    			count ++;
    		}
    		return sum;
     
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with arrays

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Problem with arrays

    double x = 0 ;
    double [] numbers = {x*(Math.random()*10)};

    What do you get when you multiply a number by 0?

  4. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    Jad_Asmar (November 23rd, 2013)

  5. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Problem with arrays

    Quote Originally Posted by ChristopherLowe View Post
    double x = 0 ;
    double [] numbers = {x*(Math.random()*10)};

    What do you get when you multiply a number by 0?
    Oh god , what a foolish mistake , thank you !

  6. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Problem with arrays

    All good. There are other problems here. Are you trying to create an array with 10 elements?

    double [] numbers = {(Math.random()*10)};

    This is creating an array with exactly one element. Instead, use this to create an array with a desired length:

    int length = 9;
    double[] numbers = new double[length]

  7. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    Jad_Asmar (November 23rd, 2013)

  8. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Problem with arrays

    Thank you Christopher , but the thing is I would like to create an array but randomly and by that i mean it can be 4 arrays 5 or 6 up to 10 ; that's what i want .
    To make it clear my whole program is about creating random arrays adding there value and calculating their average and finally to print the values that are less than the average itself.

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with arrays

    it can be 4 arrays 5 or 6 up to 10
    Change the value assigned to the variable: length in ChristopherLowe's post#5 to be a random number in the range of the sizes that you want.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with Arrays in code !!!!
    By Cluelius in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 8th, 2012, 06:58 PM
  2. Help with 2-d arrays, tough problem
    By aesguitar in forum Algorithms & Recursion
    Replies: 22
    Last Post: June 21st, 2012, 09:03 AM
  3. [SOLVED] Problem with Arrays
    By dashpilot in forum Object Oriented Programming
    Replies: 4
    Last Post: March 6th, 2012, 10:11 AM
  4. Problem with arrays
    By coorscollector in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2011, 03:50 AM
  5. 2 D arrays problem
    By Pulse_Irl in forum Collections and Generics
    Replies: 2
    Last Post: March 5th, 2010, 04:49 PM