Category Archives: TEALS

Brush up

Central Connecticut State University has great CS lessons online. We’re going to go through a few lessons to make sure we’re ready for projects I’d like to start in the near future.

If you think you know these subjects, finish the quiz or the exercises and then I’ll give you something more challenging to work on.

Array Practice Problems

  1. Declare and define double array that can hold up to 100 elements.
  2. Given an array called myNums get the length of the array and store it in a variable.
  3. Declare and define an integer array and initialize every value in the array to 1.
  4. Write a function that accepts 2 arguments a Boolean array and an integer, I want it to return a Boolean. The function only returns true if the number of true Boolean values in the array counts up to the integer passed into the function.
  5. Write a function that accepts a sorted integer array as an argument and it returns the largest value in the array. Do this without using a loop.
  6. Write a function that accepts an unsorted integer array as an argument and it returns the smallest value in the array.
  7. Represent a two dimensions inside of a single dimension array. For instance, this array could be storing objects that appear in a 100 x 100 grid. Get the object in the 3rd row and 70th column.
  8. Create an array that contains 10 integer arrays. Each integer array must contain 10 numbers 1-10, 11-20, 21 -30, …
  9. Write a function that accepts an integer array as an argument, and it returns an array that doesn’t contain any duplicate numbers. So if 1 appeared twice in the array passed in the argument, the returned array will only have one occurrence of the number 1.
  10. Given an unsorted array of size n containing the integers 1 through n in random order with one element randomly replaced by 0, determine the missing element most efficiently.

 

* Note – some of these questions were pulled from other sources on the Internet.

Arrays

Based on what we know, how do we represent a collection of things in a computer?

We must instantiate a variable for each thing we want to keep track of. For instance, this is a way that I could represent students in a classroom:

String student1 = "David";
String student2 = "Ethan";
String student3 = "T.V.";
String student4 = "James";
...

Another example is a collection of test scores from an exam:

int score1 = 70;
int score2 = 86;
int score3 = 94;
int score4 = 89;
…
int score25 = 32;

Now what if I wanted to average all of the test scores, what do I have to do?

double average = (score1 + score2 +score3 + score4 + … + score25) / 25; // this would be a super long line if completely filled out

Wouldn’t it be nice if we could manage collections of similar items in a more succinct way? Wouldn’t it be nice to reference the collection as a whole and grab more granular items when we want?

We can manage collections with arrays!

What is an array? It’s a data structure that has a fixed length and can hold 0 or more items of a specific type.

In Java arrays have the following syntax:

 
type[] variableName = new type[]; // the length of the array denotes the maximum number of elements you can store in the array.

Here’s an integer array that can hold 10 elements:

int[] myIntegerElements = new int[10]; // how many elements are inside of the array?

You can access elements inside of the array by indexing the array. Check out the two examples below. You’ll notice that you need to use brackets with the position number of the element you want to retrieve after the variable name. Keep in mind that arrays are zero based indexed so the first element is referenced by asking for the item indexed by zero.

int firstElem = myIntegerElements[0]; // what is the value stored in firstElem after this line executes?

What if I want the last element?

int lastElem = myIntegerElements[9]; // what is the value stored in lastElem after this line executes?

Also, arrays have instance variables that are pretty useful. For example, an array can tell you its length.

int arrayLength = someRandomArray.length; // assume someRandomArray is already declared and defined.

In what ways is an array’s length useful?

Say I wrote a function that accepts an integer array as an argument and it averages the numbers. What would that function look like?

public double AverageNumsInArray(int[] numArray){

	int sum = 0;
	for(int i = 0; i < numArray.length; i++){
		sum = sum + numArray[i];
	}
	return (sum/numArray.length);
}

What if I wanted a function that accepted an integer array and an integer called numToLookFor as arguments and it told me whether or not the integer numToLookFor existed inside of the array. What would that function look like?

public bool DoesIntegerExistInArray(int[] numArray, int numToLookFor){
    for(int i = 0; i < numArray.length; i++){
        if(numArray[i] == numToLookFor){
            return true;
        }
    }
    return false;
}