Skip to content

Commit ec246d3

Browse files
committed
add Chapter 6
1 parent 54c862e commit ec246d3

File tree

10 files changed

+276
-16
lines changed

10 files changed

+276
-16
lines changed

README.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,14 @@ Course 'CS205 C/C++ Program Design' in 2021 Fall at Southern University of Scien
3232
* [Lab notes](week05/Lab05.pptx)
3333
* [Examples](week05/examples)
3434

35-
## Chapter 6: Basics of Functions
35+
## [Chapter 6: Basics of Functions](week06/README.md)
3636

37-
### Function arguments (pass by value)
38-
39-
### array and pointer arguments
40-
41-
### C style array strings
42-
43-
### struct arguments (copy data or address)
44-
45-
### Reference and const
46-
47-
### Lab:
48-
49-
* an example to test integer arguments, pointer argument and reference arguments
37+
* [Lecture notes](week06/Lecture06.pptx)
38+
* [Lab notes](week06/Lab06.pptx)
39+
* [Examples](week06/examples)
5040

5141
## Chapter 7: Advances in Functions
5242

53-
### inline function (why inline? Comparison with macro)
54-
5543
### Default arguments
5644

5745
### Function overloading

week04/array-illustration.xlsx

575 Bytes
Binary file not shown.

week06/Lecture06.pptx

1.01 MB
Binary file not shown.

week06/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Chapter 6: Basics of Functions
2+
3+
## Function arguments (pass by value)
4+
5+
## array and pointer arguments
6+
7+
## C style array strings
8+
9+
## struct arguments (copy data or address)
10+
11+
## Reference and const
12+
13+
## inline function (why inline? Comparison with macro)
14+
15+
## Lab:
16+
17+
* an example to test integer arguments, pointer argument and reference arguments

week06/examples/function.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <iostream>
2+
#include <float.h>
3+
4+
struct Matrix
5+
{
6+
int rows;
7+
int cols;
8+
float * pData;
9+
};
10+
11+
float matrix_max(struct Matrix mat)
12+
{
13+
float max = FLT_MIN;
14+
//find max value of mat
15+
for(int r = 0; r < mat.rows; r++)
16+
for (int c = 0; c < mat.cols; c++)
17+
{
18+
float val = mat.pData[ r * mat.cols + c];
19+
max = ( max > val ? max : val);
20+
}
21+
return max;
22+
}
23+
24+
Matrix * create_matrix(int rows, int cols)
25+
{
26+
Matrix * p = new Matrix{rows, cols};
27+
p->pData = new float[p->rows * p->cols]{1.f, 2.f, 3.f};
28+
//you should check if the memory is allocated successfully
29+
return p;
30+
}
31+
32+
bool matrix_add(const Matrix & matA, const Matrix & matB, Matrix & matC)
33+
{
34+
// check the dimensions of the three matrices
35+
// re-create matC if needed
36+
// do: matC = matA + matB
37+
// return true if everything is right
38+
return true;
39+
}
40+
41+
int main()
42+
{
43+
using namespace std;
44+
45+
Matrix matA = {3,4};
46+
matA.pData = new float[matA.rows * matA.cols]{1.f, 2.f, 3.f};
47+
48+
Matrix matB = {4,8};
49+
matB.pData = new float[matB.rows * matB.cols]{10.f, 20.f, 30.f};
50+
51+
Matrix matC = {4, 2};
52+
matC.pData = new float[matC.rows * matC.cols]{100.f, 200.f, 300.f};
53+
54+
// some operations on the matrices
55+
56+
float maxa = matrix_max(matA);
57+
float maxb = matrix_max(matB);
58+
float maxc = matrix_max(matC);
59+
60+
cout << "max(matA) = " << maxa << endl;
61+
cout << "max(matB) = " << maxb << endl;
62+
cout << "max(matC) = " << maxc << endl;
63+
64+
65+
delete [] matA.pData;
66+
delete [] matB.pData;
67+
delete [] matC.pData;
68+
69+
return 0;
70+
}

week06/examples/inline.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
inline float max_function(float a, float b)
5+
{
6+
if (a > b)
7+
return a;
8+
else
9+
return b;
10+
}
11+
12+
#define MAX_MACRO(a, b) a>b ? a : b
13+
14+
//#define MAX_MACRO(a, b) (a)>(b) ? (a) : (b)
15+
16+
int main()
17+
{
18+
int num1 = 20;
19+
int num2 = 30;
20+
int maxv = max_function(num1, num2);
21+
cout << maxv << endl;
22+
23+
maxv = MAX_MACRO(num1, num2);
24+
cout << maxv << endl;
25+
26+
maxv = MAX_MACRO(num1++, num2++);
27+
cout << maxv << endl;
28+
29+
num1 = 0xAB09;
30+
num2 = 0xEF08;
31+
maxv = MAX_MACRO(num1&0xFF, num2&0xFF);
32+
cout << maxv << endl;
33+
34+
return 0;
35+
}

