This is my first post in the Dani community. I am brand new to java, 5 weeks into an intro to computer programming course. My assignment is to create 10 random numbers between 50 and 100. Then I am supposed to sort them from lowest to highest, find the average and the max and min. I have got all but the sorting down. i have been struggling so much with this class I cant believe I got this much, any help in the right direction would be greatly appreciated.

package tenrandomnumbers;

import java.util.Scanner;
import java.util.Random;

public class TenRandomNumbers {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] Random;
        Random = new int[10];
        int count = 0;
        double average = 0;
        int Max = 0;
        int Min = 100;
        int sum = 0;
        int number = 0;


        for (count = 0; count < 10; count++){
            number = 50 + (int)(Math.random()*50);
            Random[count] = number;
            System.out.println(Random[count]);
            sum = sum + number;
            average = sum/10;
            while(Random[count]>Max)
                Max=Random[count];
            while(Random[count]<Min)
                Min=Random[count];
    }
        System.out.println("The average is: " + average);
        System.out.println("The highest number is " + Max);
        System.out.println("The lowest number is " + Min);



    }

}

Welcome to Daniweb! If you want to post code in the future, please use the code tags (read the 2 announcement posts at the top of the forum). The code tags just make the code easier to read.

As far as sorting is concerned, you have 2 options: you can create your own sorting algorithm or use a predefined one.

1: Creating your own algorithm:
There are several different sorting approaches you can use. The 2 most basic while still fairly efficient sorts are Selection Sort and Insertion Sort.

Selection Sort searches through all the numbers for the lowest value and swaps it with the value at the first index. Then it looks through the numbers from the second index to the end for the next lowest value and swaps it with the value at the second index. Then it searches through the array from the third index to the end, etc. etc. until the entire array is ordered.

Example:
9 3 1 7 2 //length 5
1 3 9 7 2 //iteration 1
1 2 9 7 3 //iteration 2
1 2 3 7 9 //iteration 3
1 2 3 7 9 //iteration 4

Insertion Sort begins by designating the end of the array to be the "sorted side." We know it starts off as "sorted" because we are only including the last value, and a single value is always sorted. Then insert the value at the first index in its appropriate spot on the "sorted" side. Now there are 2 values on the sorted side. Take the new value at the first index and place it into its spot on the sorted side, etc. etc. until the number of iterations is equivalent to the length of the list. The list should now be sorted.

Example:
9 3 1 7 2 //length 5
3 1 7 2 9 //iteration 1
1 7 2 3 9 //iteration 2
7 1 2 3 9 //iteration 3
1 2 3 7 9 //iteration 4

2: Using a predefined algorithm
In the util package java.util.Arrays has a static method sort(int[] x) that sorts an array of integers for you. So to sort the Random array, just use the syntax Arrays.sort(Random); This way is easier but the other is more educational for beginners.

commented: Great post, thanks for the help. +0

KVASS,
Thanks for the help. I used your advice but when I initially added the sort function I kept getting a bunch of 0's in my random numbers. I ended up using another for loop that fixed it. I barely know what I am doing in this class because although the book we have is all theory using psuedocode the instructor has us writing code. I spend most of my time searching for other code and trying to manipulate it to get the results I need. How does my code look to you? Would there have been a more efficient way to get the same result? Thanks again for the help. I put the code up again because I figure you reply helped me so I should put the end result up for the next needy person. Thanks again.

package tenrandomnumbers;

import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

public class TenRandomNumbers {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] Random;
		Random = new int[10];
		int count = 0;
		double average = 0;
		int Max = 0;
		int Min = 100;
		int sum = 0;
		int number = 0;

		
		for (count = 0; count < 10; count++){
			number = 50 + (int)(Math.random()*50);
			Random[count] = number;
			sum = sum + number;
			average = sum/10;
		}
		System.out.println("Ten randomly generated numbers from 50 to 100");
		System.out.println("In Ascending Order:");
			Arrays.sort(Random);
			for (count = 0; count < 10; count++){
				System.out.println(Random[count]);
			
			while(Random[count]>Max)
				Max=Random[count];
			while(Random[count]<Min)
				Min=Random[count];
			
	}
		System.out.println("The average is: " + average);
		System.out.println("The highest number is " + Max);
		System.out.println("The lowest number is " + Min);
		

		
	}
		
}
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.