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; } Memory allocation for int -> 4Bytes Draw the memory chip diagram again Compiler saves the address of variables Differennce of = and == ‘\n’ ascii code
What’s new? if (c = = ‘\n’) 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) 0 -> false. Anything else -> true
What’s new? Incrementing a variable Decrementing a variable Shorthand ++m; Shorthand m++; Equivalent to m = m + 1; Decrementing a variable Shorthand --m; Shorthand m--; Equivalent to m = m – 1; Prefix: increment m before m is used 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; } More specific! An example
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]” If you don’t have a null at the end of the string and you want to print the string, the printf will continue to print until to find a null character!
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: We need at least 7 here Ascii value of h e l l … Start index -> 0 array[0] array[1] array[2] array[3] array[4] array[5] array[6] ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\n’ ‘\0’
Numbering systems Binary: 0s and 1s Octal: 0, 1, 2, …, 7 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 Show the ASCII table Some weird characters in ascii. They are special characters used by IBM in the past You manipulate the characters by their ascii number Octal numbers -> grouping the binary numbers by 3. but 8 16 32 is not devisable by 3 So group them by 4 -> hexadecimal Show these numbers in decimal Char Decimal ‘0’ 48 Octal Hexadecimal 30 060
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; 0 to 9 means 10 times
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; } Skip other characters than numbers Show the ascii code chart again Figure it out where that character is and then increment that Subtract the value from ascii value of 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” Hw2 begins from here Explain trimming the end of the lines with an example. Vertical tab horizontal tab space, … Talk about pseudo code. You should not start to code after you see a question. You should think about it and try to write a pseudo code first to get idea. You write down pseudo English and avoid using C syntax. By reading it you can understand what the problem supposed to do For each function write a pseudo code in comment. Use comments in your code. Read from end of each line after saving and then ignore non characters Talk about second part of the hw2. Reverse line by line. -> change the indexes -> reversing the indexes
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 Tell the compiler to know the function. line -> starting position address of the array. You don’t need to define main()
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); Defining 2 character arrays each element 1 Byte. First input of getline -> address of the array %s means writing the string from the beginning of the array to terminating character \0 It doesn’t matter where to define functions.
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’) { ++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 Compiler checks the prototype and the actual function and they should be same Explain the loop test Read the characters until it sees a new line character Change the name of this function because stdio.h has a function with this name …… 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’)
“Octal” Dump Use “od –xc” to see hex dump of a file Show Character Show Hex Show Character Use “od –xc” to see hex dump of a file Use od –xc to see the ascii codes of the input So for the hw you can do octal dump for input and again do it for the output and see the result This flips the letters in the worlds! Because of different CPUs The hardware flips it again when it wants to use it Character ASCII value in Hex Byte offset