EE273 - lecture 05-long PDF

Title EE273 - lecture 05-long
Author James Smythe
Course Engineering Design For Software Development 2
Institution University of Strathclyde
Pages 23
File Size 1000.6 KB
File Type PDF
Total Downloads 19
Total Views 139

Summary

Lecture Notes for 5th lecture...


Description

EE273/985

Lecture 5 Arrays and memory allocation

Dr David Harle Dr Christos Tachtatzis EE273/985 – Engineering For Software Design

©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell

1

This Section

o Arrays •

Declaration, initialisation, access elements



Use in loops

o Arrays as arguments in functions o Dynamic memory allocation o Brief introduction to pointers

EE273/985 – Engineering For Software Design

©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell

2

What is an array? Arrays: variables that can memorize multiple data and stored sequentially in numbered array’s elements. So far, you have encountered variables that can only store a single piece of data (lecture 2)

In C++, indexing of the elements starts at zero . Think of an array as a set of variables e.g.



X is the name of the array variable and the indices are which unit of the train you want to work with, so X[1] is the box marked 1.

EE273/985 – Engineering For Software Design

©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell

3

What is an array? Properties: •

The individual data items in the array are called elements. •



All elements are stored contiguously in the computer’s memory •



All elements must be of the same data type.

In C and C++, the index of the 1st element is zero.

What is stored against the name of the array is actually the address of the first element of the array. Important! • •

What is stored is a pointer to the start of the array

The number in the [ ] is an offset in memory from that first location

EE273/985 – Engineering For Software Design

©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell

4

Array declaration  To declare such an array int X[6];

in the same way as normal variables except the size of the array must be specified in the declaration, in square brackets after the array name

 This declared a variable X that holds an array of 6 values.  X has 6 spaces in the array. • •

First element is X[0] Last element indexed X[5]

 In this case, the 6 places can only be filled with integer numbers. EE273/985 – Engineering For Software Design

©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell

5

Allocating values to elements in an array X[2] = 8; This means set X at position 2 (3rd element of the array) to the value 8

0 1 2 3 4 To declare a two dimensional array myarray[][]

int myarray[3][6];

0

myarray

1 2 0

1

2

3

EE273/985 – Engineering For Software Design

4

5

Row offset

How would you set the element in the 2nd row and 5th column of the 2D array to a value of 10?

myarray[1][4] = 10 ; y of Strathclyde

Column offset

arle, K.Bell

6

Why use an array?

 If we would like to store and add the scores for this course: float assignment1, assignment2, assignment3, assignment4, total_marks; total_marks = assignment1 + assignment2 + assignment3 + assignment4;

EE273/985 – Engineering For Software Design

©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell

7

Why use an array? o Alternatively: st float assignment[4],total_mark The 1 element is has index 0 total_marks=assignment[0]+assignment[1]+as signment[2]+assignment[3]; o Or using loops: Don’t forget to initialise the total_marks = 0; accumulator! for(i=0;i...


Similar Free PDFs