diff options
author | RossTheRoss <msattr@gmail.com> | 2019-06-25 17:55:13 -0500 |
---|---|---|
committer | RossTheRoss <msattr@gmail.com> | 2019-06-25 17:55:13 -0500 |
commit | 6c83506cb44b1d20f66df404f0b6468b0e291b6b (patch) | |
tree | 8e0475edc2891741862bc9b63844600bdebe6936 /ee1301/wk6 | |
parent | M E A N I N G F U L C O M M E N T S (diff) | |
download | homework-6c83506cb44b1d20f66df404f0b6468b0e291b6b.tar homework-6c83506cb44b1d20f66df404f0b6468b0e291b6b.tar.gz homework-6c83506cb44b1d20f66df404f0b6468b0e291b6b.tar.bz2 homework-6c83506cb44b1d20f66df404f0b6468b0e291b6b.tar.lz homework-6c83506cb44b1d20f66df404f0b6468b0e291b6b.tar.xz homework-6c83506cb44b1d20f66df404f0b6468b0e291b6b.tar.zst homework-6c83506cb44b1d20f66df404f0b6468b0e291b6b.zip |
Do a thing:
Diffstat (limited to 'ee1301/wk6')
-rw-r--r-- | ee1301/wk6/hw6_directory/strap012_HW6A.cpp | 430 | ||||
-rw-r--r-- | ee1301/wk6/hw6_directory/strap012_HW6B.cpp | 68 | ||||
-rw-r--r-- | ee1301/wk6/lab5/Bug.cpp | 24 | ||||
-rw-r--r-- | ee1301/wk6/lab5/Bug.hpp | 56 | ||||
-rw-r--r-- | ee1301/wk6/lab5/momentum.cpp | 54 | ||||
-rw-r--r-- | ee1301/wk6/lab5/strap012_lab5_w_1.cpp | 148 | ||||
-rw-r--r-- | ee1301/wk6/lab5/vectorArray.cpp | 84 | ||||
-rw-r--r-- | ee1301/wk6/lab5/wUp.cpp | 52 | ||||
-rw-r--r-- | ee1301/wk6/lab5/warmup.txt | 30 |
9 files changed, 473 insertions, 473 deletions
diff --git a/ee1301/wk6/hw6_directory/strap012_HW6A.cpp b/ee1301/wk6/hw6_directory/strap012_HW6A.cpp index d802c15..ac2b7d0 100644 --- a/ee1301/wk6/hw6_directory/strap012_HW6A.cpp +++ b/ee1301/wk6/hw6_directory/strap012_HW6A.cpp @@ -1,215 +1,215 @@ -//Matthew Strapp -//EE1301 -//17 April 2019 -//HW 6A: Dice Class -#include <iostream> -#include <cstdlib> -using namespace std; - -const int maxNumDie=50; - -int* userInputParser(string s); - -class Dice { -private: - int min; - int max; -public: - int roll() { - return rand() % (max-min+1) + min; - }; - Dice() { //Default constructor for debugging purposes - min=1; - max=1; - } - Dice(int gotMin, int gotMax){ - min = gotMin; - max = gotMax; - }; -}; - -int main() { - int rounds; - Dice die; - int roll[maxNumDie]; - int max=0, min=999999, sum=0, sample=0; - double avg; - srand(time(NULL)); // DO NOT WRITE THIS LINE AGAIN OR ANYWHERE ELSE - cout << "What do you want to roll? "; - string s; - getline(cin, s); - cout << "How many rounds do you want to roll? "; - cin >> rounds; - int* pairs = userInputParser(s); - - // This will display the array of die information retrieved from user. - // Replace the following code when you submit your solution. - - // pairs is an array with the following format: - // {num_dice, - // first_die_start, - // first_die_end, - // second_die_start, - // ... } - double numRolls=0; - for (int j=0; j<rounds; j++) { - int curRoll=0; - numRolls+=1; - for(int i=1; i < pairs[0]; i+=2) { - die = Dice(pairs[i], pairs[i + 1]); - roll[i-1] = die.roll(); - curRoll += roll[i-1]; - } - - if (curRoll > max) { - max = curRoll; - } - if (curRoll < min) { - min = curRoll; - } - sum+=curRoll; - sample=curRoll; - } - avg = sum / numRolls; - cout << "Sample roll: " << sample << endl - << "Minimum roll: " << min << endl - << "Maximum roll: " << max << endl - << "Average roll: " << avg << endl; - -} - - -int* userInputParser(string s) { - static int dice[2*maxNumDie+1] = {0}; // array format: - // {num_dice, - // first_die_start, - // first_die_end, - // second_die_start, - // ... } - // max of maxNumDie dice supported - - string data[4*maxNumDie]; // Intermediate storage for parsing input string - - // count how many '+'s or 'd's the roll[i-1] = die[i-1].roll();re are... - int parts = 0; - for(unsigned int i=0; i < s.length(); i++) - { - if(s[i] == 'd' || s[i] == '+') - { - parts++; - } - } - // ... so we know the number of times to decode values - - int index=0; - unsigned d = s.find('d'); - unsigned p = s.find('+'); - while(d != static_cast<unsigned>(-1) || p != static_cast<unsigned>(-1)) - { - bool dFirst = d < p; - if(dFirst) - { - string before = s.substr(0,d); // part before the 'd' (should be just one number) - // figure out what number is after 'd' - int count = 0; - bool foundDigit=false; - for(int i=0; i< static_cast<signed>(s.length()-d-1); i++) - { - if(isdigit(s[count+d+1])) - { - foundDigit=true; - } - if(!isdigit(s[count+d+1]) && foundDigit) - { - break; - } - count++; - } - string after = s.substr(d+1,count); //should be just the number after 'd' - - // store these two parts - data[index] = before; - data[index+1] = after; - index+=2; - - - // remove this part from the string s - s = s.substr(d+count+1); // discard these two parts - } - else // same idea for the '+' - { - // figure out what number is after '+' - int count = 0; - bool foundDigit=false; - for(int i=0; i< static_cast<signed>(s.length()-p-1); i++) - { - if(isdigit(s[count+p+1])) - { - foundDigit=true; - } - if(!isdigit(s[count+p+1]) && foundDigit) - { - break; - } - count++; - } - string after = s.substr(p+1,count); //should be just the number after '+' - - // store this part - data[index] = "+"; - data[index+1] = after; - index+=2; - - - // remove this part from the string s - s = s.substr(p+count+1); // discard these two parts - } - - // update d and p for next loop interation - d = s.find('d'); - p = s.find('+'); - - } - - // now we need to figure out how many dice there are (as 2d4 is 2 dice) - // we will treat "+2" as a die that rolls [2,2] - int diceCount = 0; - for(int i=0; i < parts*2; i+=2) - { - if(data[i][0] == '+') - { - diceCount++; - } - else - { - diceCount+=atoi(data[i].c_str()); - } - } - - dice[0] = diceCount*2+1; // put size in first index - - int ind=1; // index for the "dice" array (as not same as data array) - for(int i=0; i < parts*2; i+=2) - { - // if we have a +, add a "Dice" that has a range of 0 - if(data[i][0] == '+') - { - dice[ind] = atoi(data[i+1].c_str()); - dice[ind+1] = atoi(data[i+1].c_str()); - - ind+=2; - } - else // otherwise add however many of the dice requested - { - for(int j=0; j < atoi(data[i].c_str()); j++) - { - dice[ind] = 1; - dice[ind+1] = atoi(data[i+1].c_str()); - - ind += 2; - } - } - } - - return dice; -} +//Matthew Strapp
+//EE1301
+//17 April 2019
+//HW 6A: Dice Class
+#include <iostream>
+#include <cstdlib>
+using namespace std;
+
+const int maxNumDie=50;
+
+int* userInputParser(string s);
+
+class Dice {
+private:
+ int min;
+ int max;
+public:
+ int roll() {
+ return rand() % (max-min+1) + min;
+ };
+ Dice() { //Default constructor for debugging purposes
+ min=1;
+ max=1;
+ }
+ Dice(int gotMin, int gotMax){
+ min = gotMin;
+ max = gotMax;
+ };
+};
+
+int main() {
+ int rounds;
+ Dice die;
+ int roll[maxNumDie];
+ int max=0, min=999999, sum=0, sample=0;
+ double avg;
+ srand(time(NULL)); // DO NOT WRITE THIS LINE AGAIN OR ANYWHERE ELSE
+ cout << "What do you want to roll? ";
+ string s;
+ getline(cin, s);
+ cout << "How many rounds do you want to roll? ";
+ cin >> rounds;
+ int* pairs = userInputParser(s);
+
+ // This will display the array of die information retrieved from user.
+ // Replace the following code when you submit your solution.
+
+ // pairs is an array with the following format:
+ // {num_dice,
+ // first_die_start,
+ // first_die_end,
+ // second_die_start,
+ // ... }
+ double numRolls=0;
+ for (int j=0; j<rounds; j++) {
+ int curRoll=0;
+ numRolls+=1;
+ for(int i=1; i < pairs[0]; i+=2) {
+ die = Dice(pairs[i], pairs[i + 1]);
+ roll[i-1] = die.roll();
+ curRoll += roll[i-1];
+ }
+
+ if (curRoll > max) {
+ max = curRoll;
+ }
+ if (curRoll < min) {
+ min = curRoll;
+ }
+ sum+=curRoll;
+ sample=curRoll;
+ }
+ avg = sum / numRolls;
+ cout << "Sample roll: " << sample << endl
+ << "Minimum roll: " << min << endl
+ << "Maximum roll: " << max << endl
+ << "Average roll: " << avg << endl;
+
+}
+
+
+int* userInputParser(string s) {
+ static int dice[2*maxNumDie+1] = {0}; // array format:
+ // {num_dice,
+ // first_die_start,
+ // first_die_end,
+ // second_die_start,
+ // ... }
+ // max of maxNumDie dice supported
+
+ string data[4*maxNumDie]; // Intermediate storage for parsing input string
+
+ // count how many '+'s or 'd's the roll[i-1] = die[i-1].roll();re are...
+ int parts = 0;
+ for(unsigned int i=0; i < s.length(); i++)
+ {
+ if(s[i] == 'd' || s[i] == '+')
+ {
+ parts++;
+ }
+ }
+ // ... so we know the number of times to decode values
+
+ int index=0;
+ unsigned d = s.find('d');
+ unsigned p = s.find('+');
+ while(d != static_cast<unsigned>(-1) || p != static_cast<unsigned>(-1))
+ {
+ bool dFirst = d < p;
+ if(dFirst)
+ {
+ string before = s.substr(0,d); // part before the 'd' (should be just one number)
+ // figure out what number is after 'd'
+ int count = 0;
+ bool foundDigit=false;
+ for(int i=0; i< static_cast<signed>(s.length()-d-1); i++)
+ {
+ if(isdigit(s[count+d+1]))
+ {
+ foundDigit=true;
+ }
+ if(!isdigit(s[count+d+1]) && foundDigit)
+ {
+ break;
+ }
+ count++;
+ }
+ string after = s.substr(d+1,count); //should be just the number after 'd'
+
+ // store these two parts
+ data[index] = before;
+ data[index+1] = after;
+ index+=2;
+
+
+ // remove this part from the string s
+ s = s.substr(d+count+1); // discard these two parts
+ }
+ else // same idea for the '+'
+ {
+ // figure out what number is after '+'
+ int count = 0;
+ bool foundDigit=false;
+ for(int i=0; i< static_cast<signed>(s.length()-p-1); i++)
+ {
+ if(isdigit(s[count+p+1]))
+ {
+ foundDigit=true;
+ }
+ if(!isdigit(s[count+p+1]) && foundDigit)
+ {
+ break;
+ }
+ count++;
+ }
+ string after = s.substr(p+1,count); //should be just the number after '+'
+
+ // store this part
+ data[index] = "+";
+ data[index+1] = after;
+ index+=2;
+
+
+ // remove this part from the string s
+ s = s.substr(p+count+1); // discard these two parts
+ }
+
+ // update d and p for next loop interation
+ d = s.find('d');
+ p = s.find('+');
+
+ }
+
+ // now we need to figure out how many dice there are (as 2d4 is 2 dice)
+ // we will treat "+2" as a die that rolls [2,2]
+ int diceCount = 0;
+ for(int i=0; i < parts*2; i+=2)
+ {
+ if(data[i][0] == '+')
+ {
+ diceCount++;
+ }
+ else
+ {
+ diceCount+=atoi(data[i].c_str());
+ }
+ }
+
+ dice[0] = diceCount*2+1; // put size in first index
+
+ int ind=1; // index for the "dice" array (as not same as data array)
+ for(int i=0; i < parts*2; i+=2)
+ {
+ // if we have a +, add a "Dice" that has a range of 0
+ if(data[i][0] == '+')
+ {
+ dice[ind] = atoi(data[i+1].c_str());
+ dice[ind+1] = atoi(data[i+1].c_str());
+
+ ind+=2;
+ }
+ else // otherwise add however many of the dice requested
+ {
+ for(int j=0; j < atoi(data[i].c_str()); j++)
+ {
+ dice[ind] = 1;
+ dice[ind+1] = atoi(data[i+1].c_str());
+
+ ind += 2;
+ }
+ }
+ }
+
+ return dice;
+}
diff --git a/ee1301/wk6/hw6_directory/strap012_HW6B.cpp b/ee1301/wk6/hw6_directory/strap012_HW6B.cpp index 87ba640..a845e45 100644 --- a/ee1301/wk6/hw6_directory/strap012_HW6B.cpp +++ b/ee1301/wk6/hw6_directory/strap012_HW6B.cpp @@ -1,34 +1,34 @@ -//Matthew Strapp -//EE1301 -//17 April 2019 -//HW 6B: Recursion and ff(x) -#include <iostream> - -int ff(int x); -int main() { - int x; - do { - std::cout << "Please enter a value of x: "; - std::cin >> x; - } while (x<0); - std::cout << "Beginning calculation of ff(x)...\n"; - std::cout << "Calcuation complete, ff(x) = " << ff(x) << std::endl; -} - -// This function either returns 1 when x is one of two recrusive conditions depending on if x is even or odd. -int ff(int x) { - if (x > 1) { - if (x%2 == 0) { - // x is even - std::cout << "Calling ff(" << x/2 << ")\n"; - return x*ff(x/2); - } else { - // x is odd but not 1 - std::cout << "Calling ff(" << x-2 << ")\n"; - return x*ff(x-2); - } - } else { - // x is 1 (or 0) - return 1; - } -} +//Matthew Strapp
+//EE1301
+//17 April 2019
+//HW 6B: Recursion and ff(x)
+#include <iostream>
+
+int ff(int x);
+int main() {
+ int x;
+ do {
+ std::cout << "Please enter a value of x: ";
+ std::cin >> x;
+ } while (x<0);
+ std::cout << "Beginning calculation of ff(x)...\n";
+ std::cout << "Calcuation complete, ff(x) = " << ff(x) << std::endl;
+}
+
+// This function either returns 1 when x is one of two recrusive conditions depending on if x is even or odd.
+int ff(int x) {
+ if (x > 1) {
+ if (x%2 == 0) {
+ // x is even
+ std::cout << "Calling ff(" << x/2 << ")\n";
+ return x*ff(x/2);
+ } else {
+ // x is odd but not 1
+ std::cout << "Calling ff(" << x-2 << ")\n";
+ return x*ff(x-2);
+ }
+ } else {
+ // x is 1 (or 0)
+ return 1;
+ }
+}
diff --git a/ee1301/wk6/lab5/Bug.cpp b/ee1301/wk6/lab5/Bug.cpp index 337e84b..b4ef154 100644 --- a/ee1301/wk6/lab5/Bug.cpp +++ b/ee1301/wk6/lab5/Bug.cpp @@ -1,12 +1,12 @@ -#include "Bug.hpp" - -int main() { - Bug buggy(10); - buggy.display(); - buggy.move(); - buggy.display(); - buggy.turn(); - buggy.display(); - buggy.move(); - buggy.display(); -} +#include "Bug.hpp"
+
+int main() {
+ Bug buggy(10);
+ buggy.display();
+ buggy.move();
+ buggy.display();
+ buggy.turn();
+ buggy.display();
+ buggy.move();
+ buggy.display();
+}
diff --git a/ee1301/wk6/lab5/Bug.hpp b/ee1301/wk6/lab5/Bug.hpp index f3bfae1..cc704ab 100644 --- a/ee1301/wk6/lab5/Bug.hpp +++ b/ee1301/wk6/lab5/Bug.hpp @@ -1,28 +1,28 @@ -#include <iostream> - -#ifndef BUG_H - -#define BUG_H -class Bug { -private: - int position, dir; -public: - Bug() { - position=0; - dir=1; - } - Bug(int pos) { - position=pos; - dir=1; - } - void move() { - position+=dir; - } - void turn() { - dir*=-1; - } - void display() { - std::cout << "position = " << position << ", direction = " << dir << std::endl; - } -}; -#endif +#include <iostream>
+
+#ifndef BUG_H
+
+#define BUG_H
+class Bug {
+private:
+ int position, dir;
+public:
+ Bug() {
+ position=0;
+ dir=1;
+ }
+ Bug(int pos) {
+ position=pos;
+ dir=1;
+ }
+ void move() {
+ position+=dir;
+ }
+ void turn() {
+ dir*=-1;
+ }
+ void display() {
+ std::cout << "position = " << position << ", direction = " << dir << std::endl;
+ }
+};
+#endif
diff --git a/ee1301/wk6/lab5/momentum.cpp b/ee1301/wk6/lab5/momentum.cpp index a49be54..dfbaeee 100644 --- a/ee1301/wk6/lab5/momentum.cpp +++ b/ee1301/wk6/lab5/momentum.cpp @@ -1,28 +1,28 @@ -#include <iostream> - -double momentum (double velocity, double mass); - -int main() { - double velocity[3]; - double mass; - std::cout << "Please enter velocity (x y z) [m/s]: "; - 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: <"; - for (int i=0; i<=2; i++) { - vector[i]=momentum(velocity[i], mass); - std::cout << vector[i]; - if ((i<2)) - std::cout << ","; - } - delete[] vector; - std::cout << ">" << std::endl; -} - -double momentum(double velocity, double mass) { - return velocity * mass; +#include <iostream>
+
+double momentum (double velocity, double mass);
+
+int main() {
+ double velocity[3];
+ double mass;
+ std::cout << "Please enter velocity (x y z) [m/s]: ";
+ 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: <";
+ for (int i=0; i<=2; i++) {
+ vector[i]=momentum(velocity[i], mass);
+ std::cout << vector[i];
+ if ((i<2))
+ std::cout << ",";
+ }
+ delete[] vector;
+ std::cout << ">" << std::endl;
+}
+
+double momentum(double velocity, double mass) {
+ return velocity * mass;
}
\ No newline at end of file diff --git a/ee1301/wk6/lab5/strap012_lab5_w_1.cpp b/ee1301/wk6/lab5/strap012_lab5_w_1.cpp index 2c3a92f..9f5e285 100644 --- a/ee1301/wk6/lab5/strap012_lab5_w_1.cpp +++ b/ee1301/wk6/lab5/strap012_lab5_w_1.cpp @@ -1,75 +1,75 @@ -#include <iostream> -//#include <time.h> //Needed if using MinGW - -class DeckOfCards { -private: - int index=0, deck[52]; -public: - DeckOfCards(); - void shuffle(); - int dealCard(); -}; - -void showHand(int hand[], const int size); -int main() { - srand(time(NULL)); - const int size=4; //Size can be changed for larger hands - DeckOfCards deck; - int hand[size]; - for (int i=0; i<13; i++) { - for (int j=0; j<size; j++) { - hand[j]=deck.dealCard(); - } - showHand(hand, size); - } -} - -//Prints out hand shuffled from before -void showHand(int hand[], const int size) { - for (int i=0; i<size; i++) { - //Switch case needed for showing face cards and aces - switch(hand[i]%13) { - case 0: std::cout << 'A'; - break; - case 10: std::cout << 'J'; - break; - case 11: std::cout << 'Q'; - break; - case 12: std::cout << 'K'; - break; - default: std::cout << hand[i]%13+1; - } - std::cout << " "; - } - std::cout << std::endl; -} - -//Deck initialized with 1-52 and shuffled -DeckOfCards::DeckOfCards() { - for(int i=0; i<51; i++) { - deck[i]=i+1; - } - shuffle(); -} - -//Implementation of Knuth Shuffle -void DeckOfCards::shuffle() { - int j=0, temp=0; - for (int i=50; i>1; i--) { - j = rand() % 50 + 1; - if (j < i) { - temp=deck[i]; deck[i]=deck[j]; deck[j]=temp; - } - } -} - -//Function to deal the card when asked by grabbing from the deck and shuffling if such card does not exist. -//Returns the card drawn from the deck -int DeckOfCards::dealCard() { - index++; - if (index>=52) { - index=0; - shuffle(); - } - return deck[index]; +#include <iostream>
+//#include <time.h> //Needed if using MinGW
+
+class DeckOfCards {
+private:
+ int index=0, deck[52];
+public:
+ DeckOfCards();
+ void shuffle();
+ int dealCard();
+};
+
+void showHand(int hand[], const int size);
+int main() {
+ srand(time(NULL));
+ const int size=4; //Size can be changed for larger hands
+ DeckOfCards deck;
+ int hand[size];
+ for (int i=0; i<13; i++) {
+ for (int j=0; j<size; j++) {
+ hand[j]=deck.dealCard();
+ }
+ showHand(hand, size);
+ }
+}
+
+//Prints out hand shuffled from before
+void showHand(int hand[], const int size) {
+ for (int i=0; i<size; i++) {
+ //Switch case needed for showing face cards and aces
+ switch(hand[i]%13) {
+ case 0: std::cout << 'A';
+ break;
+ case 10: std::cout << 'J';
+ break;
+ case 11: std::cout << 'Q';
+ break;
+ case 12: std::cout << 'K';
+ break;
+ default: std::cout << hand[i]%13+1;
+ }
+ std::cout << " ";
+ }
+ std::cout << std::endl;
+}
+
+//Deck initialized with 1-52 and shuffled
+DeckOfCards::DeckOfCards() {
+ for(int i=0; i<51; i++) {
+ deck[i]=i+1;
+ }
+ shuffle();
+}
+
+//Implementation of Knuth Shuffle
+void DeckOfCards::shuffle() {
+ int j=0, temp=0;
+ for (int i=50; i>1; i--) {
+ j = rand() % 50 + 1;
+ if (j < i) {
+ temp=deck[i]; deck[i]=deck[j]; deck[j]=temp;
+ }
+ }
+}
+
+//Function to deal the card when asked by grabbing from the deck and shuffling if such card does not exist.
+//Returns the card drawn from the deck
+int DeckOfCards::dealCard() {
+ index++;
+ if (index>=52) {
+ index=0;
+ shuffle();
+ }
+ return deck[index];
}
\ No newline at end of file diff --git a/ee1301/wk6/lab5/vectorArray.cpp b/ee1301/wk6/lab5/vectorArray.cpp index 6e2d548..92d23bd 100644 --- a/ee1301/wk6/lab5/vectorArray.cpp +++ b/ee1301/wk6/lab5/vectorArray.cpp @@ -1,43 +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==0) { - mass=rand()%10+1.0; - } else { - mass=rand()%10*-1.0+1.0; - } - return mass*velocity; +#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==0) {
+ mass=rand()%10+1.0;
+ } else {
+ mass=rand()%10*-1.0+1.0;
+ }
+ return mass*velocity;
}
\ No newline at end of file diff --git a/ee1301/wk6/lab5/wUp.cpp b/ee1301/wk6/lab5/wUp.cpp index 492c092..0cb3957 100644 --- a/ee1301/wk6/lab5/wUp.cpp +++ b/ee1301/wk6/lab5/wUp.cpp @@ -1,27 +1,27 @@ -#include <iostream> -using namespace std; - -class Point -{ - public: -void showPoint( ); -Point( ); -Point(int newX, int newY) { - xlocation = newX; - ylocation = newY; -} -int xlocation; -int ylocation; -} ; - - -int main() { - int a(1); - int b(2); - int *p1; - p1 = &a; - Point *p2 = new Point(*p1, b); - *p1 = p2->ylocation; - p2->xlocation = 10; - cout << p2->xlocation << ' ' << *p1 << ' ' << b << ' ' << a << endl; +#include <iostream>
+using namespace std;
+
+class Point
+{
+ public:
+void showPoint( );
+Point( );
+Point(int newX, int newY) {
+ xlocation = newX;
+ ylocation = newY;
+}
+int xlocation;
+int ylocation;
+} ;
+
+
+int main() {
+ int a(1);
+ int b(2);
+ int *p1;
+ p1 = &a;
+ Point *p2 = new Point(*p1, b);
+ *p1 = p2->ylocation;
+ p2->xlocation = 10;
+ cout << p2->xlocation << ' ' << *p1 << ' ' << b << ' ' << a << endl;
}
\ No newline at end of file diff --git a/ee1301/wk6/lab5/warmup.txt b/ee1301/wk6/lab5/warmup.txt index 50a1857..e18288c 100644 --- a/ee1301/wk6/lab5/warmup.txt +++ b/ee1301/wk6/lab5/warmup.txt @@ -1,15 +1,15 @@ -1) - a. Line 5 constructs the class with no values. - b. It'll work, but it might not work well. - c. No. - d. Point(int newX, int newY); -2) - a. This changes the value xlocation and ylocation in the Point class to 3 and 10 respectively. - This changes the default values. - b. The object p1 of the class Point is initialized with an xlocation of 3 and a ylocation of 6. - c. The declaration is incorrect. - d. Line 6 is a function of return type Point with no input. - e. Point p2; -3) - a. 10 2 2 2 - b. The array is not deleted (memory leak) +1)
+ a. Line 5 constructs the class with no values.
+ b. It'll work, but it might not work well.
+ c. No.
+ d. Point(int newX, int newY);
+2)
+ a. This changes the value xlocation and ylocation in the Point class to 3 and 10 respectively.
+ This changes the default values.
+ b. The object p1 of the class Point is initialized with an xlocation of 3 and a ylocation of 6.
+ c. The declaration is incorrect.
+ d. Line 6 is a function of return type Point with no input.
+ e. Point p2;
+3)
+ a. 10 2 2 2
+ b. The array is not deleted (memory leak)
|