Download presentation

Presentation is loading. Please wait.

Published byDestiny Wells Modified over 4 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 ? **

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**

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

8
**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

9
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

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

11
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)

12
**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

13
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

Similar presentations

OK

Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.

Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google