week06/examples/nofunction.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <float.h>
3+
4+
struct Matrix
5+
{
6+
int rows;
7+
int cols;
8+
float * pData;
9+
};
10+
11+
int main()
12+
{
13+
using namespace std;
14+
15+
Matrix matA = {3,4};
16+
matA.pData = new float[matA.rows * matA.cols]{1.f, 2.f, 3.f};
17+
18+
Matrix matB = {4,8};
19+
matB.pData = new float[matB.rows * matB.cols]{10.f, 20.f, 30.f};
20+
21+
Matrix matC = {4, 2};
22+
matC.pData = new float[matC.rows * matC.cols]{100.f, 200.f, 300.f};
23+
24+
// some operations on the matrices
25+
26+
float maxa = FLT_MIN;
27+
float maxb = FLT_MIN;
28+
float maxc = FLT_MIN;
29+
30+
//find max value of matA
31+
for(int r = 0; r < matA.rows; r++)
32+
for (int c = 0; c < matA.cols; c++)
33+
{
34+
float val = matA.pData[ r * matA.cols + c];
35+
maxa = ( maxa > val ? maxa : val);
36+
}
37+
38+
//find max value of matB
39+
for(int r = 0; r < matB.rows; r++)
40+
for (int c = 0; c < matB.cols; c++)
41+
{
42+
float val = matB.pData[ r * matB.cols + c];
43+
maxb = ( maxb > val ? maxb : val);
44+
}
45+
46+
//find max value of matC
47+
for(int r = 0; r < matC.rows; r++)
48+
for (int c = 0; c < matC.cols; c++)
49+
{
50+
float val = matC.pData[ r * matC.cols + c];
51+
maxc = ( maxc > val ? maxc : val);
52+
}
53+
54+
cout << "max(matA) = " << maxa << endl;
55+
cout << "max(matB) = " << maxb << endl;
56+
cout << "max(matC) = " << maxc << endl;
57+
58+
59+
delete [] matA.pData;
60+
delete [] matB.pData;
61+
delete [] matC.pData;
62+
63+
return 0;
64+
}

week06/examples/param-pointer.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int foo(int * p)
5+
{
6+
(*p) += 10;
7+
return *p;
8+
}
9+
10+
int main()
11+
{
12+
int num1 = 20;
13+
int * p1 = &num1;
14+
int num2 = foo( p1 );
15+
cout << num1 << endl;
16+
cout << num2 << endl;
17+
18+
return 0;
19+
}

week06/examples/param-reference.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include <float.h>
3+
4+
struct Matrix
5+
{
6+
int rows;
7+
int cols;
8+
float * pData;
9+
};
10+
11+
float matrix_max(const struct Matrix & mat)
12+
{
13+
float max = FLT_MIN;
14+
//find max value of mat
15+
for(int r = 0; r < mat.rows; r++)
16+
for (int c = 0; c < mat.cols; c++)
17+
{
18+
float val = mat.pData[ r * mat.cols + c];
19+
max = ( max > val ? max : val);
20+
}
21+
return max;
22+
}
23+
24+
int main()
25+
{
26+
using namespace std;
27+
28+
Matrix matA = {3,4};
29+
matA.pData = new float[matA.rows * matA.cols]{1.f, 2.f, 3.f};
30+
31+
Matrix matB = {4,8};
32+
matB.pData = new float[matB.rows * matB.cols]{10.f, 20.f, 30.f};
33+
34+
Matrix matC = {4, 2};
35+
matC.pData = new float[matC.rows * matC.cols]{100.f, 200.f, 300.f};
36+
37+
// some operations on the matrices
38+
39+
float maxa = matrix_max(matA);
40+
float maxb = matrix_max(matB);
41+
float maxc = matrix_max(matC);
42+
43+
cout << "max(matA) = " << maxa << endl;
44+
cout << "max(matB) = " << maxb << endl;
45+
cout << "max(matC) = " << maxc << endl;
46+
47+
48+
delete [] matA.pData;
49+
delete [] matB.pData;
50+
delete [] matC.pData;
51+
52+
return 0;
53+
}

week06/examples/reference.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int num = 0;
7+
int & num_ref = num;
8+
cout << "num = " << num << endl;
9+
10+
num_ref = 10;
11+
cout << "num = " << num << endl;
12+
13+
return 0;
14+
}

0 commit comments

Comments
 (0)