LEC20080926 - Introduction to Computer Programming...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Computer Programming September 26, 2008 I have covered most materials from chapter 3 to chapter 8. CSC180 Fall 2008, University of Toronto 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 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 } 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 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 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 #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...
View Full Document

This note was uploaded on 01/10/2011 for the course CSC 180 taught by Professor Na during the Fall '01 term at University of Toronto- Toronto.

Page1 / 23

LEC20080926 - Introduction to Computer Programming...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online