aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ee1301/wk6/lab5/momentum.cpp1
-rw-r--r--ee1301/wk6/lab5/vectorArray.cpp40
-rw-r--r--ee1301/wk6/lab5/warmup.txt4
3 files changed, 41 insertions, 4 deletions
diff --git a/ee1301/wk6/lab5/momentum.cpp b/ee1301/wk6/lab5/momentum.cpp
index 0a19e04..a49be54 100644
--- a/ee1301/wk6/lab5/momentum.cpp
+++ b/ee1301/wk6/lab5/momentum.cpp
@@ -9,6 +9,7 @@ int main() {
std::cin >> velocity[0] >> velocity[1] >> velocity [2];
std::cout << "Please enter mass [kg]: ";
std::cin >> mass;
+
double* vector;
vector = new double[3];
std::cout << "Momentum: <";
diff --git a/ee1301/wk6/lab5/vectorArray.cpp b/ee1301/wk6/lab5/vectorArray.cpp
index 069ef6a..279988c 100644
--- a/ee1301/wk6/lab5/vectorArray.cpp
+++ b/ee1301/wk6/lab5/vectorArray.cpp
@@ -1,7 +1,43 @@
+#include <iostream>
-
+double randVec();
+double momentum (double velocity);
+int main() {
+ srand(time(NULL));
+ double sum[3]={0,0,0};
+ double* randVel; double* momArray;
+ randVel = new double[1000];
+ momArray = new double[1000];
+ for (int i=0; i<1000; i++) {
+ randVel[i]=randVec();
+ momArray[i]=momentum(randVel[i]);
+ int j=i%3;
+ sum[j]+=momArray[i];
+ }
+ std::cout << '<' << sum[0]/1000.0 << ',' << sum[1]/1000.0 << ',' << sum[2]/1000.0 << '>' << std::endl;
+ delete[] randVel;
+ delete[] momArray;
+}
double randVec() {
-
+ double vector[3];
+ for (int i=0; i<3; i++) {
+ if (rand()%2+1==1) {
+ vector[i]=rand()%100+1.0;
+ } else {
+ vector[i]=rand()%100*-1.0+1.0;
+ }
+ }
+ return *vector;
+}
+
+double momentum(double velocity) {
+ double mass;
+ if (rand()%2+1==1) {
+ mass=rand()%10+1;
+ } else {
+ mass=rand()%10*-1+1;
+ }
+ return velocity*mass;
} \ No newline at end of file
diff --git a/ee1301/wk6/lab5/warmup.txt b/ee1301/wk6/lab5/warmup.txt
index 6dd4bc2..50a1857 100644
--- a/ee1301/wk6/lab5/warmup.txt
+++ b/ee1301/wk6/lab5/warmup.txt
@@ -11,5 +11,5 @@
d. Line 6 is a function of return type Point with no input.
e. Point p2;
3)
- a.
- b.
+ a. 10 2 2 2
+ b. The array is not deleted (memory leak)