Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?

Similar presentations


Presentation on theme: "1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?"— Presentation transcript:

1 1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books? HW2 (due class 6) on line now DON’T USE and string library functions, e.g. strlen ( ) – Write your own!

2 2 Counting Lines #include int main ( ) { int c, m; m = 0; while ((c=getchar( )) != EOF) if (c== ‘\n’) ++m; printf(“Number of lines = %d\n”, m); return 0; }

3 3 What’s new? if (c == ‘\n’) –If statement with logical expression in parentheses Result of comparison equal to 0 is treated as False Result of comparison not equal to 0 is treated as True –The expression is a check for int c equal to ‘\n’ or not –Use double equals (==) for checking “equals” condition if (c = ‘\n’) –If int c wasn’t equal to ‘\n’ before, it is now! –And the expression is treated as true (‘\n’ is not = 0)

4 4 What’s new? Incrementing a variable Shorthand ++m; Shorthandm++; Equivalent to m = m + 1; Decrementing a variable Shorthand--m Shorthandm-- Equivalent tom = m - 1

5 5 Review of Control Statements While Statement while (logical expression) { statements while expression is true; } While does not execute any statements if the logical expression is false upon entry!

6 6 Review of Control Statements For Statement for (initialize; loop test; increment) { statements for expression is true); } For does not execute any statements if the loop test is false after initialization!

7 7 Review of Control Statements If-else Statement if (logical expression) { statements when expression is true; } else { statements when expression is false; } “Else” portion of statement is optional!

8 8 Review of Control Statements If-else-if Statement if (logical expression) { statements when expression is true; } else if (logical expression) { statements when expression is false; } else if (logical expression) …. Only one of the blocks of statements will run!

9 9 Arrays / Character Strings Unlike Java, C has no “special” string type Character string is an array of character type values (integers) ending with a 0 (‘\0’) “array[]” is “a pointer” to sequential memory locations containing elements of defined type Individual element n accessed as “array[n]”

10 10 Arrays / Character Strings Defining/initializing an array to contain string “hello” plus an end of line character: char array[7] = “hello\n”; Sets up memory locations as follows: array[0]array[1]array[2]array[3]array[4]array[5]array[6] ‘h’‘e’‘l’ ‘o’‘\n’‘\0’

11 11 Arrays / Character Strings /* count.c: count digit s 0-9 coming from stdin */ #include int main( ) { int c,i; /* c for char - ASCII code for integers */ char ndigit[10]; /* subscripts 0 through 9 */ for (i=0;i<=9;++i) /* clear the count array */ ndigit[i]=0;

12 12 Arrays / Character Strings while ((c=getchar())!=EOF) if(c>='0' && c<='9') /* if c is a digit */ ++ndigit[c-'0']; /* add one to its counter */ printf("digit count\n"); /* print heading */ for (i=0;i<=9;++i) /* print counts */ printf("%5d %5d\n",i, ndigit[i]); return 0; }

13 13 Arrays / Character Strings u18(14)% gcc -Wall -o count count.c u18(15)% count 123456789000000000000000044444444444447777777777777fgf digit count 0 16 1 1 2 1 3 1 4 14 5 1 6 1 7 14 8 1 9 1 u18(16)%

14 14 Program: maxline Outline of maxline program (“Pseudocode”) while (there’s another line) if (it’s longer than the previous longest) save it save its length print longest line Large enough to break up into “functions”

15 15 Program: maxline #include /* define maximum length of lines */ #define MAXLINE 1000 /* define our function prototypes */ int getline(char line[], int maxline); void copy(char to[], char from[]);

16 16 Program: maxline /* print longest input line */ int main ( ) { /* initialization */ int len,max; char line[MAXLINE], longest[MAXLINE]; max = 0;

17 17 Program: maxline while ((len = getline(line, MAXLINE)) >0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf (“%s”, longest); }

18 18 Function: getline( ) /* getline: read a line into s, return length */ int getline(char s[], int lim) { int c, i; for (i=0; i<lim-1 && (c=getchar())!=EOF && c!=‘\n’; ++i) s[i] = c; if (c == ‘\n’) { s[i] = c; ++i; } s[i] = ‘\0’; return i; }

19 19 Function: copy ( ) /* copy: copy ‘from’ into ‘to’ assume size of array ‘to’ is large enough */ void copy (char to[], char from[]) { int i; i = 0; while ((to[i] = from[i]) != ‘\0’) ++i; }

20 20 Notes on Details Precedence of Operators in getline( ) –i < lim-1 –(c = getchar()) != EOF –(expression && expression && expression) Pass by value arguments for copy (pointers) –void copy(char to[], char from[]) –while ((to[i] = from [i]) != ‘\0’)


Download ppt "1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?"

Similar presentations


Ads by Google