Download presentation

Presentation is loading. Please wait.

Published byDestiny Wells Modified over 2 years ago

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

2
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?

3
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

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

5
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?

6
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

7
Entering data into an array 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 Read R Enter A(R ) R B

8
Accumulating the elements of array 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 R151R151 Sum = Sum + A(R ) R B Calc

9
Dry run Dry run the previous array algorithm using the following data ( remember sum was 0 initially) Loop counter Array valves sum

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

11
Question a)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 b)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)

12
a)Answer Entering the Sales valves into an array b) answer 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 Read R Enter Sales (R ) R B Loop counter Sales

13
a)Answer c) 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 Print R Print R, Sales (R ) Percent(R ) R B Loop counter Sales Print headings

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google