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 6 of 6

Thread: Beginner Coding problem in a class taught by professor with limited English comprehension

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Beginner Coding problem in a class taught by professor with limited English comprehension

    Hello, I have been taking a cs 2010 class and have finally gotten to start coding in Java. However, my professor cannot speak or write English very well so I am having to learn mostly by myself.
    He recently sent us a project in which we have to:

    1. Write a program that will use an array to store 10,000 randomly generated
    numbers (ranging from 1 to 10,000 with no repeating numbers)

    2. Using the selection sort algorithm to sort the number in ascending order, display the sorted sequence, each line showing just one number.

    I have discovered that I will need to presumably generate the array with the 10,000 numbers, shuffle them, and then sort them.
    However, I am completely lost on how to fill the array with the 10,000 numbers to begin with. I only know how to randomly fill them, but by doing that, I have repeating numbers.

    Here is a snippet of what I have tried so far with no clue as to how to go from there. I thought I could write a while loop that would fill each one with (sum = 1+ sum), but I am not sure of the syntax or if that is even possible with arrays. Any help would be oh so helpful.




    package array.sorter.project;
     
    import java.util.*;
    import java.util.Arrays;
     
     
    public class Sorting {
    	//generating 10000 spots in the array
    	public static void main(String args[]){
    	int[] tenthousand = new int[10000];
     
    	//loop to add 1 to x and set the new value of x to the array spot
    	int sum = 0;
    	while ( sum < 10000){
    			 sum = sum + 1;
    	Arrays.fill(tenthousand, sum);
    	}
     
     
     
    	  for (int i = 0; i < tenthousand.length; i++) {
    		  int smallestNo = tenthousand[i];
    		  int posWithSmallest = i;
    		  for (int j = i+1; j < tenthousand.length; j++) {
    		    int val = tenthousand[j];
    		    if (val < smallestNo) {
    		      smallestNo = val;
    		      posWithSmallest = j;
    		    }
    		  }
    		  int tmp = tenthousand[i];
    		  tenthousand[i] = smallestNo;
    		  tenthousand[posWithSmallest] = tmp;
     
     
    	}
     
     
    	for (int i = 0; i < tenthousand.length; i++) {
    		  System.out.println("Position " + i + " : " + tenthousand[i]);
    		}
     
     
     
    		}
     
    }



    I apologize in advance in any of this is terribly written or just completely wrong. As I said, I have no clue.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Beginner Coding problem in a class taught by professor with limited English comprehension

    I have discovered that I will need to presumably generate the array with the 10,000 numbers, shuffle them, and then sort them.
    However, I am completely lost on how to fill the array with the 10,000 numbers to begin with. I only know how to randomly fill them, but by doing that, I have repeating numbers.
    Are you sure the array is supposed to contain 10,000 numbers?

    If so, and you didn't want any repeats, you wouldn't fill them with random numbers - you would fill them with the numbers 1->10,000 (that takes care of having no repeats) then shuffle them (that provides the randomness). What I'm thinking is that sorting the resulting array would be a bit pointless in this case as you know in advance what the result would be.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Coding problem in a class taught by professor with limited English comprehension

    I agree completely but that's exactly what the instructions say. I don't know. How does one go about filling the array with the numbers?

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Beginner Coding problem in a class taught by professor with limited English comprehension

    How does one go about filling the array with the numbers?
    You are making it too complicated with that while loop and the fill() thing. Use a for loop. In fact it will be a for loop that looks a lot like the one at the end that you use to print the contents of the array. Instead of printing each element you assign a value to each element:

    for (int i = 0; i < tenthousand.length; i++) {
        tenthousand[i] = ...
    }

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Beginner Coding problem in a class taught by professor with limited English comprehension

    Break the problem up and tackle and test each problem.

    1. How do we generate a random number between 1 and 10,000?
    2. Create an array to hold 10,000 numbers.
    3. How do I store only unique values? Do I filter them before filling the array or after? What tools are there in the API to help me?
    4. Look up the required "selection sort" algorithm and make sure I know how it works. Implement a sort method and test it.
    etc..

  6. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Beginner Coding problem in a class taught by professor with limited English comprehension

    Quote Originally Posted by tdat3 View Post
    1. Write a program that will use an array to store 10,000 randomly generated
    numbers (ranging from 1 to 10,000 with no repeating numbers)
    If this is correct then your end sorted result is going to be an array with every value 1 to 10,000. Creating actual random numbers would take a long long time for this program to run. Perhaps this is what your prof wants, but IMO that's crazy. If i were to do this i would do it in a psuedo random way (if your prof argues, start the argument that nothing is truly random).

    My Opinion:
    write a for loop that will go through each number 1 through 10,000
    use the random number generator to get your array space, if that space already has a value, increment until you find an open space and just put it there...random enough IMO
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. hangman problem java coding
    By angelshark in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 8th, 2013, 12:55 PM
  2. Simplifying Uml Class Diagram For System Comprehension
    By ArjanZ in forum Other Programming Languages
    Replies: 0
    Last Post: June 17th, 2012, 03:37 AM
  3. Help with Java coding problem!
    By eyesackery in forum Java Theory & Questions
    Replies: 4
    Last Post: June 4th, 2012, 09:21 AM
  4. Self-Taught Programmer Seeking Soulmate (language) [LONG STORY]
    By Destro in forum Member Introductions
    Replies: 1
    Last Post: April 18th, 2012, 08:30 AM
  5. Coding Problem
    By BohmfalkCW in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 26th, 2012, 06:10 PM

Tags for this Thread