LEC20080925 - Introduction to Computer Programming CSC180...

Info icon This 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
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right 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
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) { if (c >= ’0’ && c <= ’9’) ++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
Image of page 5

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

View Full Document Right Arrow Icon
printf(‘‘, white space = %d, other = %d\n’’, nwhite, nother); return 0; } Exercise: improve count.c such that it can count occurrences of each letter from
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern