250 likes | 265 Views
Learn about control statements, character strings, arrays, and more programming concepts. Includes code examples and explanations.
E N D
Your questions from last session • If your computer has problem with “–m32”. Your problem will be probably fixed by typing: sudo apt-get install libx32gcc-4.8-dev sudo apt-get install libc6-dev-i386 • To show exponent numbers in printf, you should use %e instead of %d
Counting Lines #include <stdio.h> int main (void) { int c, m; m = 0; /* a counter */ while ((c=getchar( )) != EOF) if (c= = ‘\n’) ++m; printf(“%d\n”, m); return 0; }
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! • if(c = ‘\n’) is the same as if(c) • And the expression is treated as true (‘\n’ is not = 0)
What’s new? Prefix: increment m before m is used • Incrementing a variable Shorthand ++m; Shorthand m++; Equivalent to m = m + 1; • Decrementing a variable Shorthand --m; Shorthand m--; Equivalent to m = m – 1; Postfix: increment m after m is used m = 3; c = m++; // c = ? m = ? c = ++m; // c = ? m = ?
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!
Review of Control Statements • do while Statement do { statements; } while (logical expression); • Do while does execute the statements at least once even if the logical expression is false!
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! • Generally use for when you have an index that changes each iteration. Use while when you don’t.
Review of Control Statements • if-else Statement if (logical expression) { statements when expression is true; } else { statements when expression is false; }
Review of Control Statements • if-else-if Statement if (logical expression 1) { statements when expression 1 is true; } else if (logical expression 2) { statements when expression 1 is false, 2 is true; } else if (logical expression 3) { statements when expression 1,2 are false, 3 is true; } else { statements when expression 1,2,3 are false; } • Only one of the blocks of statements will run!
No brace case While(c != 10) for(i= 0; i < 5; i++) if(j == 3) printf(……); Single statement Single statement Single statement Printf(…); Indentation is important, making your program easy to read!
Arrays / Character Strings • Character string is an array of character type values (integers) ending with a null character (\0) • “array[]” is “a pointer” to sequential memory locations containing elements of defined type • Individual element n is accessed as “array[n]”
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’ ‘l’ ‘o’ ‘\n’ ‘\0’
Numbering systems The only thing computers understand is the numbers. The getchar() reads the numbers representing the characters (ASCII value representation) Binary: 0s and 1s Octal: 0, 1, 2, …, 7 Hexadecimal: 0, 1, 2, …, 9, A, B, C, D, E, F Octal -> 3 5 3 Binary -> 0 1 1 1 0 1 0 1 1 Hexa -> 0 E B Char Decimal ‘0’ 48 Octal Hexadecimal 30060
Arrays / Character Strings #include <stdio.h> /* count digit characters 0-9 coming from stdin */ int main(void) { int c, i; /* c for getchar - ASCII code for integers */ int ndigit[10]; /* subscripts 0 through 9 */ for (i = 0; i <= 9; ++i) /* Set all array value = 0 */ ndigit[i] = 0;
Arrays / Character Strings (cont’d) while ((c = getchar()) != EOF) if(c >= '0' && c <= '9') /* if c is a digit */ ++ndigit[c-'0']; /* increment 1 array element */ printf("digits = "); for (i = 0; i <= 9; ++i) printf("%d ", ndigit[i]); printf("\n"); return 0; }
Arrays / Character Strings % gcc count.c % a.out 123456789011222333344444555555677888999000 fgfgfgfg (Note: These won’t be counted as digits) ^D (Control-D is End of File – EOF) digits = 4 3 4 5 6 7 2 3 4 4 %
Program: maxline • Program to figure out which line is the longest, print it. • 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”
There is another line • Put this as a new “function” • Function name: getline • Function input: a character array to save the next line • Function output: the length of the next line
Copy one character array to another (save the longest line) • Put this as a new “function” • Function name: copy • Function input: two character arrays • Function output: void
Program: maxline #include <stdio.h> /* define maximum length of lines */ #define MAXLINE 1000 /* define function prototypes */ int getline(char line[], int maxline); void copy(char to[], char from[]); Declare function prototypes such that main function knows their existence
Program: maxline (cont’d) /* print longest input line */ int main (void){ int len, max; /* initialization */ char line[MAXLINE], longest[MAXLINE]; max = 0; while ((len = getline(line, MAXLINE)) >0) if (len > max) { /*You found a longer line*/ max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf (“%s”, longest); }
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; } K&R p.30 -an array of characters; length unspecified The variable name could be different, but the position matters …… array[0] array[1] array[2] array[3] array[4] array[5] array[6] …… ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\n’ ‘\0’
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; } an array of characters; length unspecified
Notes on the Details • Precedence of operators in getline( ) - (expression1 && expression2 && expression3) • i < lim-1; • ((c = getchar()) != EOF); • ++i • Pass by address arguments for copy (pointers) • void copy(char to[], char from[]) • while ((to[i] = from [i]) != ‘\0’)
Show Hex “Octal” Dump Show Character • Use “od –xc” to see hex dump of a file Character ASCII value in Hex Byte offset