Posts: 6,055
Threads: 111
Joined: May 2008
Yeah I have a Java programming online course, I've been doing decent but now we're at Arrays and I'm starting to get confused.
Anyway I need help figuring out the value of the num array with this program. I think I have an idea but I'm not sure. Take a look.
public class QuestionTwo
{
public static void main(String args [])
{
int [] num= {20,50,10,70,40,80,30,90,60};
int t=0;
for (int i=0; i<num.length; i++)
{
for (int j=i+1; j<num.length; j++)
if (num[i]>num[j])
{
t=num[i];
num[i]=num[j];
num[j]=t;
}
}
}
}
Posts: 231
Threads: 2
Joined: May 2008
(06-20-2011, 10:46 PM)Koopaul Wrote: Yeah I have a Java programming online course, I've been doing decent but now we're at Arrays and I'm starting to get confused.
Anyway I need help figuring out the value of the num array with this program. I think I have an idea but I'm not sure. Take a look.
I tried to provide the answer stepwise, such that you can try and figure out the rest yourself after a certain step, or go to the next one if you are stuck.
The 'thumb' and 'finger' metaphores I use below are what I've been taught with, and correspond to how you could do it on paper (which usually helps with these kind of problems).
Code: // creation of an array with some initial values.
// The i'th value is accessed using num[i] starting at 0, so num[2] is currently 10
int [] num= {20,50,10,70,40,80,30,90,60};
int t=0;
// place the thumb on each position in the array, 'from left to right'
for (int i=0; i<num.length; i++) {
// go with the finger through the rest of the array
for (int j=i+1; j<num.length; j++) {
// whenever the value at the finger is less than the value at the thumb, do something
if (num[i]>num[j]) {
t=num[i];
num[i]=num[j];
num[j]=t;
}
}
}
I could just tell you what the 'do something' is, but it may be more helpful to try and to this with some paper;
- write a couple of numbers (or any other type of value of course) next to each other in boxes (a horizontal ladder, as it were); this is the array
- for convenience, you could write the index of each element in the array above/below it. Be sure to start with 0.
- draw an empty box somewhere else on the paper. This represents the value of ' t'. (you could fill it with a 0, as it's t's initial value)
- pick any value for i and j, for which i < j and both i and j are valid indices of the array (the array should have a number at both i and j).
- the perform the 'do something' on the paper;
-- erase the value at t, and replace it with the value of the i'th element in the array
-- erase the value at the i'th element in the array, and replace it with the value of the j'th element in the array
-- erase the value at the j'th element in the array, and replace it with the value of t - If you remember the original set of values of the array (or if you wrote them down / re-used the example array from the exercise), you should be able to see what has just happened. (you can ignore the value of t for that; it's the t of Temporary, and is only used to enable the operation that is being performed in the 'do something' part)
-- It swaps the values at location i and j in the array
Once you know what 'do something' does, the next step is figuring out what it does to the array when used in the program above.
- the value at the thumb is swapped with the value at the finger whenever the value at the finger is less than the value at the thumb - Will the value at the thumb ever decrease?
No, it is only swapped when the thumb is higher than the finger. - Will there be a value 'to the right of' the thumb that is smaller than the thumb?
No. The thumb is always the minimum value between the thumb and the finger (inclusive). - Since the thumb is moved from left to right, this, means that for every element in the array, all values to the right will be ... - ergo, the values are not decreasing when read from left to right. - ergo, the array is sorted in ascending order
Posts: 6,055
Threads: 111
Joined: May 2008
Thanks for posting it this way! It's better if I figure it out myself.
But I'm going to use your advice,
So let's see:
int i is to the length of the num array which would be
0
1
2
3
4
5
6
7
8 right?
int j is equal to int i plus 1 which would be
1
2
3
4
5
6
7
8
9
num[i] is less than num[j] which means t equals num[i] which is equal to num[j]?
21
52
13
74
45
86
37
98
69
Am I on the right track?
Posts: 231
Threads: 2
Joined: May 2008
(06-21-2011, 04:39 PM)Koopaul Wrote: Thanks for posting it this way! It's better if I figure it out myself.
But I'm going to use your advice,
So let's see:
int i is to the length of the num array which would be
0
1
2
3
4
5
6
7
8 right? Theoretically correct, however these are not the values that you will see when you would print the value of i inside the for-loop. The contents of the loop are only executed while the given condition holds, which is i < num.length. The length of the array is 8.
(see below for an example of how a for-loop works)
Quote:int j is equal to int i plus 1 which would be
1
2
3
4
5
6
7
8
9
Since j is again defined and used in a for-loop, it will have a range of values for every i. j does indeed start at i+1, but it will again end when the condition evaluates to false.
Quote:num[i] is less than num[j] which means t equals num[i] which is equal to num[j]?
Keep in mind that the lines are executed sequentially. First the value of t is set to the current value of num[i]. Only after that operation has been completed will the other lines be executed.
Quote:21
52
13
74
45
86
37
98
69
The only additions in the program are on variables i and j. These two variables are only used as indices to the array, and will thus never be added to the values in the array. With the example code, the values of the array will never contain any number that is not one of the set {20,50,10,70,40,80,30,90,60}.
Just in case, here's how a for-loop structure is evaluated:
Given the following pseudo-code;
Code: for(DEF; COND; INCR) {
CODE
}
This could be rewritten as: (there are probably some obscure cases where this will not work, but it should suffice for now)
Code: {
DEF;
while (COND) {
CODE;
INCR;
}
}
In other words;
- Before anything else, the DEF-code is executed. This will usually be definition or assignment of some counter.
- The COND (condition) is checked.
- If COND evaluates to true, the CODE is executed, after which INCR is executed. INCR usually increments the counter defined in DEF. After this, the COND is checked again.
- Once the COND evaluates to false, the loop stops.
(- if a variable was defined in DEF, it will not be visible anywhere else. hence the surrounding brackets)
With example code:
Code: for(int i = 0; i < 3; i++) {
System.out.print(i + " ");
}
System.out.println(".");
DEF Wrote:- define variable i
- initialize i to 0
COND Wrote:- evaluate i < 3.
- 0 is less than 3 -> 'true' CODE Wrote:- print i (which is 0) followed by a whitespace INCR Wrote:- increment i by 1.
COND Wrote:- evaluate i < 3.
- 1 is less than 3 -> 'true' CODE Wrote:- print i (which is 1) followed by a whitespace INCR Wrote:- increment i by 1.
COND Wrote:- evaluate i < 3.
- 2 is less than 3 -> 'true' CODE Wrote:- print i (which is 2) followed by a whitespace INCR Wrote:- increment i by 1.
COND Wrote:- evaluate i < 3
- 3 is not less than 3 -> 'false' The loop ends, and a '.' and newline-character is printed.
(underlined words were supposed to be italics, but using num[i] at the same time doesn't work)
Posts: 4
Threads: 0
Joined: Jul 2011
|