{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

LEC20080926 - Introduction to Computer Programming I have...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computer Programming September 26, 2008 I have covered most materials from chapter 3 to chapter 8. CSC180 Fall 2008, University of Toronto
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
break Early exit: break escapes one level of nesting. Copy at most n characters from b to a : void strncpy(char a[], char b[], int n) { int i; for (i = 0; i < n; i++) { if (b[i] == ’\0’) break; a[i] = b[i]; } a[i] = ’\0’; } CSC180 Fall 2008, University of Toronto 1
Background image of page 2
continue In while and do-while , continue makes control pass to the conditional test part; in for , it makes control pass to the increment step. Example: int sum_age(int a[], int n) { int sum = 0; while (n) { if (a[--n] < 0) /* skip negative elements */ continue; sum += a[n]; } return sum; CSC180 Fall 2008, University of Toronto 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
} int sum_age2(int a[], int n) { int i, sum = 0; for (i = 0; i < n; i++) { if (a[i] < 0) /* skip negative elements */ continue; sum += a[i]; } return sum; } CSC180 Fall 2008, University of Toronto 3
Background image of page 4
Multi-dimensional arrays Two-dimensional arrays: /* from password.c */ int pool[4][2] = {{ ’*’, 4 }, { ’0’, 10 }, { ’a’, 26 }, { ’A’, 26 }}; A 4 × 2 matrix: ’*’ 4 ’0’ 10 ’a’ 26 ’A’ 26 CSC180 Fall 2008, University of Toronto 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
In memory (row major): addr value addr retrieval 0xbff5b2e0 42 (’*’) <- pool[0] or &pool[0][0] 0xbff5b2e4 4 <- &pool[0][1] 0xbff5b2e8 48 (’0’) <- pool[1] or &pool[1][0] 0xbff5b2ec 10 <- &pool[1][1] 0xbff5b2f0 97 (’a’) <- pool[2] or &pool[2][0] 0xbff5b2f4 26 <- &pool[2][1] 0xbff5b2f8 65 (’A’) <- pool[3] or &pool[3][0] 0xbff5b2fc 26 <- &pool[3][1] Print addresses of all elements in a two-dimensional array: /* address.c */ #include <stdio.h> CSC180 Fall 2008, University of Toronto 5
Background image of page 6
#define ROW 3 #define COL 5 typedef double Type; int main() { Type a[ROW][COL]; int i, j; printf(‘‘a: %#x\n\n’’, a); /* start address */ for (i = 0; i < ROW; i++) { /* address of each element */ for (j = 0; j < COL; j++) printf(‘‘a[%d][%d]: %#x\n’’, i, j, &a[i][j]); printf(‘‘\n’’); } for (i = 0; i < ROW; i++) /* address of each row */ printf(‘‘a[%d]: %#x\n’’, i, a[i]); return 0; } CSC180 Fall 2008, University of Toronto 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
typedef creates new type names. In the above example, Type becomes
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}