9/25/18
Screenshots from HW 3 exercises:
I got more practice using emacs to write files and make scripts. I also learned about how script arguments work and the sed command.
10/8/18
C++ Comments:
#include <iostream> //Tells compiler to load and use iostream commands
using namespace std; //Tells compiler to convert std::cout to cout (and other commands)
int main() { //First line of code
cout <<"Hello World!" << endl; //Print hello world to screen followed by end line (endl)
return 0; //Exit the program
}
#include <iostream>
using namespace std;
int main() {
cout << "hello world" << endl; //Sends hello world and endline to standard output, then flushes buffer
int i=2; //Defines new 32-bit integer variable i to 2
cout << "i = " <<i<<endl; //Sends i to standard output
double a=3.3; //Defines new 64-bit floating point var a to 3.3
cout << "a = " <<a<<endl; //Sends a to standard output
int j = a*i; //Sets new int j to a times i, truncated to 6
cout << "a*i = "<<j<<endl; //Sends j to standard output
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n=10;
cout << "n is "<<n<<endl;
n--; //Decreases n by 1;
cout<<"n is now "<<n<<endl;
n++; //Increases n by 1;
cout<<n is now "<<n<<endl;
return 0;
}
What do the ++ and -– operations do? (and does this give you an idea why C++ has its name?)
++ and -- increment or decrement numbers without requiring an equals sign. (This means c++ is one better than C)
#include <iostream>
using namespace std;
int main() {
bool prop; //Defines new boolean variable prop that could be either true/1 or false/0
prop = (5>1); //Sets prop to whether 5>1 (true)
cout<<"prop is "<<prop<<endl; //Prints the value of prop (1 for true)
prop = (1>5); //Resets prop to 1>5 (false)
cout<<"prop is "<<prop<<endl; //Prints prop is 0 (for false)
prop = (1 != 5); //Sets prop to whether 1 does not equal 5 (true)
cout << "prop is " <<prop<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n=10;
while(n>0) { //Repeats the following code while int n is greater than 0
cout<<"n is "<<n<<endl; //Prints n
n--; //Decrements n
} //Code goes back to while loop conditional when run
return 0;
}
Try it yourself: rewrite the following code using for loops in place of the while loops.
#include <iostream>
using namespace std;
int main() {
for(int n=0; n<10; n++) {
// this is the slow (or outer) loop
cout << "n is " << n << ": ";
for(int m=0; m<=n; m++) {
// this is the fast (or inner) loop
// in this loop, the slow loop variable (n) is a constant
// this loop must run to completion before the slow loop
// can progress (during every iteration of the slow loop!)
cout << m;
}
// now the fast loop has finished and the slow loop can
// continue with the current iteration
cout << endl;
}
return 0 ;
}
10/16/18 HW 5
C++ Comments:
#include <iostream>
using namespace std;
int main() {
int n = 10; //Creates new int variable to use in while loop, could alternatively be created in for loop
while (n>=10) { //Runs interior code at least once as n starts equal to 10
if(n>5) { //Runs inner code if n is greater than 5, which is true 10>5
cout<<"n is "<<n<<endl; //Prints value of n (10)
}
else { //Runs this code if the above case n>5 is false
cout<<"n = "<<n<<endl; //Prints value of n like above
n--; //Decrements n in else statement (if n<5), possible typo to be outside of if-else
}
return 0; //Ends main method after first run of while loop (typo?)
}
}
#include <iostream>
using namespace std;
int main() {
int i = 10;
cout << "The memory address of i is " << &i << "\n"; //& operator gets memory address of variable i
cout << "The data stored at memory address " << &i << " is " << i << "\n"; //Without & operator i is just an integer
int* p = &i; //int* declares a new variable as an integer pointer, set to &i, the address of i
cout << "The value of p is " << p << "\n"; //The value of p is a memory address rather than a number
cout << "We say that p 'points at' the memory location referenced by address " << p << "\n";
cout << "The data stored at memory address " << p << " is " << *p << "\n"; //* operator used on a pointer variable retrieves the info stored at that memory address, in this case the int 10
return 0;
}
/*PROGRAM 1*/
#include <iostream>
using namespace std;
int main(){
int i = 10;
int j = i;
cout << "i= " << i << " and j= " << j << "\n"; //Prints 10 and 10
i=5; //Changes i independent of j
cout << "i= " << i << " and j= " << j << "\n"; //Prints 5 and 10
j=1; //Changes j independent of i
cout << "i= " << i << " and j= " << j << "\n"; //Prints 5 and 1
return 0;
}
/*PROGRAM 2*/
#include <iostream>
using namespace std;
int main(){
int i = 10;
int* p = &i; //Creates p as pointer to location of i variable
cout << "i= " << i << " and *p= " << *p << "\n"; //Prints 10 and 10
i=5; //Changes i and also therefore the value at *p
cout << "i= " << i << " and *p= " << *p << "\n"; //Prints 5 and 5
*p=1; //Changes value at address p, which is also the value of i
cout << "i= " << i << " and *p= " << *p << "\n"; //Prints 1 and 1
return 0;
}
#include <iostream>
using namespace std;
int main(){
int* p = new int(5); //'new int()' creates new int and returns its memory address
cout << "p points at address " << p << "\n"; //Prints memory address of new pointer variable
cout << "The data stored in address " << p << " is " << *p << "\n"; //Prints 5
*p = 10; //Changes data stored at address p to 10
cout << "Now the data stored in address " << p << " is " << *p << "\n"; //Prints 10
return 0;
}
To test your understanding, try writing a similar code. In your code, use the ‘new’ construct to create a new pointer, ‘p1’, and point it at a new double with value ‘3.14’. Then, declare another pointer, ‘p2’, and point it at the same double as p1. Print the address pointed to by each pointer, and the value when the * operator is used on each pointer. Finally, multiply the value of the original double by 2 (you’ll have to use the * operator for this) and print the values of *p1 and *p2 again.
#include <iostream>
using namespace std;
int main() {
double* p1 = new double(3.14);
double* p2 = p1;
cout << "p1 is " << p1 << " and p2 is " << p2 << "\n";
cout << "The value at p1 is " << *p1 << " and the value at p2 is " << *p2 << "\n";
*p1 = 2 * *p1;
cout << "The value at p1 is " << *p1 << " and the value at p2 is " << *p2 << "\n";
return 0;
}
10/26/18 HW 6
C++ Comments:
#include <iostream>
using namespace std;
int main() {
int ii[3] = {1,2,3}; //Creates new array with values in one line
int j=0;
while (j<3) {
cout <<" ii of "<<j<<" is "<<ii[j]<<endl; //Loop prints each value in the array ii
j++;
}
int LL[2][3] = {1,2,3,4,5,6}; //Creates new 2-dimensional array with 2 rows and 3 columns
j=0;
int k;
while (j<2) { //Outer loop goes through both rows
k=0;
while (k<3) { //Inner loop goes through each column entry in each row
cout<<" LL of "<<j<<" "<<k<<" is "<<LL[j][k]<<endl; //Loop prints all the values in the array
k++;
}
j++;
}
return 0;
}
#include <iostream>
#include <fstream> //Includes new class for writing to files
using namespace std;
int main() {
ofstream myfile; //Creates new ofstream file object
myfile.open("example.txt"); //Gives myfile a text file to work with
myfile<<"write some junk."; //Writes into the given file
myfile.close(); //Closes the file, flushing any buffer
return 0;
}
/* DOTPROD.CPP */
#include <iostream>
using namespace std;
double dot_prod(double v1[3],double v2[3]) { //Creates new function that takes 2 arrays of 3 doubles
double dotdot;
dotdot = v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]; //Dot-product math: multiplies the matching elements of each array and adds the results
cout<<" The dot product is "<<dotdot<<endl; //Prints the result
return 0;
}
Now try modifying main.cpp so that the third line of vectors.txt is read in as a double called 'scalar'. Print the value of this scalar after the lines printing the two vectors. Now, write and include a new program called 'scalarmult.cpp' which contains a function called scalar_mult. The scalar_mult function should take one vector and one scalar as inputs and print the vector multiplied by the scalar (recall that we just multiply each vector component by the scalar to do this). Finally, call the scalar_mult function on both vector1 and scalar, and vector2 and scalar after the call to dot_prod.
/* MAIN.CPP */
#include <iostream>
#include <fstream>
// include the program dotprod.cpp so that we can find the dot_prod function
#include "dotprod.cpp"
#include "scalarmult.cpp"
using namespace std;
int main () {
// declare the vectors
double vector1[3];
double vector2[3];
double scalar;
// open the input file
ifstream infile;
infile.open("vectors.txt");
// store the input in the vectors and print the vectors for the user
infile>>vector1[0]>>vector1[1]>>vector1[2];
cout<<" Vector 1 is ("<<vector1[0]<<","<<vector1[1]<<","<<vector1[2]<<")"<<endl;
infile>>vector2[0]>>vector2[1]>>vector2[2];
cout<<" Vector 2 is ("<<vector2[0]<<","<<vector2[1]<<","<<vector2[2]<<")"<<endl;
infile>>scalar;
cout<<"Scalar is "<<scalar<<endl;
// close the input file
infile.close();
// call the dot_prod function from dotprod.cpp
dot_prod(vector1,vector2);
scalar_mult(vector1, scalar);
scalar_mult(vector2, scalar);
return 0;
}
/* scalarmult.CPP */
#include <iostream>
using namespace std;
double[3] scalar_mult(double v[3],double s) {
double out;
out = v[0]*s+v[1]*s+v[2]*s;
cout<<" The scalar multiple is "<<out<<endl;
return 0;
}]
HW 9
pandas=10 # Creates new variable "pandas" equal to value 10
zebras=20
total_animals_in_zoo=pandas+zebras # Creates new variable equal to sum of above two vars
print(total_animals_in_zoo) # Prints value of total animals to standard output
pi=3.14159 #Approx.
diameter=10
######YOUR CODE HERE####
area = pi * (diameter / 2) ** 2 # Sets area equal to pi times radius (d/2) squared
print(area)
########################
#####YOUR CODE HERE###########
print(max([1, 5, 10]) - min([3, 7]) == sum([1, 2, 3])) # 10 - 3 =? 6
Exercise. Google the functions used by the matplotlib and numpy libraries to get an understanding of the flow of the code. In the blank code space below, import the matplotlib and numpy libraries and make any plot of your own. Use the above examples and the documentation pages to help you.
import matplotlib as m
import matplotlib.pyplot as plt
import numpy as n
t = np.arange(0, 100, 1) # set t to the array of whole numbers 0..100
s = [np.random.randint(0, 1 + np.random.randint(1, 5) * i) for i in t] # Get a random number between 0 and 1 plus t times a random number from 1 to 5
fig, ax = plt.subplots()
ax.plot(t, s)
ax.grid()
plt.show() # Show the generated random plot
HW 10
Original Photo:
Deep Dream Result: