From f12f962fbcb40f667a9c7251b5d8266d15810fcf Mon Sep 17 00:00:00 2001 From: Arlo Date: Mon, 14 Aug 2017 15:35:57 +0200 Subject: [PATCH 01/11] Exercise 2.1 --- guessing-game.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 guessing-game.cpp diff --git a/guessing-game.cpp b/guessing-game.cpp new file mode 100644 index 0000000..8625ff0 --- /dev/null +++ b/guessing-game.cpp @@ -0,0 +1,48 @@ +#include +#include +#include + +using namespace std; + +int main() { + + int UserGuess = 0 ; + int RandNum = 0; + int TurnCounter = 0; + + srand(time(0)); + + RandNum =rand() % 100+1; + + + cout <<"Please enter your guess."<> UserGuess; + + if (TurnCounter ==4 && UserGuess!=RandNum){ + cout<< "You lose"<RandNum){ + cout << "Guess lower" < Date: Mon, 14 Aug 2017 15:38:26 +0200 Subject: [PATCH 02/11] Exercise 3.1 --- complex/complex.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/complex/complex.cpp b/complex/complex.cpp index 8f52bb3..ccdeef6 100644 --- a/complex/complex.cpp +++ b/complex/complex.cpp @@ -21,3 +21,7 @@ int main() return 0; } +//The commented line does not work because the "answer++" declaration +//is attempting to use the shorthand incrementation that is solely used for integer types and therefore cannot increment the float data type + + From 9dc37ea8c1770ae106f7ed988b8d0bbdf4666596 Mon Sep 17 00:00:00 2001 From: Arlo Date: Mon, 14 Aug 2017 15:45:21 +0200 Subject: [PATCH 03/11] Exercise 3.2 --- complex/complex.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/complex/complex.cpp b/complex/complex.cpp index ccdeef6..9d74e5c 100644 --- a/complex/complex.cpp +++ b/complex/complex.cpp @@ -5,17 +5,19 @@ #include // required for the complex class using namespace std; +using Comp = complex; -int main() -{ - complex num1{ 2.0, 2.0 }; // using C++11 uniform initialisation syntax - complex num2{ 4.0, -2.0 }; // old syntax: complex num2(4.0,-2.0); +int main(){ + +Comp num1{ 2.0, 2.0 }; // using C++11 uniform initialisation syntax - auto answer = num1 * num2; // using C++11 auto keyword, - // answer is of type: complex +Comp num2{ 4.0, -2.0 }; // old syntax: complex num2(4.0,-2.0); - cout << "The answer is: " << answer << endl; - cout << "Or in a more familiar form: " << answer.real() << " + " << answer.imag() << "j" << endl; +auto answer = num1 * num2; // using C++11 auto keyword, + // answer is of type: complex + +cout << "The answer is: " << answer << endl; +cout << "Or in a more familiar form: " << answer.real() << " + " << answer.imag() << "j" << endl; // answer++; From c16889ef1f544efe65fa9b57d5d3f7dce349fb51 Mon Sep 17 00:00:00 2001 From: Arlo Date: Mon, 14 Aug 2017 15:47:10 +0200 Subject: [PATCH 04/11] Exercise 3.3 --- complex/complex.cpp | 57 ++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/complex/complex.cpp b/complex/complex.cpp index 9d74e5c..1385e4d 100644 --- a/complex/complex.cpp +++ b/complex/complex.cpp @@ -1,29 +1,44 @@ -// complex.cpp -// Multiplying complex numbers - #include -#include // required for the complex class +#include using namespace std; -using Comp = complex; - -int main(){ - -Comp num1{ 2.0, 2.0 }; // using C++11 uniform initialisation syntax - -Comp num2{ 4.0, -2.0 }; // old syntax: complex num2(4.0,-2.0); -auto answer = num1 * num2; // using C++11 auto keyword, - // answer is of type: complex - -cout << "The answer is: " << answer << endl; -cout << "Or in a more familiar form: " << answer.real() << " + " << answer.imag() << "j" << endl; - - // answer++; +using Comp = complex; + +int main() { + + Comp a = 0.0; + Comp b = 0.0; + Comp c = 0.0; + char Continue; + + while(Continue != 'q'){ + + cout << "Please enter a :" << endl; + cin >> a; + cout << "Please enter b :" << endl; + cin>> b; + cout << "Please enter c :" << endl; + cin >> c; + + +Comp Root = sqrt(pow(b,2) -4.0*a*c); + +Comp answer1 = (-b + Root)/(2.0*a); + +Comp answer2 = (-b - Root)/(2.0*a); + +cout << "The roots are: " << answer1.real() << " + " <> Continue; + + + } return 0; } -//The commented line does not work because the "answer++" declaration -//is attempting to use the shorthand incrementation that is solely used for integer types and therefore cannot increment the float data type - From 94cee2db4b4e8e5b4e4d64651c458d7925835b9c Mon Sep 17 00:00:00 2001 From: Arlo Date: Mon, 14 Aug 2017 15:49:55 +0200 Subject: [PATCH 05/11] Exercise 4.1 --- screen/scr_main.cpp | 61 ++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/screen/scr_main.cpp b/screen/scr_main.cpp index 14e6f78..2a0a0d7 100644 --- a/screen/scr_main.cpp +++ b/screen/scr_main.cpp @@ -1,4 +1,4 @@ -// Exercising the Screen class +// Making the initial A #include "screen.h" #include @@ -7,27 +7,42 @@ using namespace std; int main() { Screen myScreen{6,6}; - - myScreen.forward(); - myScreen.set('*'); - myScreen.down(); + + myScreen.clear(' '); + myScreen.move(1,3); myScreen.set('*'); - myScreen.move(3,3); - myScreen.set("---"); - - myScreen.display(); - cout << endl; - - myScreen.reSize(16,16); - myScreen.display(); - myScreen.clear(' '); - - myScreen.move(7,7); - myScreen.set("BIG"); - myScreen.move(8,5); - myScreen.set("SCREEN"); - myScreen.display(); - - return 0; + myScreen.forward(); + myScreen.set('*'); + myScreen.move(2,2); + myScreen.set('*'); + myScreen.move(2,5); + myScreen.set('*'); + myScreen.move(3,6); + myScreen.set('*'); + myScreen.move(3,1); + myScreen.set('*'); + myScreen.move(4,1); + myScreen.set('*'); + myScreen.forward(); + myScreen.set('*'); + myScreen.forward(); + myScreen.set('*'); + myScreen.forward(); + myScreen.set('*'); + myScreen.forward(); + myScreen.set('*'); + myScreen.forward(); + myScreen.set('*'); + myScreen.move(5,1); + myScreen.set('*'); + myScreen.move(6,1); + myScreen.set('*'); + myScreen.move(5,6); + myScreen.set('*'); + myScreen.move(6,6); + myScreen.set('*'); + + myScreen.display(); + + return 0; } - From 3c4b784763a8ff51668d3bf86487846223828ab9 Mon Sep 17 00:00:00 2001 From: Arlo Date: Mon, 14 Aug 2017 15:52:29 +0200 Subject: [PATCH 06/11] Exercise 4.2 --- screen/screen.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/screen/screen.cpp b/screen/screen.cpp index 12d8625..15e93bd 100644 --- a/screen/screen.cpp +++ b/screen/screen.cpp @@ -180,3 +180,17 @@ string::size_type Screen::row() const return (_cursor + _width)/_width; } +/*Three different instances of the const type in use include: + + --const string::size_type TOP_LEFT = 0; + --This is used so that the user is unable to change the position of the top left corner of the screen + + --string::size_type Screen::remainingSpace() const + --This is used to make sure that this member function can not change the member variable of the class + + -- void Screen::set( const string& s ) + -- This is used to make sure that the fucntion "set" cannot modify the variable "s" within the function "set" + +The "at" function of string class used in function "reSize" returns a reference to the character at specified location "pos". +Bounds checking is performed, exception of type std::out_of_range will be thrown on invalid access +reference to the requested character. From 23c076d2ae42c37f10a4a10909b2a0198bcf46db Mon Sep 17 00:00:00 2001 From: Arlo Date: Mon, 14 Aug 2017 15:56:43 +0200 Subject: [PATCH 07/11] Exercise 4.3 --- screen/screen.cpp | 33 +++++++++++++++++++++++++++++++++ screen/screen.h | 4 +++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/screen/screen.cpp b/screen/screen.cpp index 15e93bd..9acff4f 100644 --- a/screen/screen.cpp +++ b/screen/screen.cpp @@ -69,6 +69,32 @@ void Screen::move( string::size_type row, string::size_type col ) return; } +void Screen::move(Direction dir) +{ + if(dir == Direction::FORWARD){ + forward(); + } + if(dir == Direction::BACK){ + back(); + } + if(dir == Direction::UP){ + up(); + } + if(dir == Direction::DOWN){ + down(); + } + + if(dir == Direction::HOME){ + home(); + } + + + if(dir == Direction::END){ + end(); + } + + return; +} char Screen::get( string::size_type row, string::size_type col ) { // position _cursor @@ -194,3 +220,10 @@ string::size_type Screen::row() const The "at" function of string class used in function "reSize" returns a reference to the character at specified location "pos". Bounds checking is performed, exception of type std::out_of_range will be thrown on invalid access reference to the requested character. + + +* 4.3 + +No this is not a necessity because all of the functions called within the overloaded move function +already exist and it just lumps them all together in one function when they all exist separately anyway. +The function is just a copy of existing code. diff --git a/screen/screen.h b/screen/screen.h index 954c360..096c5ec 100644 --- a/screen/screen.h +++ b/screen/screen.h @@ -12,6 +12,8 @@ using namespace std; // *size* of any string that can be held by the string class as well as any index into // the string. +enum class Direction {HOME,FORWARD,BACK,UP,DOWN,END}; + class Screen { public: // Screen's constructor @@ -36,7 +38,7 @@ class Screen { void down(); // move the cursor to the specified row and column void move(string::size_type row, string::size_type col); - + void move(Direction dir); // get the character at the cursor's current position char get() const { return _screen[_cursor]; } // get the character at the specified row and column From 82bcffbc3462ec0877f49926c64dbd10caa39c86 Mon Sep 17 00:00:00 2001 From: Arlo Date: Mon, 14 Aug 2017 15:59:30 +0200 Subject: [PATCH 08/11] Exercise 4.4 --- screen/screen.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/screen/screen.cpp b/screen/screen.cpp index 9acff4f..bec9093 100644 --- a/screen/screen.cpp +++ b/screen/screen.cpp @@ -38,7 +38,7 @@ void Screen::up() { // move _cursor up one row of screen // do not wrap around if ( row() == 1 ) // at top? - cerr << "Screen::up - Cannot wrap around in a vertical direction" << endl; + _cursor = _cursor + (_width*(_height-1)); else _cursor -= _width; @@ -49,7 +49,7 @@ void Screen::down() { // move _cursor down one row of screen // do not wrap around if ( row() == _height ) // at bottom? - cerr << "Screen::down - Cannot wrap around in a vertical direction" << endl; + _cursor = _cursor - (_width*(_height-1)); else _cursor += _width; From 5592b45bcceff42ba356b82a7f6c1fec5a98ad00 Mon Sep 17 00:00:00 2001 From: Arlo Date: Mon, 14 Aug 2017 16:04:00 +0200 Subject: [PATCH 09/11] Exercise 4.5 --- screen/screen.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++ screen/screen.h | 1 + 2 files changed, 54 insertions(+) diff --git a/screen/screen.cpp b/screen/screen.cpp index bec9093..1dcfdf9 100644 --- a/screen/screen.cpp +++ b/screen/screen.cpp @@ -13,6 +13,49 @@ Screen::Screen(string::size_type height, string::size_type width, char bkground) // character value of bkground { /* all the work is done with the member initialization list */ } +void Screen::EmptySquare(unsigned int x,unsigned int y, unsigned int length){ // x and y are the cords of the top left corner with length + + if(x + (length-1) > _width){ + + cerr<<"Not enough space for entered dimensions" << endl; + return; + } + + if(y + (length-1) > _height){ + + cerr<<"Not enough space for entered dimensions" << endl; + return; + + } + + + clear(' '); + move(x,y); // sets original starting position + set('*'); + + for(int j =0; j Date: Mon, 14 Aug 2017 16:05:32 +0200 Subject: [PATCH 10/11] Exercise 4.6 --- screen/screen.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/screen/screen.cpp b/screen/screen.cpp index 1dcfdf9..0ecac6a 100644 --- a/screen/screen.cpp +++ b/screen/screen.cpp @@ -279,4 +279,16 @@ No a function like this does not, in our opinion, form part of the Screen class to create another class purely for drawing shpaes as each class should have its own focus on a task and not get complicated with functions that dont fit the purpose of the class. +*/ + +/*4.6 + +Yes instead of using a string type as the screen displayed you could implement a 2D character array to perform the same function which +would be more intutive as it it basically a grid just like a screen should be interperated. + +It is easier to change the implementation rather than the interface because if the way the fucntions work are changed (for example using a 2D character array) +its less work than writing all new functions to work with the changes the developer wishes to make as the interface already perfoms the correct purpose. + + + */ \ No newline at end of file From bcfddfbb019309c1ce6a0d08084813c37b8d2706 Mon Sep 17 00:00:00 2001 From: Arlo Date: Mon, 14 Aug 2017 16:08:47 +0200 Subject: [PATCH 11/11] Exercise 5.1 --- stopwatch/StopWatch.cpp | 32 +++++++++++++++++++++++++++++--- stopwatch/StopWatch.h | 20 +++++++++++++++++--- stopwatch/Stopwatch_Main.cpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 stopwatch/Stopwatch_Main.cpp diff --git a/stopwatch/StopWatch.cpp b/stopwatch/StopWatch.cpp index 70fb928..b8fc7a0 100644 --- a/stopwatch/StopWatch.cpp +++ b/stopwatch/StopWatch.cpp @@ -1,10 +1,36 @@ #include "StopWatch.h" #include + using namespace std; -// returns the amount of time in seconds that has passed since the process (i.e. your program) started executing + // returns the amount of time in seconds that has passed since the process (i.e. your program) started executing double getProcessTime() { - clock_t time = clock(); - return static_cast(time)/CLOCKS_PER_SEC; + clock_t time = clock(); + return static_cast(time)/CLOCKS_PER_SEC; +} + +StopWatch::StopWatch( double startTime, double endTime): _startTime(startTime), _endTime(endTime){}; + +void StopWatch::StartWatch() +{ + _startTime = getProcessTime(); + return; +} + +void StopWatch::EndWatch() +{ + _endTime = getProcessTime(); +} + +void StopWatch::ResetWatch() +{ + _startTime = 0; + _endTime = 0; } + +double StopWatch::getFunctionTime() // calculate the lapsed time in calculating function +{ + return _endTime - _startTime; +} + diff --git a/stopwatch/StopWatch.h b/stopwatch/StopWatch.h index 7e99fd0..ad4387e 100644 --- a/stopwatch/StopWatch.h +++ b/stopwatch/StopWatch.h @@ -1,8 +1,22 @@ #ifndef STOPWATCH_H #define STOPWATCH_H -// returns the amount of time in seconds that has passed since the process (i.e. your program) started executing + // returns the amount of time in seconds that has passed since the process (i.e. your program) started executing double getProcessTime(); - -#endif +class StopWatch{ + +public: + + StopWatch( double startTime = 0, double endTime = 0 ); // class constructor + void StartWatch(); // stopwatch start function + void EndWatch(); // stopwatch end funtion + void ResetWatch(); // stop watch reset function + double getFunctionTime(); // caluclating the time lasped in the process being timed +private: + + double _startTime; + double _endTime; + }; + +#endif \ No newline at end of file diff --git a/stopwatch/Stopwatch_Main.cpp b/stopwatch/Stopwatch_Main.cpp new file mode 100644 index 0000000..87c595a --- /dev/null +++ b/stopwatch/Stopwatch_Main.cpp @@ -0,0 +1,33 @@ +#include "StopWatch.h" +#include +#include + +using namespace std; + +void FunctionInQuestion(); + +int main() +{ + StopWatch NewWatch; // declare object NewWatch + NewWatch.StartWatch(); // start the time for the stop watch + //Function Start + FunctionInQuestion(); // run the function to be timed + //Function End + NewWatch.EndWatch(); // stop the time + + cout << "The function took "<< NewWatch.getFunctionTime() << " seconds" << endl; + NewWatch.ResetWatch(); // the reset function to clear previous time + cout << NewWatch.getFunctionTime() << " -the Stopwatch has been reset" << endl; + return 0; +} + +void FunctionInQuestion() +{ + int i = 0; + while(i != 1000000000) + { + i++; + } + + return; +}