0

I am not sure how print the values of arrays when called from methods, I have to solve these:

1- create an array consisting of 100 random integers in the range 100 to 500, including the end points. (This part i am OK, the next 2 points i am quite doubtful on how solve it)

2- make a method to print the array, 5 numbers per line, with a space between each. (I got almost everything right except I don't know how Return the value, I tried return System.outprint..... but didn't work either anyway the method has a void some made it worse)

3- make a method to print the smallest number in the array. (this i got no clue how to do it!)

  1. make a method to print the sum of all numbers in the array. (This I don't quite see why the "return ad;" is not working as most of the code seems correct to me at least hehe)

This is my code so far:

package randomhundred;

import java.util.Arrays;

public class RandomHundred {
    private static int[] rand;

    public static void main(String[] args) {
        // TODO code application logic here

        //setting the 100 array

        /* PART I*/

       int rand [] = new int [100];
       int numb;
       for(int i=0; i<rand.length; i++){
        numb =  (int) (100 + (Math.random() * (  (500 - 100)  + 1)));
         numb = rand[i];
     }  

        }

    /* PART II */
    public static void arai (){
     for (int i=0; i<100; i++){
     System.out.print(rand[i] + " ");

      if( i%5 == 0){
            System.out.println();
            }
      else{
          System.out.print(rand[i] + " ");
      }
     }


    /**
    PART IV
     */
    public static int suma(){
      int ad;
      for(int i=0; i<100; i++){
      ad =+rand[i];
      }
      return ad;
  }   

    }   
    }
3
  • 1
    For part 1, you have numb = rand[i]. You're assigning numb to rand[i];, not the other way round, which is wrong. It should really be rand[i] = numb; Commented Nov 19, 2013 at 23:02
  • You are right Chandrasu, sorry for not thanking you before, thanx a lot man and thax for the tip now! As for slider, thanx i all try that now! Commented Nov 19, 2013 at 23:08
  • You are right did it now, thanx for the tip man, didnt know that was the way to do it! gave u a link as well in my blog pugle.net/blog Commented Nov 19, 2013 at 23:35

3 Answers 3

2

Change:

ad =+rand[i];

to

ad += rand[i];

for part IV to work.

Sign up to request clarification or add additional context in comments.

1 Comment

How does this answer address points 1, 2 or 3 in the question? In fact, I don't think there was any reference to this in the question! (I guess I had to read between the lines! :P )
1

First of all, when setting your numbers, you need to set the array index... e.g.

rand[i] =  (int) (100 + (Math.random() * 401)); // 100-500

Part 2 should read:

for (int i=0; i<rand.size(); i++){
    if( i%5 == 4){
        System.out.println(rand[i] + " ");
    } else{
        System.out.print(rand[i] + " ");
    }
}

Part 3 should read:

int ad = 500;
for(int i=0; i<100; i++){
    ad = Math.min(ad, rand[i]);
}
System.out.println("Smallest="+ad);

1 Comment

The code for part 2 will skip every sixth number in the array.
0

For part 3, you're going to want to

  1. Create an integer and set it to the first variable in your array
  2. Loop through all the variables in the array
  3. For each variable, if it's smaller than the one we created in step 1, set the integer we created to the smaller variable

By the end of this loop the integer we created must be the smallest possible number, as we went through every possible variable to see if there was a smaller one. All you have to do now is print it out.

Also I don't know why you would want to return the values in part 2, a void function doesn't have to return anything and you can just print out the numbers straight from the function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.