public class ArrayAlgs
{
public static void main(String[] args)
{ new ArrayAlgs(); }
public ArrayAlgs()
{
alpha();
System.out.println("------");
beta();
System.out.println("------");
gamma(15);
System.out.println("------");
gamma(17);
System.out.println("------");
delta();
System.out.println("------");
epsilon(12,15);
System.out.println("------");
omega();
alpha();
}
int[] nums = {12,15,20,19,16,13,15,20};
public void alpha()
{
for (int x = 0; x < nums.length; x = x+1)
{
System.out.println(x + ":" + nums[x]);
}
}
public void beta()
{
int c = 0;
for (int x = 0; x < nums.length; x = x+1)
{
if (nums[c] < 15)
{
c = c+1;
System.out.println(c);
}
}
}
public void gamma(int choose)
{
for (int x = 0; x < nums.length; x = x+1)
{
if (nums[x] == choose)
{ System.out.println(x); }
}
}
public void delta()
{
for (int x = 0; x < 5; x = x + 1)
{
System.out.println( nums[x+1] - nums[x] );
}
}
public void epsilon(int a, int b)
{
for (int x = 0; x < nums.length; x = x+1)
{
if (nums[x] >= a && nums[x] <= b)
{
System.out.println(x + ":" + nums[x]);
}
}
}
public void omega()
{
for (int x = 0; x < 4; x = x+1)
{
nums[x] = nums[x*2];
}
}
}
/****************************
You should be able to READ and WRITE algorithms (methods) to do any of
the tasks listed below:
- print all the numbers in an array
- count all the numbers that are below a specific number
- print all the numbers that are in a certain range (between two numbers)
- find and print the smallest number in an array
- find days where the temperature increased or decreased over night
- search for a specific number in an array
- add up all the numbers in the array
- count how many times a specific number occurs in an array
- calculate the average of all the numbers in an array
- exchange (swap) two numbers in an array, like the first and last
- multiply all the numbers in the array by 2, making them bigger
- add 50% to each price in an array (inflation)
- find and erase a specific
number
- move the first number in the array to the end of the list,
moving
all other numbers ahead one position
You will have a test on Friday 23 January. You will need to READ and
WRITE algorithms like those listed above ON PAPER. Most of the questions
on the test will be similar or identical to those listed above. So you
should PRACTICE by writing algorithms for these tasks.
The program above contains some algorithms that perform some of these
tasks. READ the program and FIGURE OUT what each method does - that is,
what the program will print. THEN run the program and check whether
you were right. If not, FIND OUT WHERE YOUR THINKING WAS INCORRECT.
The test will also contain algorithms that DON'T do what they
are SUPPOSED to do, and you will need to fix them.
Practice!
*****************************/