Results 1 to 12 of 12
  1. #1
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default need help with basic programming assignment (with arrays)

    What I need to do is create an array of 1001 numbers and have the user enter as many positive numbers up to 1001 numbers into the program and terminate the array with a 0 or negative number. In this example, the user entered 19 13 7 21 11 9 15 17 -1. Then I need to have the program ask the user to enter 2 of the numbers that appeared/or didn't in the list they originally entered and have the program (For example 13 and 9) and print out these three things-

    1) The 2 additional numbers appear in the original list in the same order (not necessarily consecutive).
    2)The 2 additional numbers appear in the original list, but not in the same order ( and, again, not necessarily consecutive).
    3)These 2 additional numbers do not appear together in the original list (although one of them might).

    For example to see what I am talking about:

    1) That pair (13 and 9) could be found, in that order, in the following list:
    19 13 7 21 11 9 15 17
    2) That pair (9 and 13) could be found, in that order, in the following list:
    19 13 7 21 11 9 15 17
    3) That (8 and 9) could not be found, at all, in the following list:
    19 13 7 21 11 9 15 17

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    prettty much, how do i do it?...i am so stuck. my teacher doesn't teach java at all, he just expects us to learn it ourselves. so far this is what I have...


    import java.util.Scanner; // imports the scanner into the program
    public class Prog4
    {
    public static void main(String[] args) // starts the main method
    {
    Scanner keyboard = new Scanner(System.in); // obtains the scanner keyboard
    int i = 0; // initializes the position number
    int current; // declares the number to be entered into the program
    int[] number; // declares the array
    int size = 0; // initializes the size of the array
    number = new int[1001]; // declares a name "number" for the array
    System.out.println("Enter a series of positive " // asks to user to input numbers into program
    + "numbers\n\t(ending with a negative number): ");

    current = keyboard.nextInt(); // gets the numbers entered by the user

    while((current > 0) && (size < 1001)) // while loop that states when the number is
    { // positive and the amount of numbers is less then 1001
    number [size++] = current; // gives the number entered to the position "size" in the array
    i += 1; // increments the position in the array
    current = keyboard.nextInt(); // gets the number entered by the user and assigns it to "current"
    }

    System.out.println("Enter 2 numbers to be found\n\tin the " +
    "list you just entered: ");
    int any = keyboard.nextInt();

    System.out.println("That pair( and )")
    }
    }

  4. #4
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by magictricks1020 View Post
    my teacher doesn't teach java at all, he just expects us to learn it ourselves.
    What an evil teacher, making their students do research and think for themselves. :(
    so far this is what I have...
    Use CODE or PHP tags when posting code. Do you really have to put usercomments on every single row of code? Most of them are obvious and don't need commenting. It just makes the code harder to read.

    I still don't see a question other than "How do I do this?" and that's not for us, but for you, to figure out.

    Does the code compile (We can all see if it does, but can you?)? If no, post the exact error messages and what lines they indicate. If yes, what's the output? What output are you expecting?

    Where are you stuck, on what part exactly? So far you're way too vague and submits too little information in your posts for us to help you efficiently.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What does your teacher do then?

  6. #6
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    He just told us. Not teaching Java. :)

  7. #7
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    he does everything but teach us. we are expected to look up how to do everything. i went to his office hours and all he says is "i don't know, I can't help you". Professors at my school are more interested in research then actually teaching us. And at the comment about the comments. if we don't have a comment for every single line of code, we get a 0 for the project. I wish I could tell you the part im exactly stuck at, but it's pretty much everything after initializeing the array. I have no idea how to use an array, so I am using the internet as my last resort, or I will have to take the 0 because I have no idea how to do this. I appreciate anything you guys can do to help me.

  8. #8
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    If the teacher really is useless you should make this clear to whomever is in charge, like a board of schools or however the system works where you live.

    As for the Java, it's quite hard to do anything but write the entire thing for you when you don't know where to start. And even if the teacher situation is as bad as you claim, it's still your problem to address.

    This is fairly basic Java stuff and you shouldn't really have too much of an problem to grasp working with arrays (assigning values, looping etc.) after having spent some time with the online tutorials provided by Sun. All you really need is a good aptitude for programming and its logics.

    You also need to really think about what your code does. It seems like you're kind of just throwing random lines of code on the wall hoping it will stick.

    These 2 lines for example:
    PHP Code:
    number [size++] = current;	 // gives the number entered to the position "size" in the array
    i += 1;	 // increments the position in the array
    What do expect them to do? They're not logical at all. The second one is actually entirely pointless as the i variable is never used in your while statement.

  9. #9
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    I played around a little bit and got the first two parts!!! :) How would I do part 3, and also, how do I make it print the array after it says "in the following list: " ??


    import java.util.Scanner; // imports the scanner into the program
    public class Prog4
    {
    public static void main(String[] args) // starts the main method
    {
    Scanner keyboard = new Scanner(System.in); // obtains the scanner keyboard
    int i = 0; // initializes the position number
    int current; // declares the number to be entered into the program
    int[] number; // declares the array
    int size = 0; // initializes the size of the array
    number = new int[1001]; // declares a name "number" for the array
    System.out.println("Enter a series of positive " // asks to user to input numbers into program
    + "numbers\n\t(ending with a negative number): ");

    current = keyboard.nextInt(); // gets the numbers entered by the user

    while((current > 0) && (size < 1001)) // while loop that states when the number is
    { // positive and the amount of numbers is less then 1001
    number [size++] = current; // gives the number entered to the position "size" in the array
    i += 1; // increments the position in the array
    current = keyboard.nextInt(); // gets the number entered by the user and assigns it to "current"
    }


    System.out.println("Enter 2 numbers to be found\n\tin the " +
    "list you just entered: ");

    int num1 = keyboard.nextInt();
    int num2 = keyboard.nextInt();

    int pos1 = -1;
    int pos2 = -1;
    for(i = 0; i < size; i++)
    {
    if(number[i] == num1)
    {
    pos1 = i;
    }
    else if(number[i] == num2)
    {
    pos2 = i;
    }
    }

    System.out.println("That pair"+"("+num1+" and "+num2+")");

    if(pos1 < pos2)
    {
    System.out.println("\tcould be found, in that order, \n\tin the following list:");
    }
    else if(pos1 > pos2)
    {
    System.out.println("\tcould be found, but not in that order, \n\tin the following list:");
    }
    }
    }

  10. #10
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    If neither can be found, that means they were never set if the for loop. Now you know what values they'll have and how to check that. :)

    As for printing the array. You already know how for loops works and how to access array indexes. Just combine your knowledge on these two subjects.

  11. #11
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    i have no idea, i've tried a few different things and to no avail :/

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm wondering, what textbook do you have? Have you been reading it? If your teacher isn't helping you that sucks, but almost all of the java basics can be learned quite fast if you apply yourself. Consider checking out the java tutorials for more help, or even buy another book if you can afford to.

Similar Threads

  1. help..stuck w/ a java programming assignment
    By clemsontigers in forum New To Java
    Replies: 16
    Last Post: 02-19-2014, 05:16 AM
  2. assistance w/ java programming assignment
    By clemsontigers in forum New To Java
    Replies: 4
    Last Post: 04-07-2011, 09:07 PM
  3. Replies: 9
    Last Post: 03-06-2011, 04:00 AM
  4. Replies: 1
    Last Post: 10-02-2009, 02:35 AM
  5. Replies: 2
    Last Post: 12-10-2007, 06:17 AM

Tags for this Thread

Posting Permissions

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