LEC20080925 - Introduction to Computer Programming...

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

View Full Document Right Arrow Icon
Introduction to Computer Programming September 25, 2008 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 DocumentRight Arrow Icon
Arrays Required readings: chapter 8. An array contains n consecutive elements of same type, where n is array size. Example: int a[5]; /* an array of 5 integers */ char pwd[20]; /* an array of 20 characters */ char *argv[10]; /* an array of 10 pointers */ An array subscript (index) starts from 0: CSC180 Fall 2008, University of Toronto 1
Background image of page 2
int a[5]; /* initialize array a */ a[0] = a[1] = a[2] = a[3] = a[4] = 1; It is not a good idea to modify a[5] . A subscript can be any integer expression. Example: putchar(c - pwd[count++ % len]); /* from encrypt.c */ Example: 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 DocumentRight Arrow Icon
/* list.c */ #include <stdio.h> /* list command-line arguments */ int main(int argc, char *argv[]) { int i = 0; while (argc--) printf(‘‘%s\n’’, argv[i++]); return 0; } Example: /* count.c */ #include <stdio.h> CSC180 Fall 2008, University of Toronto 3
Background image of page 4
/* count digits, white space, and others */ int main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; i++) ndigit[i] = 0; while ((c = getchar()) != EOF) { ++ndigit[c-’0’]; else if (c == ’ ’ || c == ’\n’ || c == ’\t’) ++nwhite; else ++nother; } printf(‘‘digits =’’); for (i = 0; i < 10; i++) printf(‘‘ %d’’, ndigit[i]); 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 DocumentRight Arrow Icon
printf(‘‘, white space = %d, other = %d\n’’, nwhite, nother); return 0;
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the 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 / 18

LEC20080925 - Introduction to Computer Programming...

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

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