Day 8
- Reading: Read Chapter 8 and 9
- Lab 4 is due at beginning of lab today
- Take Lab 4 Survey
- Lab 5 is due at beginning of lab next Tuesday
- Lab Quiz 2nd Chance Thursday during lab
- Project 3 is assigned
- Lecture Topics
int num, sum = 0, result1;
FILE *inp1;
FILE *outp1;
inp1 = fopen("input.dat", "r");
outp1 = fopen("output.dat", "w");
result1 = fscanf(inp1, "%d", &num);
while (result1 != EOF) {
sum = sum + num;
result1 = fscanf(inp1, "%d", &num);
}
fprintf(outp1, "%d\n", sum);
fclose(inp1);
fclose(outp1);
File permissions with chmod
Makefiles with multiple targets
prog1: prog1.c
gcc -Wall -Werror -ansi -pedantic -lm prog1.c
prog2: prog2.c
gcc -Wall -Werror -ansi -pedantic -lm prog2.c
Arrays
- Example: Write a program that reads ten numbers, then prints them in reverse order. (see solution here)
- Example: Write a program that reads integers to the end of file, then prints them in reverse order.
- Example: Write a function that reads up to twenty salaries into an array.
- Example: Write a function that computes the sum of up to twenty salaries in a given array.
- Example: Write a function that returns the smallest of an array of ints.
Strings
- null character '\0' terminates string
- char array needs extra character to hold null character
- Example: Write a program that reads a first and last name into two strings.
- Example: Write a program that reads up to twenty names into an array of strings.