$ cd ~/cs35/labs/ $ git clone <link you got from github> ./lab01
$ cd ~/cs35/labs/lab01 $ ln -s /usr/local/doc/lab01-images/ ./test_dataNote the path /usr/local/doc/lab01-images/ is local only to the machines on the CS network and will not work if you clone your code to your personal computer.
To call a function in another program, you need to include the header file, but not the .cpp file. Note that ppmio.cpp contains the line
#include "ppmio.h"as one of its includes. We use quotes "" when we are including local header files we wrote, and angle brackets <> when including common system-wide headers.
Our main program in picfilter.cpp includes both of our local header files
#include "image.h" #include "ppmio.h"
Using header files can break larger programs into smaller, more modular pieces and can separate easy to understand function declarations in header files from sometimes messy implementations in .cpp files.
$ clang++ -std=c++11 -o picfilter picfilter.cpp image.cpp ppmio.cppThe name of the output file is specified with the -o flag, in this case -o picfilter. This option can go either near the front, or at the end of the clang++ command.
What super fancy feature are we using that requires the latest and greatest of C++? Don't get too excited, it's just the fstream::open(string filename) method buried in ppmio.cpp. And since it's in a file you don't need to edit/read/understand, you don't need to worry too much about this feature. But if you forget the -std=c++11 you'll get the following weird error:
ppmio.cpp:25:13: error: no viable conversion from 'string' (aka 'basic_string<char>') to 'const char *' file.open(filename);
cd ~/cs35/examples git pull
To declare a static array, in which you know the exact size of the array at compile time, use the following syntax.
int a[10];In the example above, the variable a is an array of ten integers. Individual elements can be accessed for reading/writing by their index, using the example syntax below.
a[0] = 7; //write cout << a[0] << endl; //read value in first index
C++ does not check if you try to access an array out of bounds and has no way of determining the length of the array given only its name. If you want a data structure with these features, keep taking this course.
See simpleArray.cpp for a full program using an array.
First, declare a variable that is a pointer to an int. This syntax is int* dynArr. The type of the variable dynArr is int*, which can either be the address of a single integer, or the address of the first integer in an array of integers. We will be using our pointer to allocate an array dynamically once we know the size using the new keyword.
int* dynArr; /* a pointer to (eventually) a dynamically allocated array */ int size = 10; dynArr = new int[size];At this point, we can use dynArr just like a statically allocated array. However, since the compiler knows how big a static array is at compile time, it can reserve enough spots for the array on the function call stack. When a static array goes out of scope, C++ can automatically free this reserved space for later reuse. Since dynamic arrays are created at runtime, the compiler can only reserve space for the pointer/address, not the entire array, on the stack. The new command allocates space dynamically in a separate region of the computer's memory called the heap. We will talk more about pointers, and the heap later in the semester, but for this week, what you need to know is that calls to new require a matching call to delete to free dynamically allocated memory. This is done when the memory for the array is no longer needed using the sample syntax:
delete [] dynArr; // free the memory of dynamically allocated arrayIn both incArray.cpp in your examples folder and picfilter.cpp for lab 01, the calls to new and delete have already been implemented in the proper place. We do not expect you to fully understand pointers or dynamic memory in this lab, but simply wanted to highlight a new C++ feature you will encounter in this lab.
The file incArray.cpp also shows how to pass arrays in functions, a feature you will use in lab 01. Note the syntax in the function declaration, int* a. Here we are saying a is a pointer to (the first element of) an array of 1 or more integers. We want the program to be flexible enough to work with any size array, but since arrays do not know their own size, we additionally pass a second parameter with the integer size of the array.
Try to implement the function void increment(int* a, int size) in incArray.cpp. Note the return type of this function is void. If we modify the contents of a locally inside increment, will the changes be visible in main or printArray? Let's find out.