Copy file ~p5720/examples/c++broken.cc is C++ code which is quite broken. It is supposed to print out this:
here is a list of sin(i)/i versus i: sin(1)/i is 0.841471 sin(2)/i is 0.454649 sin(3)/i is 0.04704 sin(4)/i is -0.189201 sin(5)/i is -0.191785 sin(6)/i is -0.0465692 sin(7)/i is 0.0938552 sin(8)/i is 0.12367 sin(9)/i is 0.0457909 sin(10)/i is -0.0544021but there are several problems. The exercise is to fix them. Things to look out for: curly braces; ending statements with a semicolon; declaring all variables; data types (e.g., int versus double); and the use of "=" versus "==" in conditionals.
Also, when using one of the standard C-math functions (e.g., sin()) it may be in good taste (if not necesary) to
#include
Somethimes it is helpful to put the g++ compiler in maximal whine mode with the -Wall because it can tip you off as to mistakes or potential problems. For example, try
g++ -Wall c++broken.ccOnce you get c++broken.cc into working order, what is the name of the executable code that g++ generates?
Compile and run the codes linked below so that you feel comfortable with the following topics:
You do not need to modify them, but it may be valuable to do so in order to understand the code.
This is an exercise which introduces arrays and pointers. Write a C++ code with the statements:
#includeCompile and run this code, then add some calls to "cout" to find the valuesusing namespace std; // gets us to the standard C++ functions const int n = 10; // this is a constant! (not a variable) int main() { double a[n]; int i; for (i = 0; i < n; i++) a[i] = (i+1)*100.0; cout << " array elements: \n"; cout << " a[0]: " << a[0] << endl; cout << " a[9]: " << a[9] << endl; cout << " a[10]: << a[10] << endl; // ugh!! cout << " now, using pointers: \n"; double* b = a; cout << " the pointer itself (address of a[0]): " << b << endl; cout << " the value *b: " << *b << endl; return 0; }
*(b+1); b[9]; *++b;Also, try setting up b, as in
b = &a[??];so that b[1] has the same value as a[3]. What is the value of b[-1]? How about b[9] in this case?
Finally, try setting a[2] by using *b=-1000.0.