# need help with basic programming assignment (with arrays)

• 05-02-2011, 09:47 PM
magictricks1020
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
• 05-02-2011, 10:14 PM
Fubarable
So what's your question? What have you tried before coming here?
• 05-02-2011, 10:17 PM
magictricks1020
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 )")
}
}
• 05-02-2011, 11:32 PM
ojn
Quote:

Originally Posted by magictricks1020
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. :(
Quote:

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.
• 05-02-2011, 11:36 PM
sunde887
What does your teacher do then?
• 05-02-2011, 11:41 PM
ojn
He just told us. Not teaching Java. :)
• 05-02-2011, 11:45 PM
magictricks1020
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.
• 05-02-2011, 11:59 PM
ojn
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:
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.
• 05-03-2011, 12:01 AM
magictricks1020
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:");
}
}
}
• 05-03-2011, 12:17 AM
ojn
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.
• 05-03-2011, 02:03 AM
magictricks1020
i have no idea, i've tried a few different things and to no avail :/
• 05-03-2011, 02:19 AM
sunde887
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.