CS计算机代考程序代写 Lecture 03

Lecture 03

Connect to slido.com
#ece209

ECE 209
Computer Systems Programming
Spring 2021

Lecture 06

Announcements
Z3 was moved to Thursday, 2/18
Program 1 moved to Friday, 2/26
Problem session PS2: 2/17 — CLion!!!
Quiz 2: 2/24

Photo by Patrick Fore on Unsplash

https://unsplash.com/@patrickian4?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
https://unsplash.com/s/photos/announce?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText

New Data Types:
Arrays
Strings

An array is a sequence of values in a single variable.
int x;

x

int x[4];

x[0]

x[1]

x[2]

x[3]

An array is a sequence of values in a single variable.
double a;

a

double a[3];

a[0]

a[1]

a[2]

Array Properties
(1) Array has a fixed size, specified when it is created (declared).

type name[size];

Can be any type.

Normal rules for variable name.

Size must be a positive integer.
(Can be expression.)

If no initializer, array values are undefined.

Array Properties
(2) Array is allocated as one contiguous sequence of locations.

This will become important later…

int x[4];

x[0]

x[1]

x[2]

x[3]

Array Properties
(3) Array elements are accessed by index.

This will become important later…

int x[4];

x[0]

x[1]

x[2]

x[3]

Index is an integer expression.

x[2]
x[j]

x[i+3]
x[–j]

Index of first element is 0.
Index of last element is size-1.

Example

// array of grades for a class
double grades[142];

// calculate average grade
avg = 0;
for (i=0; i<142; i++) { avg += grades[i]; } avg = avg / 142.0; "traversing" an array visit each element and do something Example // array of grades for a class double grades[142]; // what is max grade? max = grades[0]; for (i=1; i<142; i++) { if (grades[i] > max)
max = grades[i];

}

“traversing” an array
visit each element and
do something

Start by assuming first
item is min/max.
Avoids bad assumptions
about range of data in array.

Example

// array of grades for a class
double grades[142];

// how many grades are 90 or above?
count = 0;
for (i=0; i<142; i++) { if (grades[i] >= 90.) ++count;

}

“traversing” an array
visit each element and
do something

Caution

// calculate average grade
for (i=0; i < num_students; i++) { avg += grades[i]; } Cannot tell how big the array is by "looking" at it. There is no special value that indicates the end of an array. Must know the size (but could be a variable). Array Properties (4) Array may be initialized. More details on next slide... 10 int x[4] = {10, 20, -5, 3}; x[0] 20 -5 3 x[1] x[2] x[3] type name[size] = {val0, val1, val2, ...}; Element 0 initialized to val0. Element 1 initialized to val1. Element 2 initialized to val2. etc. type name[size] = {val0, val1, val2, ...}; If the number of values is less than size, unspecified elements will be initialized to zero. type name[] = {val0, val1, val2, ...}; If size is missing, will be determined by number of initializer values. This is the ONLY time when array can be declared with an empty size. int a[3]; // elements are undefined double b[4] = {1., 3., 3e3, -99.} int c[10] = {11, 12, 13}; int d[] = {50, 100, 150}; // array size = 3 int e[]; // illegal, unspecified size int f[n]; // ok, if n is defined int g[n] = {0, 1, 2}; // cannot initialize dynamic array int h[100] = {0}; // easy way to initialize to zero Array Properties (4) Array expression is an Lvalue -- specifies a place. Can be assigned. Can be incremented/decremented. 10x[0] 20 -5 3 x[1] x[2] x[3] x[0] = 32; b = x[2]; c = ++x[i]; x[a+b] = z; No Assignment Assignment operator only works on array element, not on entire array. int a[10]; int b[20]; a = b; // illegal a[0] = b[19]; a[j] = b[i]; Array Properties (5) C does not check whether index is within array bounds. 10x[0] 20 -5 3 x[1] x[2] x[3] x[4] = 32; i = -10; c = ++x[i]; It is your job, as the programmer, to make sure that array index is within the proper range. legal, but a bad idea Example // array of integers int num[n]; // set all elements to zero for (i=0; i