Update 10 Jan: Sample solutions for in-class exercises are available.
Call by reference
Call by reference allows us to write functions that modify their parameters without using pointers explicitly:
void shuffle(int a, int *b, int &c) { int temp = a; a = *b; b = &temp; c = a + *b + temp; } int main() { int a = 1; int b = 2; int c = 3; shuffle(a, &b, c); cout << a << ',' << b << ',' << c << endl; } File I/O
You should have covered C-style file I/O at the end of 142. Here's a refresher, plus the new and better C++ way:
/* C way */ #include int main() { FILE *infile, *outfile; char c; /* Open filehandles */ infile = fopen("in.txt", "r"); outfile = fopen("out.txt", "w"); /* Test if open successful */ if (infile != NULL && outfile != NULL) { /* read until EOF */ while ( fscanf(infile, "%c", &c) != EOF ) { /* writing the character each time */ fprintf(outfile, "%c", c); } } /* Close file handles */ fclose(infile); fclose(outfile); } // C++ way: does NOT do the same thing as the above #include #include int main() { // Open file streams. Notice "constructor" syntax ifstream in("in.txt"); ofstream out("out.txt"); char word[200]; if (!in) { cout << "Unable to open input file." << endl; return 1; } if (!out) { cout << "Unable to open output file." << endl; return 1; } // Can you guess what the following does? while (in >> word) out << word << '\n'; } Review
For the first assignment, we'll be picking up right where 142 left off. You must know C pretty well by now. Here's some refresher material:
Strings
Recall the following C string functions:
- strlen(char *str): returns the length, not including null character
- strcpy(char *target, char *source): copies source to target
- strcmp(char *first, char *second): compares first with second
Remember that C strings are zero-terminated; that is, the end of a C string is marked by the special character '\0'. Can you write a plausible implementation of strlen()? strcpy()?
int strlen(char *str) { } void strcpy(char *target, char *source) { } Can you rewrite the following C++ program to pick the word in the file in.txt that comes first in alphabetical order, and print it to standard output?
#include #include int main() { ifstream in("in.txt"); char word[200]; while (in >> word) { } return 0; } Structs (old and new)
A struct allows you to define a type with named pieces, sometimes called members or fields.
typedef struct { /* C way */ int id; char name[200] } Student; struct Student { // C++ way int id; char name[200] }; Structs can be nested. Try rewriting the above struct so that the name, instead of being a single char array, is a struct with first and last name members. Then, initialize it to your personal data.
struct Student{ int id; }; /* Declaration; initialize this: */ Student s = How would you print out the name and ID with a comma between them?
cout << Optional homework: Write a program that:
All the pieces, except arrays of structs, are in the notes above. It should take you about an hour at most. I will post a solution on my web page in a couple of days. (Update: solutions are available.)
- Reads in a set of 10 student names (first and last) from a file
- Assigns each student a unique integer ID
- Prints out student whose last name comes first in alphabetical order