Presentation is loading. Please wait.

Last week lecture Loops (lets look at for loop again) Dry Runs

Presentation on theme: "Last week lecture Loops (lets look at for loop again) Dry Runs"— Presentation transcript:

Last week lecture Loops (lets look at for loop again) Dry Runs
Nested layouts - if then else statements, loops More complex loops Test plans

This week lecture - Arrays
Data is stored in a computer in a logical way so that it can be found as quickly as possible Consider a telephone directory – how is data stored The way data is stored is called the structure of the data, hence the term data structures You have come across one data structure – which one ? Another type of data structure is an array Often we solve problems in everyday life by using tables Can you think of when you may want to use a table to organise you data?

What is an Array We are interested in one-dimensional arrays
An array has a fixed number of elements and all elements are of the same type Each box has an index which in java and C++ starts with 0 Here is an array which holds the ages of 10 people

Lets look at this array in detail What do you notice about the array ?
Lets look at this array in detail What do you notice about the array ? Is the data organised in any particular way?

Examples of Arrays Draw an array of 20 elements which contains student marks – what type will it be ? Draw an array of 15 elements which contains student grades ranging form A-E – what type will it be?

Entering data into an array
When you enter data into an array we use a loop What type of loop do you think we will use? Hint – we know the number of elements there are in the array Use a counter to keep track of how many elements are entered into the array

Entering data into an array
Read Algorithm Loop : R = 1 to 10 Enter A( R) Loop end The number of elements in array is 10 The counter of loop (R ) allows the computer to increase the element number by 1 each time a piece of data is entered into a memory location The computer can find a particular element in the array by using the reference number index represented by R R = loop counter A(R ) = element R in the A array R 10 1 Enter A(R ) R B

Accumulating the elements of array
Calc You may need to sum the elements of an array Initialise the sum variable which contains the total to zero The instruction Sum = Sum + A(R ) tells the computer to add the valve of element A(R ) to the old valve of the sum (Sum) and to store the result into sum memory location (Sum) Algorithm Loop : R = 1 to 10 sum = Sum + A( R) Loop end The number of elements in array is 10 The counter of loop (R ) allows the computer to increase the element number by 1 each time a piece of data is entered into a memory location Sum = Sum of the elements of A A(R ) = element R in the A array R 5 1 Sum = Sum + A(R ) R B

Dry run Dry run the previous array algorithm using the following data (remember sum was 0 initially) Loop counter Array valves sum 1 2 4 6 8 10

Dry run Dry run the previous array algorithm using the following data (remember sum was 0 initially) Loop counter Array valves sum 1 2 4 6 3 12 8 20 5 10 30

Question Trendyclothes is a corporation that sells clothes and has 10 stores. Write an algorithm and flowchart to read the sales figures for all 10 stores for Trendyclothes into an array Do a dry run for entering the sales data Dry run should only include 2 columns, one for array index element and second for sales figures for each store Sales figures :- 200 ,300,150,40,25,233,256,123,562, 128 c) Write an algorithm and flowchart to print the array element and the sales figures of the array (remember to print the headings) (for those who want something more challenging – in the lab session - print the sales figures as a percentage of total sales of the corporation also Hint – you will need to sum the sales first and then work out the sales percentage for each store)

Answer Entering the Sales valves into an array b) answer
Loop counter Sales 1 200 2 300 3 150 4 400 5 275 6 125 7 175 8 450 9 475 10 350 Read Algorithm Read (*Sales) Loop : R = 1 to 10 Enter Sales Loop end The number of elements in array is 10 The counter of loop (R ) allows the computer to increase the element number by 1 each time a piece of data is entered into a memory location A(R ) = element R in the A array R 10 1 Enter Sales (R ) R B

Answer c) Print Loop counter Sales 1 200 2 300 3 150 4 400 5 275 6 125 7 175 8 450 9 475 10 350 Print headings Algorithm Print (store number, Sales, Percentage) Loop : R = 1 to 10 Enter Sales Loop end The number of elements in array is 10 The counter of loop (R ) allows the computer to increase the element number by 1 each time a piece of data is entered into a memory location A(R ) = element R in the A array R 10 1 Print R, Sales (R ) Percent(R ) R B

Download ppt "Last week lecture Loops (lets look at for loop again) Dry Runs"

Similar presentations

Ads by Google