Skip to content

Commit 113e335

Browse files
committedMar 8, 2022
Neaten C++ and MATLAB tutorials
1 parent a3d281f commit 113e335

File tree

2 files changed

+59
-153
lines changed

2 files changed

+59
-153
lines changed
 

‎C++/02 C++ - Functions and File IO.md

+53-73
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Functional C++ Crash Course
1+
# Function C++ Crash Course
22

33
Author: methylDragon
44
Contains a syntax reference for C++
@@ -15,28 +15,22 @@ I'll be adapting it from the ever amazing Derek Banas: https://www.youtube.com/w
1515
- Linux (**Terminal/Console proficiency**) (We're going to need to compile our stuff)
1616
- Gone through the all preceding parts of the tutorial
1717

18-
19-
2018
## Table Of Contents <a name="top"></a>
2119

2220
1. [Introduction](#1)
23-
2. [Functional C++ Syntax Reference](#2)
21+
2. [Function C++ Syntax Reference](#2)
2422
2.1 [Functions](#2.1)
2523
2.2 [Function Overloading](#2.2)
2624
2.3 [Recursive Functions](#2.3)
2725
2.4 [File I/O](#2.4)
2826
2.5 [Lambda Functions](#2.5)
2927
2.6 [Inline Functions](#2.6)
3028

31-
32-
3329
## 1. Introduction <a name="1"></a>
3430

3531
We've gone through the basics of C++. Now let's throw in some functions and file interactions!
3632

37-
38-
39-
## 2. Functional C++ Syntax Reference <a name="2"></a>
33+
## 2. Function C++ Syntax Reference <a name="2"></a>
4034

4135
### 2.1 Functions <a name="2.1"></a>
4236

@@ -51,10 +45,10 @@ firstNum and secondNum are attributes. We set secondNum's default value if no va
5145

5246
```c++
5347
int addNumbers(int firstNum, int secondNum = 0) {
54-
int combinedValue = firstNum + secondNum;
55-
56-
return combinedValue;
57-
48+
int combinedValue = firstNum + secondNum;
49+
50+
return combinedValue;
51+
5852
}
5953
```
6054
@@ -72,14 +66,12 @@ addNumbers(1, 2); // It'll return 3
7266
```
7367

7468
> **Note:**
75-
>
69+
>
7670
> - When you write the data-type infront of the function name, you're defining a new function **prototype**
7771
> - If you don't, you're calling it.
78-
>
72+
>
7973
> Remember the distinction!
8074
81-
82-
8375
#### **Returning arrays**
8476

8577
Read more + code source: https://www.geeksforgeeks.org/return-local-array-c-function/
@@ -99,10 +91,10 @@ int *fun()
9991

10092
arr[0] = 10;
10193
arr[1] = 20;
102-
94+
10395
return arr; // Return the pointer to the array!
10496
}
105-
97+
10698
int main()
10799
{
108100
int *ptr = fun();
@@ -122,10 +114,10 @@ int *fun()
122114

123115
arr[0] = 10;
124116
arr[1] = 20;
125-
117+
126118
return arr; // Return the pointer to the array!
127119
}
128-
120+
129121
int main()
130122
{
131123
int *ptr = fun();
@@ -141,17 +133,17 @@ struct arrWrap
141133
{
142134
int arr[100];
143135
};
144-
136+
145137
struct arrWrap fun() // Function returns the struct
146138
{
147139
struct arrWrap x;
148-
140+
149141
x.arr[0] = 10;
150142
x.arr[1] = 20;
151-
143+
152144
return x;
153145
}
154-
146+
155147
int main()
156148
{
157149
struct arrWrap x = fun();
@@ -160,8 +152,6 @@ int main()
160152
}
161153
```
162154
163-
164-
165155
### 2.2 Function Overloading <a name="2.2"></a>
166156
167157
[go to top](#top)
@@ -186,8 +176,6 @@ void sameFunction(int a, float b){
186176

187177
Functions can have the same name, but do DIFFERENT THINGS depending on what gets passed to them!
188178

189-
190-
191179
### 2.3 Recursive Functions <a name="2.3"></a>
192180

193181
[go to top](#top)
@@ -196,21 +184,19 @@ These are functions that call THEMSELVES. Trippy.
196184

197185
```c++
198186
int getFactorial(int number){
199-
int sum;
200-
if(number == 1)
201-
sum = 1; //Yeah you can do this
202-
else
203-
sum = getFactorial(number - 1) * number;
204-
return sum;
187+
int sum;
188+
if(number == 1)
189+
sum = 1; //Yeah you can do this
190+
else
191+
sum = getFactorial(number - 1) * number;
192+
return sum;
205193
)
206194
```
207195

208196
This function returns the factorial of itself, and it keeps calling itself which results in it calling itself until it stops, then it resolves one by one till all are resolved.
209197

210198
The last function calls to go on the stack are resolved first! USE THE STACK! Play some Magic!
211199

212-
213-
214200
### 2.4 File I/O <a name="2.4"></a>
215201

216202
[go to top](#top)
@@ -226,13 +212,13 @@ ofstream writer("dragonQuote.txt"); //Open a .txt file called dragonQuote, this
226212

227213
if(! writer) { //Check to see if the filestream is open
228214

229-
cout << "Error opening file" << endl;
230-
return -1; // Return -1 if failed
215+
cout << "Error opening file" << endl;
216+
return -1; // Return -1 if failed
231217

232218
} else {
233219

234-
writer << dragonQuote << endl; // Write dragonQuote to writer, which causes dragonQuote.txt to contain only dragonQuote
235-
writer.close(); // Close the file
220+
writer << dragonQuote << endl; // Write dragonQuote to writer, which causes dragonQuote.txt to contain only dragonQuote
221+
writer.close(); // Close the file
236222

237223
}
238224

@@ -246,41 +232,39 @@ ofstream writer2("dragonQuote.txt", ios::app); //Create a writer object that app
246232

247233
if(! writer2) { //Check to see if the filestream is open
248234

249-
cout << "Error opening file" << endl;
250-
return -1; // Return -1 if failed
235+
cout << "Error opening file" << endl;
236+
return -1; // Return -1 if failed
251237

252238
} else {
253239

254-
writer2 << "\n -methylDragon" << endl; // Append "\n -methylDragon"
255-
writer2.close(); // Close the file
240+
writer2 << "\n -methylDragon" << endl; // Append "\n -methylDragon"
241+
writer2.close(); // Close the file
256242

257243
}
258244

259245
{
260-
char letter;
261-
262-
ifstream reader("dragonQuote.txt"); // Open an input filestream that reads dragonQuote.txt
263-
264-
if(! reader){
265-
266-
cout << "Error opening file" << endl;
267-
return -1;
268-
269-
} else {
270-
271-
for(int i = 0; ! reader.eof(); i++) { // Read until end of file
272-
reader.get(letter); // Get the next letter
273-
cout << letter; // And print it
274-
}
275-
276-
}
277-
278-
cout << endl;
279-
reader.close();
280-
}
281-
```
246+
char letter;
247+
248+
ifstream reader("dragonQuote.txt"); // Open an input filestream that reads dragonQuote.txt
249+
250+
if(! reader){
251+
252+
cout << "Error opening file" << endl;
253+
return -1;
254+
255+
} else {
282256

257+
for(int i = 0; ! reader.eof(); i++) { // Read until end of file
258+
reader.get(letter); // Get the next letter
259+
cout << letter; // And print it
260+
}
283261

262+
}
263+
264+
cout << endl;
265+
reader.close();
266+
}
267+
```
284268
285269
### 2.5 Lambda Functions <a name="2.5"></a>
286270
@@ -419,8 +403,6 @@ auto y = [&r = x, x = x+1]()->int {
419403
}(); // Updates x to 6, and initializes y to 7.
420404
```
421405

422-
423-
424406
### 2.6 Inline Functions <a name="2.6"></a>
425407

426408
Inline functions help to avoid the overhead of function calls. When you compile the code, the entire call to an inline function is instead replaced with the code inside the function call itself.
@@ -458,15 +440,13 @@ class S
458440
public:
459441
int square(int s); // declare the function
460442
};
461-
443+
462444
inline int S::square(int s) // use inline prefix
463445
{
464-
446+
465447
}
466448
```
467449

468-
469-
470450
```
471451
. .
472452
. |\-^-/| .

‎MATLAB/MATLAB Crash Course.md

+6-80
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ I'll be adapting it from the ever amazing Derek Banas: https://www.youtube.com/w
1212

1313
- Systems
1414

15-
16-
1715
## 1. Introduction
1816

1917
MATLAB stands for Matrix Laboratory. It's a very famous program used for working with matrices.
@@ -33,8 +31,6 @@ If you ever need to ask for help, either Google for it, or use the `help` functi
3331
help <function>
3432
```
3533

36-
37-
3834
## 2. Command Line Interface
3935

4036
### 2.1 Path
@@ -43,8 +39,6 @@ Be sure to ensure that all scripts you want to write are in MATLAB **Path**, in
4339

4440
In the file explorer on the left, you may add folders and subfolders to your path by right clicking and selecting `Add To Path`. Alternatively, you may double click on a folder to focus it and add it to Path, since your Path also includes your current focused folder!
4541

46-
47-
4842
### 2.2 Clearing Data
4943

5044
```matlab
@@ -57,33 +51,25 @@ clear all % Clear everything
5751
clear <variable> % Clear single variable
5852
```
5953

60-
61-
6254
### 2.3 Configure Output
6355

6456
```matlab
6557
format compact % Keeps output compact!
6658
```
6759

68-
69-
7060
### 2.4 Stop Execution
7161

7262
Use `CTRL-c` to stop executing a particular command!
7363

74-
75-
7664
### 2.5 Precision
7765

7866
Precision is set to 15 digits by default. But you can configure that!
7967

80-
81-
82-
## 3. Basic MATLAB Syntax Reference <a name="2"></a>
68+
## 3. Basic MATLAB Syntax Reference
8369

8470
Click on New Script to start creating a new script!
8571

86-
### 3.1 Comments <a name="2.1"></a>
72+
### 3.1 Comments
8773

8874
[go to top](#top)
8975

@@ -99,8 +85,6 @@ Click on New Script to start creating a new script!
9985

10086
You can also use `CTRL-r` to comment and `CTRL-t` to uncomment!
10187

102-
103-
10488
### 3.2 Variables
10589

10690
Everything is a `double` by default in MATLAB.
@@ -148,8 +132,6 @@ realmax
148132
realmax('single')
149133
```
150134

151-
152-
153135
### 3.3 Basic Operations
154136

155137
I really shouldn't need to explain this... You can use MATLAB like a calculator, and the results can go into variables!
@@ -167,8 +149,6 @@ mod(5, 4) % Mod (get remainder)
167149
% NOTE! MATLAB doesn't have increment and decrement operators. So you can't do stuff like a++, b--, etc.
168150
```
169151

170-
171-
172152
### 3.4 Casting
173153

174154
```matlab
@@ -178,8 +158,6 @@ v3 = int8(v2) % Like so!
178158

179159
Just write the appropriate class identifier and use it like a function!
180160

181-
182-
183161
### 3.5 Printing and Displaying
184162

185163
```matlab
@@ -194,8 +172,6 @@ sprintf('5 + 4 = %d\n', 5 + 4)
194172
% With the %d here, it transplants the next variable argument into the string!
195173
```
196174

197-
198-
199175
### 3.6 User Input
200176

201177
Using the `;` suppresses the input printout after the user has entered the input!
@@ -222,8 +198,6 @@ vInput = input('Enter a vector : ');
222198
disp(vInput)
223199
```
224200

225-
226-
227201
### 3.7 Useful Math Functions
228202

229203
`help elfun` for a whole list of math functions
@@ -243,8 +217,6 @@ deg2rad(90)
243217
rad2deg(3.14)
244218
```
245219

246-
247-
248220
### 3.8 Conditionals
249221

250222
```matlab
@@ -287,8 +259,6 @@ switch score
287259
end % REMEMBER TO END
288260
```
289261

290-
291-
292262
### 3.9 Vectors
293263

294264
They're one dimensional rows or columns!
@@ -331,8 +301,6 @@ a * b
331301
% 4 8 2
332302
```
333303

334-
335-
336304
### 3.10 Vector Methods
337305

338306
```matlab
@@ -363,8 +331,6 @@ linspace(1, 20, 4) % Generates 4 numbers equally spaced between 1 and 20
363331
logspace(1, 3, 3) % Like linspace, but the spacing is logarithmic
364332
```
365333

366-
367-
368334
### 3.11 Matrices
369335

370336
It's MATLAB, not Vector lab.
@@ -402,8 +368,6 @@ a - b
402368
a + b
403369
```
404370

405-
406-
407371
### 3.12 Matrix Methods
408372

409373
The list is not exhaustive!
@@ -459,8 +423,6 @@ repmat(a, 2, 1) % Duplicate matrix into new matrix. Eg. If original matrix was 3
459423
repelem(m3, 2, 1) % Duplicates ELEMENTS, so in this case, each element is duplicated twice in terms of the row
460424
```
461425

462-
463-
464426
### 3.13 For Loops
465427

466428
For loops! It's pretty Pythonic. It iterates through a range.
@@ -488,8 +450,6 @@ for i = 1:length(b)
488450
end
489451
```
490452

491-
492-
493453
### 3.14 While Loops
494454

495455
```matlab
@@ -501,16 +461,14 @@ while i < 20
501461
i = i + 1; % Semicolon suppresses the print
502462
continue
503463
end % This end is for i
504-
464+
505465
i = i + 1;
506466
if i >= 10
507467
break
508468
end
509469
end
510470
```
511471

512-
513-
514472
### 3.15 Cell Arrays
515473

516474
You can store data of multiple types
@@ -533,8 +491,6 @@ a = ['A', 'BB', 'CCC']
533491
char_array = cellstr(a)
534492
```
535493

536-
537-
538494
### 3.16 Strings
539495

540496
Strings are vectors of characters!
@@ -587,8 +543,6 @@ lower(str)
587543
upper(str)
588544
```
589545

590-
591-
592546
### 3.17 Structures
593547

594548
Custom data types! Think C++ structs! Or Python dictionaries/maps.
@@ -615,8 +569,6 @@ fieldnames(methyl_struct)
615569
a(1) = methyl_struct
616570
```
617571

618-
619-
620572
### 3.18 Tables
621573

622574
Tables are labelled rows of data in a table format
@@ -644,8 +596,6 @@ test_table({'A', 'B'}, :) % This defaults to using the RowName as indexer
644596
a(ismember(test_table.b, {29, 42},:)
645597
```
646598

647-
648-
649599
### 3.19 File IO
650600

651601
```matlab
@@ -666,8 +616,6 @@ a = 123
666616
save -append params a % This appends to the normal variable
667617
```
668618

669-
670-
671619
### 3.20 Eval
672620

673621
If you know Python you should know what this does already.
@@ -680,8 +628,6 @@ eval(toExecute) % Executes it as:
680628
% total = 5 + 4
681629
```
682630

683-
684-
685631
### 3.21 Pausing
686632

687633
You can pause in MATLAB too! Think of it like Arduino `delay()` or Python `time.sleep()`
@@ -693,8 +639,6 @@ pause off % Disable pause
693639
pause on % Enable pause
694640
```
695641

696-
697-
698642
## 4. Functional and OOP MATLAB
699643

700644
### 4.1 Functions
@@ -742,8 +686,6 @@ function [varargout] = getNumbers(howMany)
742686
end
743687
```
744688

745-
746-
747689
### 4.2 Anonymous Functions
748690

749691
No named functions! Think lambda in python
@@ -782,8 +724,6 @@ function func = doMath2(num)
782724
end
783725
```
784726

785-
786-
787727
### 4.3 Recursive Functions
788728

789729
They call themselves!
@@ -798,8 +738,6 @@ end
798738
end
799739
```
800740

801-
802-
803741
### 4.4 Classes
804742

805743
Static members are shared amongst all members of a class
@@ -810,7 +748,7 @@ classdef Shape
810748
height
811749
width
812750
end
813-
751+
814752
methods(Static)
815753
function out = setGetNumShapes(data)
816754
% Persistent values are shared by all objects also
@@ -824,7 +762,7 @@ classdef Shape
824762
out = Var;
825763
end
826764
end
827-
765+
828766
methods
829767
% Define a constructor
830768
function obj = Shape(height, width)
@@ -863,8 +801,6 @@ Shape.setGetNumShapes
863801
a1 > a2
864802
```
865803

866-
867-
868804
### 4.5 Class Inheritance
869805

870806
```matlab
@@ -873,7 +809,7 @@ classdef Trapezoid < Shape % Trapezoid inherits from Shape
873809
properties
874810
width2
875811
end
876-
812+
877813
methods
878814
function obj = Trapezoid(height, width, width2)
879815
obj@Shape(height,width) % The @ sign means you're taking it from the parent
@@ -898,8 +834,6 @@ disp(a3)
898834
a3.getArea
899835
```
900836

901-
902-
903837
## 5. Plotting
904838

905839
### 5.1 Plotting in MATLAB
@@ -946,8 +880,6 @@ clf % Delete all figures
946880
y = A.*cos(2*pi .* t/T - 2*pi .* x/lambda + phi0);
947881
```
948882

949-
950-
951883
### 5.2 3D Plotting in MATLAB
952884

953885
#### **3D Plots and Meshgrids**
@@ -986,8 +918,6 @@ quiver3() % 3D
986918
colorbar % Add a colorbar!
987919
```
988920

989-
990-
991921
## 6. Intermediate and Advanced Math
992922

993923
### 6.1 Vector Calculus
@@ -999,18 +929,14 @@ curl()
999929
del2() % Discrete laplacian
1000930
```
1001931

1002-
1003-
1004932
```
1005933
. .
1006934
. |\-^-/| .
1007935
/| } O.=.O { |\
1008-
1009936
```
1010937

1011938
1012939

1013940
------
1014941

1015942
[![Yeah! Buy the DRAGON a COFFEE!](./assets/COFFEE%20BUTTON%20%E3%83%BE(%C2%B0%E2%88%87%C2%B0%5E).png)](https://www.buymeacoffee.com/methylDragon)
1016-

0 commit comments

Comments
 (0)
Please sign in to comment.