Results 1 to 3 of 3
  1. #1
    soniabobit is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Default 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
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default Re: Problem with arrays

    Try to do it one step after another.
    First is to create a random number elements, I think it means you need to create array size using random number
    Second having the size known, create the array and instantiate it
    Create random numbers and assign it as array members (for loop will do)
    Now as you have the array (try to list it, like doing arrayName[x]), you can simply add them or it can also be done while assigning it

    I hope this helps
    First is to make something usable, next is to aim for perfection which means a never ending improvements.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,512
    Rep Power
    5

    Default Re: Problem with arrays

    First, you need to place your code between tags. I have done it for you here.


    Java Code:
    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;
     
     
        }
    }
    Your first problem is here:
    Java Code:
    double x = 0 ;double [] numbers = {x*(Math.random()*10)};
    All that does is allocate a double array of size 1 and set the first element to 0. Your method will always return 0 since x starts out as 0.

    Regards,
    Jim
    Last edited by jim829; 11-25-2013 at 02:35 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. A little problem with arrays
    By ariel in forum New To Java
    Replies: 1
    Last Post: 02-15-2013, 11:16 AM
  2. Problem with sorting two arrays
    By jason3460 in forum New To Java
    Replies: 20
    Last Post: 12-07-2012, 06:58 PM
  3. Having problem with Arrays.binarySearch()
    By xEnOn in forum New To Java
    Replies: 13
    Last Post: 05-10-2011, 05:04 PM
  4. Problem with arrays
    By Viper in forum New To Java
    Replies: 7
    Last Post: 10-07-2010, 02:49 PM
  5. Problem: Arrays and Sorting
    By Rhez in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 02:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •