Objectives

Students will be able to:

  • understand that an array is a static data-structure containing homogenous elements.
  • understand that an array has to be initialised with a fixed length.
  • understand that elements in an array can have new data assigned to them.
  • understand that data a loop can be used to iterate through an entire array.

Data structures

 

A scientist measures outdoor temperature 5 times a week.

He is writing a program to process the data a determine the

A random number generator is used to simulate temperatures.

Scientific Simulator

01234

The max temp is

Task

Start a new project in your IDE called ArraysIntro.

Paste the starter code and open the worksheet.

Note: if you are struggling with this unit, the helper video covers the concepts in the worksheet.

Observations

  • An array exists in computer memory.
  • The values in an array are homogenous ie they all have the same datatype
  • An array has a fixed length ie it is static, not dynamic
  • A computer program can communicate with it because there is a reference variable, nums
  • The array has 5 elements
  • Each element has a unique index, starting at 0
  • The value in tempData[0] is
  • The value in tempData[1] is
  • The value in tempData[2] is
  • total = tempData[0] + tempData[1] + tempData[2] + teampData[3] + tempData[4];

Processing an array with a loop

So each element in an array has an index, starting at 0.

We can use this

int[ ] nums = {13,16,34,65,34,76}; //this is a shortcut to initialising an array!

int total = 0;

for(int i = 0; i< nums.length; i++){ //nums.length is handy!

total = total + nums[i];

}

System.out.println("The total is "+total);

Coding 1

Design a function countTemp(int threshold) which returns a count of all values in the global array referenced by nums which are less than our equal to the parameter threshold.

Coding 2

Design a function getTotal() which returns the sum of all values in the global array referenced by nums.

Coding 3

Design a function getAveragel() which returns the average of all values in the global array referenced by nums.

Your solution must call the getTotal function to help solve this problem.

Helper video

If you get it, let's continue to the exercises.

Tags

iterateauto-indent variablecompile sum machine code count-controlled loop integer pseudocodesyntax