Hi:

I have to write a program that uses methods and arrays. I have to create a method that creates an array and populates it with 10 random numbers between 1 and 10 inclusive. Then I have to write another method that sorts the number from lowest to highest and print then in the console. So far so good, except I'm generating a 0 as a value, and I cannot have a 0, only numbers between 1-10 :?

Here's what I got so far

public class SortTenDigits {

	public static void main(String[] args) {

		///Main method to call other methods

	int[] numbers = new int[10]; //create array for space for 10 numbers only

	populateArray( numbers );
	sortPrintArray( numbers );



	} // main end

	public static int[] populateArray( int[] numbers ) { //populates array with numbers

		for ( int i = 1; i < numbers.length; i++) {
		numbers[i] = (int)(1 + Math.random() * 10); //create numbers between 1 and 10

	}
		return numbers;
	}//populateArray end

	public static void sortPrintArray ( int[] numbers ) { //sort and print the numbers

		for ( int el : numbers ) { //copy numbers array to array el

			for ( int i = numbers.length - 1; i >= 1; i--) { //toggle between the numbers and place them in order

				int Max = numbers[0];
				int maxIndex = 0;

				//Find the maximun value and assign it to the Max
				for ( int j = 1; j <= i; j++) {

					if ( Max < numbers[j] ) {
						Max = numbers[j];
						maxIndex = j;
					}

				}

				//swap values if necessary
				if ( maxIndex != i ) {
					numbers[maxIndex] = numbers[i];
					numbers[i] = Max;
				}
			}
			//Print results
			System.out.println( el );


		}//for end


	} //printArray

}

Any hints to my problem would be greatly appreciated :)

public class SortTenDigits {

	public static void main(String[] args) {

		///Main method to call other methods

	int[] numbers = new int[10]; //create array for space for 10 numbers only

	populateArray( numbers );
	sortPrintArray( numbers );



	} // main end

	public static int[] populateArray( int[] numbers ) { //populates array with numbers

		for ( int i = 0; i < numbers.length; i++) {
		numbers[i] = (int)(1 + Math.random() * 10); //create numbers between 1 and 10

	}
		return numbers;
	}//populateArray end

	public static void sortPrintArray ( int[] numbers ) { //sort and print the numbers

		for ( int el : numbers ) { //copy numbers array to array el

			for ( int i = numbers.length - 1; i >= 1; i--) { //toggle between the numbers and place them in order

				int Max = numbers[0];
				int maxIndex = 0;

				//Find the maximun value and assign it to the Max
				for ( int j = 1; j <= i; j++) {

					if ( Max < numbers[j] ) {
						Max = numbers[j];
						maxIndex = j;
					}

				}

				//swap values if necessary
				if ( maxIndex != i ) {
					numbers[maxIndex] = numbers[i];
					numbers[i] = Max;
				}
			}
			//Print results
			System.out.println( el );


		}//for end


	} //printArray

}

your first for loop needed to be initialized at 0. I think because you were starting at index 1 for the array, it just used a default value for index 0 (the real "first" item in the array).

hmm it did work, I did get rid of the 0 but then the formatting is all screwed up now after changing the value of the first loop from 1 to 0 :?

I don't notice any kind of format change.

Edit. It does formats correctly, except the first value generated always changes. Its not always the lowest value. So sometimes the format is correct because the first value is 1, and other times is wrong because the first value is bigger than the second value.

I found my solution. Thanks for the help :)

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.