The VG Resource
Some programming help? - Printable Version

+- The VG Resource (https://www.vg-resource.com)
+-- Forum: Archive (https://www.vg-resource.com/forum-65.html)
+--- Forum: July 2014 Archive (https://www.vg-resource.com/forum-139.html)
+---- Forum: Other Stuff (https://www.vg-resource.com/forum-6.html)
+----- Forum: Questions, Info, and Tutorials (https://www.vg-resource.com/forum-89.html)
+----- Thread: Some programming help? (/thread-17660.html)



Some programming help? - Koopaul - 06-20-2011

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;
}
}

}
}





RE: Some programming help? - Barubary - 06-21-2011

(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;
        }
    }
}

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.



RE: Some programming help? - Koopaul - 06-21-2011

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?


RE: Some programming help? - Barubary - 06-22-2011

(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:

(underlined words were supposed to be italics, but using num[i] at the same time doesn't work)


RE: Some programming help? - Work Polo Shirts - 07-15-2011

Thanks for posting it this way!

Logo Polo Shirts
Work Polo Shirts
Sport Polo Shirts