-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
251 changed files
with
19,397 additions
and
0 deletions.
There are no files selected for viewing
Submodule Homework
added at
df4e12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <iostream> // library for reading & writing from the console/keyboard | ||
#include <cmath> // library with the square root function & absolute value | ||
#include <cstdlib> // library with the exit function | ||
|
||
|
||
// Returns true if the candidate root is indeed a root of the polynomial a*x*x + b*x + c = 0 | ||
bool check_root(int a, int b, int c, float root) { | ||
// plug the value into the formula | ||
float check = a * root * root + b * root + c; | ||
// see if the absolute value is zero (within a small tolerance) | ||
if (fabs(check) > 0.0001) { | ||
std::cerr << "ERROR: " << root << " is not a root of this formula." << std::endl; | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
/* Use the quadratic formula to find the two real roots of polynomial. Returns | ||
true if the roots are real, returns false if the roots are imaginary. If the roots | ||
are real, they are returned through the reference parameters root_pos and root_neg. */ | ||
bool find_roots(int a, int b, int c, float &root_pos, float &root_neg) { | ||
// compute the quantity under the radical of the quadratic formula | ||
int radical = b*b - 4*a*c; | ||
// if the radical is negative, the roots are imaginary | ||
if (radical < 0) { | ||
std::cerr << "ERROR: Imaginary roots" << std::endl; | ||
return false; | ||
} | ||
float sqrt_radical = sqrt(radical); | ||
// compute the two roots | ||
root_pos = (-b + sqrt_radical); | ||
root_pos /= 2 * a; | ||
root_neg = (-b - sqrt_radical); | ||
root_neg /= 2 * a; | ||
return true; | ||
} | ||
|
||
int main() { | ||
// We will loop until we are given a polynomial with real roots | ||
while (true) { | ||
std::cout << "Enter 3 integer coefficients to a quadratic function: a*x*x + b*x + c = 0" << std::endl; | ||
int my_a, my_b, my_c; | ||
std::cin >> my_a >> my_b >> my_c; | ||
// create a place to store the roots | ||
float root_1, root_2; | ||
bool success = find_roots(my_a,my_b,my_c, root_1,root_2); | ||
// If the polynomial has imaginary roots, skip the rest of this loop and start over | ||
if (!success) continue; | ||
std::cout << "The roots are: " << root_1 << " and " << root_2 << std::endl; | ||
// Check our work... | ||
if (check_root(my_a,my_b,my_c, root_1) && check_root(my_a,my_b,my_c, root_2)) { | ||
// Verified roots, break out of the while loop | ||
break; | ||
} else { | ||
std::cerr << "ERROR: Unable to verify one or both roots." << std::endl; | ||
// if the program has an error, we choose to exit with a | ||
// non-zero error code | ||
exit(1); | ||
} | ||
} | ||
// by convention, main should return zero when the program finishes normally | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <iostream> // library for reading & writing from the console/keyboard | ||
#include <cmath> // library with the square root function & absolute value | ||
#include <cstdlib> // library with the exit function | ||
|
||
|
||
// Returns true if the candidate root is indeed a root of the polynomial a*x*x + b*x + c = 0 | ||
bool check_root(int a, int b, int c, float root) { | ||
// plug the value into the formula | ||
float check = a * root * root + b * root + c; | ||
// see if the absolute value is zero (within a small tolerance) | ||
if (fabs(check) > 0.0001) { | ||
std::cerr << "ERROR: " << root << " is not a root of this formula." << std::endl; | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
/* Use the quadratic formula to find the two real roots of polynomial. Returns | ||
true if the roots are real, returns false if the roots are imaginary. If the roots | ||
are real, they are returned through the reference parameters root_pos and root_neg. */ | ||
bool find_roots(int a, int b, int c, float &root_pos, float &root_neg) { | ||
// compute the quantity under the radical of the quadratic formula | ||
int radical = b*b - 4*a*c; | ||
// if the radical is negative, the roots are imaginary | ||
if (radical < 0) { | ||
std::cerr << "ERROR: Imaginary roots" << std::endl; | ||
return false; | ||
} | ||
float sqrt_radical = sqrt(radical); | ||
// compute the two roots | ||
root_pos = (-b + sqrt_radical) / 2*a; | ||
root_neg = (-b - sqrt_radical) / 2*a; | ||
return true; | ||
} | ||
|
||
int main() { | ||
// We will loop until we are given a polynomial with real roots | ||
while (true) { | ||
std::cout << "Enter 3 integer coefficients to a quadratic function: a*x*x + b*x + c = 0" << std::endl; | ||
int my_a, my_b, my_c; | ||
std::cin >> my_a >> my_b >> my_c; | ||
// create a place to store the roots | ||
float root_1, root_2; | ||
bool success = find_roots(my_a,my_b,my_c, root_1,root_2); | ||
// If the polynomial has imaginary roots, skip the rest of this loop and start over | ||
if (!success) continue; | ||
std::cout << "The roots are: " << root_1 << " and " << root_2 << std::endl; | ||
// Check our work... | ||
if (check_root(my_a,my_b,my_c, root_1) && check_root(my_a,my_b,my_c, root_2)) { | ||
// Verified roots, break out of the while loop | ||
break; | ||
} else { | ||
std::cerr << "ERROR: Unable to verify one or both roots." << std::endl; | ||
// if the program has an error, we choose to exit with a | ||
// non-zero error code | ||
exit(1); | ||
} | ||
} | ||
// by convention, main should return zero when the program finishes normally | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <iostream> // library for reading & writing from the console/keyboard | ||
#include <cmath> // library with the square root function & absolute value | ||
#include <cstdlib> | ||
#include <vector> | ||
|
||
using std::vector; | ||
|
||
// How many numbers to enter | ||
|
||
int main() | ||
{ | ||
int numToEnter = 10; | ||
std::cout << "Enter number of terms to check"<< std::endl; | ||
std::cin >> numToEnter; | ||
vector <float> sum; | ||
float average = 0; | ||
float input; | ||
|
||
// Get 10 numbers from the user | ||
for (int i = 0; i != numToEnter; i++) | ||
{ | ||
std::cout << "Enter a number: "; | ||
std::cin >> input; | ||
|
||
sum.push_back(input); | ||
} | ||
|
||
// Get average and print it | ||
for (int i = 0; i <= sum.size(); i++){ | ||
average += sum[i]; | ||
} | ||
average /= sum.size(); | ||
std::cout << "The average is: " << average << std::endl; | ||
for (int i = 0; i < sum.size(); i++){ | ||
if (sum[i] < average){ | ||
std::cout << sum[i] << std::endl; | ||
} | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
LAB 1: GETTING STARTED | ||
|
||
|
||
NAME: Yaseen M. Mahmoud | ||
|
||
|
||
|
||
COLLABORATORS AND OTHER RESOURCES: | ||
List the names of everyone you talked to in the first lecture & first | ||
lab (classmates, graduate TAs, undergraduate programming mentors, ALAC | ||
tutors, upperclassmen, students/instructor via LMS, etc.), and all of | ||
the resources (books, online reference material, etc.) you consulted | ||
in completing this assignment. | ||
|
||
|
||
Overflow, Stack | ||
|
||
|
||
|
||
NAMES OF YOUR TA & MENTORS | ||
|
||
Who is your graduate lab TA? | ||
|
||
Maurício | ||
|
||
Who are the undergraduate programming mentors assigned to your lab? | ||
|
||
Matt S., John Allwein, Alec, & Fred | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
|
||
class Node{ | ||
public: | ||
Node() : left{NULL}, right{NULL}, value{0} {} | ||
Node(int val) : left{NULL}, right{NULL}, value{val} {} | ||
Node* left; | ||
Node* right; | ||
int value; | ||
}; | ||
|
||
void printfu(std::ostream& out, Node* head, int depth) { | ||
if(!head){ | ||
return; | ||
} | ||
|
||
printfu(out,head->right,depth+1); | ||
out << std::string(depth,' '); | ||
out << " [" << head->value << "]" << std::endl; | ||
printfu(out,head->left,depth+1); | ||
} | ||
void print(std::ostream& out, Node* root) { | ||
printfu(out,root,0); | ||
} | ||
|
||
bool shape_match(Node* a, Node* b){ | ||
if (!a && !b){return true;} | ||
if (!a && b){return false;} | ||
if(a && !b){ return false;} | ||
return (shape_match(a->left, b->left), | ||
shape_match(a->right, b->right)); | ||
} | ||
|
||
Node* find_subtree_match(Node* a, Node* b){ | ||
if (!b){return a;} | ||
if (a == NULL){ return NULL;} | ||
if (shape_match(a,b)){ | ||
return a; | ||
} | ||
return(find_subtree_match(a->left,b), | ||
find_subtree_match(a->right,b)); | ||
} | ||
|
||
int main(){ | ||
Node* ah = new Node; | ||
ah->left = new Node; | ||
ah->right = new Node; | ||
ah->value = 5; | ||
ah->right->value = 13; | ||
ah->left->value = 7; | ||
ah->left->left = new Node; | ||
ah->left->right = new Node; | ||
ah->left->left->value = 6; | ||
ah->left->right->value = 11; | ||
ah->right->right = new Node; | ||
ah->right->right->value = 21; | ||
|
||
Node* h = new Node; | ||
h->left = new Node; | ||
h->right = new Node; | ||
h->value = 17; | ||
h->right->value = 22; | ||
h->left->value = 9; | ||
h->left->left = new Node; | ||
h->left->right = new Node; | ||
h->left->left->value = 10; | ||
h->left->right->value = 1; | ||
h->right->right = new Node; | ||
h->right->right->value = 8; | ||
|
||
Node *T = new Node(26); | ||
T->right = new Node(3); | ||
T->right->right = new Node(3); | ||
T->left = new Node(10); | ||
T->left->left = new Node(4); | ||
T->left->left->right = new Node(30); | ||
T->left->right = new Node(6); | ||
|
||
Node *S = new Node(10); | ||
S->right = new Node(6); | ||
S->left = new Node(4); | ||
S->left->right = new Node(30); | ||
|
||
|
||
cout<<"TREE 1:"<<endl; | ||
print(std::cout, ah); | ||
cout<<endl<<endl<<"TREE 2:"<<endl; | ||
print(std::cout, h); | ||
|
||
cout<<"SHAPES MATCH: "<<boolalpha<<shape_match(ah,h)<<", SHOULD BE TRUE"<<endl; | ||
cout<<"SUBTREE MATCH: "<<endl; | ||
Node* temp = find_subtree_match(ah, h); | ||
print(std::cout, temp); | ||
|
||
cout<<"TREE 1:"<<endl; | ||
print(std::cout,S); | ||
cout<<endl<<endl<<"TREE 2:"<<endl; | ||
print(std::cout, T); | ||
|
||
cout<<"SHAPES MATCH: "<<boolalpha<<shape_match(T,S)<<", SHOULD BE FALSE"<<endl; | ||
cout<<"SUBTREE MATCH: "<<endl; | ||
temp = find_subtree_match(T, S); | ||
print(std::cout, temp); | ||
|
||
|
||
} |
Oops, something went wrong.