Logic statement code and comments:
#include <iostream>
using namespace std;
int main() {
int n = 10; //Initializes integer variable "n" with value=10
while (n>=10) { //Starts and continues a loop as long as variable "n" has a value greater or equal to "10."
if(n>5) { //Continues to the line below if variable "n" has a value greater than 5
cout<<"n is "<<n<<endl; //Prints "n is " followed by the value of the variable "n" and then ends the line
} //Ends the lines of code only accessed by the "if" statement above
else { //Continues to the lines below if variable "n" does not match any above "if" statements, which in this case means if "n" has a value of 5 or lower.
cout<<"n = "<<n<<endl; //Prints "n = " followed by the value of variable "n" and then ends the line
n--; //Subtracts 1 from the value of "n"
} //Ends the lines of code only accessed by the "else" statement above
return 0; //Ends the program (and breaks the loop)
} //Ends (does not break) the "while" loop
} //Ends the program
Compiling and Output:
[cms-opendata@localhost Programs]$ g++ -Wall logic.cpp -o logic
[cms-opendata@localhost Programs]$ ./logic
n is 10
There seem to be a few unexpectedly placed lines of code so the loop only runs once and then the whole code ends due to what seems like a misplaced "return 0;"
Pointers code with comments:
#include <iostream>
using namespace std;
int main() {
int i = 10; //Declares integer variable "i" with value=10
cout << "The memory address of i is " << &i << "\n"; //Prints the written phrase followed by the memory address where variable "i" is stored on the machine and then ends the line
cout << "The data stored at memory address " << &i << " is " << i << "\n"; //Prints the written phrase followed by the memory address "i" is stored on followed by "is " followed by the value of variable "i" and then ends the line
int* p = &i; //Declares variable "p" as a pointer with the value of the memory address of variable "i"
cout << "The value of p is " << p << "\n"; //Prints the written phrase followed by value of pointer "p" and ends the line
cout << "We say that p 'points at' the memory location referenced by address " << p << "\n"; //Prints the written phrase followed by the value of the pointer pointer "p"
cout << "The data stored at memory address " << p << " is " << *p << "\n"; //Prints the written phrase followed by the value of pointer "p" followed by the value of the variable stored on the memory address stored in pointer "p," which is the variable "i" with value=10
return 0; //Ends the code
}
Code Compiling and Output:
[cms-opendata@localhost Programs]$ g++ -Wall pointers.cpp -o pointers
[cms-opendata@localhost Programs]$ ./pointers
The memory address of i is 0x7fffb6220d74
The data stored at memory address 0x7fffb6220d74 is 10
The value of p is 0x7fffb6220d74
We say that p 'points at' the memory location referenced by address 0x7fffb6220d74
The data stored at memory address 0x7fffb6220d74 is 10
If we wished to declare pointers that pointed to memory addresses storing variable other than integers, we would simply declare the pointer using the desired variable type. If one wanted to declare a pointer that pointed to a memory address storing a double, one would declare the pointer using "double*" instead of "int*"
First practice code with comments:
#include <iostream>
using namespace std;
int main(){
int i = 10; //Declares integer variable "i" with value=10
int j = i; //Declares integer variable "j" with value = the value of variable "i"
cout << "i= " << i << " and j= " << j << "\n"; //Prints "i= " followed by the value of variable "i" followed by " and j= " followed by the value of variable "j"
i=5; //Changes the value of declared variable "i" to 5
cout << "i= " << i << " and j= " << j << "\n"; //Prints "i= " followed by the value of variable "i" followed by " and j= " followed by the value of variable "j"
j=1; //Changes the value of declared variable "j" to 1
cout << "i= " << i << " and j= " << j << "\n"; //Prints "i= " followed by the value of variable "i" followed by " and j= " followed by the value of variable "j"
return 0; //Ends the code
}
Compiling and Running:
[cms-opendata@localhost Programs]$ g++ -Wall pointers2.cpp -o pointers2
[cms-opendata@localhost Programs]$ ./pointers2
i= 10 and j= 10
i= 5 and j= 10
i= 5 and j= 1
Second Practice code with comments:
#include <iostream>
using namespace std;
int main(){
int i = 10; //Declares integer variable "i" with value=10
int* p = &i; //Declares pointer variable "p" with value = the memory address where variable "i" is stored
cout << "i= " << i << " and *p= " << *p << "\n"; //Prints "i= " followed by the value of variable "i" followed by "and *p= " followed by the value of the variable stored at the memory address "p" points to which is variable "i" with value=10
i=5; //Changes the value of declared variable "i" to 5
cout << "i= " << i << " and *p= " << *p << "\n"; //Prints "i= " followed by the value of variable "i" followed by "and *p= " followed by the value of the variable stored at the memory address "p" points to which is variable "i" with value=5
*p=1; //Changes the value of the variable stored at the memory address pointer "p" points to, which is variable "i," to value=1
cout << "i= " << i << " and *p= " << *p << "\n"; //Prints "i= " followed by the value of variable "i" (which has been changed to 1) followed by "and *p= " followed by the value of the variable stored at the memory address "p" points to which is variable "i" with value=1
return 0; //Ends code
}
Compiling and running:
[cms-opendata@localhost Programs]$ g++ -Wall pointers3.cpp -o pointers3
[cms-opendata@localhost Programs]$ ./pointers3i= 10 and *p= 10
i= 5 and *p= 5
i= 1 and *p= 1
The behavior of the second program is different from the first because in the second program, an alternative method of manipulating the variable "i" is used before the last printed statement by changing the value of variable "i" by changing the value of the variable stored at the memory address pointer "p" points to, which is exactly "i." Each printed statement using "p" in the second program provides the value of the variable at the memory address "p" points to by using the "*p" tag.
Example pointer code with comments:
#include <iostream>
using namespace std;
int main(){
int* p = new int(5); //Declares a pointer that points to a memory address storing an integer and set the value stored at that memory address = 5
cout << "p points at address " << p << "\n"; // Prints the written phrase followed by the memory address represented by the pointer "p"
cout << "The data stored in address " << p << " is " << *p << "\n"; //Prints the written phrase followed by memory address represented by "p" followed by " is " followed by the value stored at the memory address "p" represents
*p = 10; //Changes the value stored at the memory address "p" points to from 5 to 10
cout << "Now the data stored in address " << p << " is " << *p << "\n"; //Prints the written phrase followed by the memory address represented by "p" followed by " is " followed by the value stored at the memory address "p" represents
return 0; //Ends code
}
Compiling and Running:
[cms-opendata@localhost Programs]$ g++ -Wall pointers4.cpp -o pointers4
[cms-opendata@localhost Programs]$ ./pointers4
p points at address 0xc95010
The data stored in address 0xc95010 is 5
Now the data stored in address 0xc95010 is 10
Practice pointers code with comments:
#include <iostream>
using namespace std;
int main(){
double* p1 = new double(3.14); //Declares a pointer that points to a memory address storing a double and sets the value stored at that memory address = 3.14
double* p2 = new double(*p1); //Declares a pointer that points to a memory address storing a double and sets the value stored at that memory address equal to the value stored at the memory address "p1" points to
cout << "p1 points at address " << p1 << " and p2 points at address" << p2<<"\n"; // Prints the written phrase followed by the memory address represented by the pointer "p1" followed by the written phrase followed by the memory address represented by the pointer "p2" and ends the line
cout << "The data stored in p1 is " << *p1 << " and in p2 is " << *p2 << "\n"; //Prints the written phrase followed by the value stored at the memory address pointer "p1" points to followed by " and in p2 is " followed by the value stored at the memory address "p2" points to
cout << "*p1 * *p2 = " << *p1**p2 << "\n"; //Prints "*p1 * *p2 = " followed by the product of the values stored at the memory addresses pointer "p1" and "p2" point to which is 3.14*3.14
cout << "The data stored in p1 is now " << *p1 << " and in p2 is " << *p2 << "\n"; //Prints the written phrase followed by the value stored at the memory address pointer "p1" points to followed by " and in p2 is " followed by the value stored at the memory address pointer "p2" points to and then ends the line
return 0; //Ends the code
}
Compiling and Running:
[cms-opendata@localhost Programs]$ g++ -Wall pointers4.cpp -o pointers4
[cms-opendata@localhost Programs]$ ./pointers4
p1 points at address 0xb25010 and p2 points at address0xb25030
The data stored in p1 is 3.14 and in p2 is 3.14
*p1 * *p2 = 9.8596
The data stored in p1 is now 3.14 and in p2 is 3.14
HW 5 Code prompts:
Logic statement practice code with comments:
#include <iostream>
using namespace std;
int main() {
int t = 6; //Declares the integer variable "t" with value=6
while (t>0) { //Begins and continues a loop if value of "t" is greater than 0
t--; //Subtracts 1 from the value of "t"
if (t>0) { //Continues to the next line of code if the value of "t" is greater than 0
cout<<"The value of t is "<<t<<endl; //Prints the written phrase followed by the value of "t" and ends the line
} else { //Ends the previous "if" statement and begins an "else" statement that continues to the next line of code if the previous "if" statements were not met, which means t=<0 in this case
cout<<"The value of t is 0 and the loop breaks"<<endl; //Prints the written statement and ends the line
} //Ends the "else" statement's lines of code
} //Ends, but does not break, the "while" loop
return 0; //Ends the code
}
Compiling and Running:
[cms-opendata@localhost Programs]$ g++ -Wall logic2.cpp -o logic2
[cms-opendata@localhost Programs]$ ./logic2
The value of t is 5
The value of t is 4
The value of t is 3
The value of t is 2
The value of t is 1
The value of t is 0 and the loop breaks