Homework Assignments

advertisement
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 <string.h> and string library functions,
e.g. strlen ( ) – Write your own!
1
Counting Lines
#include <stdio.h>
int main ( )
{
int c, m;
m = 0;
while ((c=getchar( )) != EOF)
if (c== ‘\n’) ++m;
printf(“Number of lines = %d\n”, m);
return 0;
}
2
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)
3
What’s new?
• Incrementing a variable
Shorthand
Shorthand
Equivalent to
++m;
m++;
m = m + 1;
• Decrementing a variable
Shorthand
Shorthand
Equivalent to
--m
m-m=m-1
4
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!
5
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!
6
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!
7
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!
8
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]”
9
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’
10
Arrays / Character Strings
/* count.c: count digit s 0-9 coming from stdin */
#include <stdio.h>
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;
11
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;
}
12
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)%
13
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”
14
Program: maxline
#include <stdio.h>
/* define maximum length of lines */
#define MAXLINE 1000
/* define our function prototypes */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
15
Program: maxline
/* print longest input line */
int main ( )
{
/* initialization */
int len,max;
char line[MAXLINE], longest[MAXLINE];
max = 0;
16
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);
}
17
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;
}
18
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;
}
19
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’)
20
Download