Skip to content

Commit ec425af

Browse files
committed
update chapter 04
1 parent de0f7a2 commit ec425af

10 files changed

+176
-0
lines changed

week02/Lecture02.pptx

-12 Bytes
Binary file not shown.

week04/Lecture04.pptx

77.7 KB
Binary file not shown.

week04/examples/array.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
using namespace std;
3+
int main()
4+
{
5+
int num_array1[5]; //uninitialized array, random values
6+
int num_array2[5] = {0, 1, 2, 3, 4}; //initialization
7+
8+
for(int idx = 0; idx < 5; idx++)
9+
cout << num_array1[idx] << " ";
10+
cout << endl;
11+
12+
for(int idx = 0; idx < 5; idx++)
13+
cout << num_array2[idx] << " ";
14+
cout << endl;
15+
}

week04/examples/const-array.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
//float array_sum(const float *values, size_t length)
5+
//float array_sum(const float values[4], size_t length)
6+
float array_sum(const float values[], size_t length)
7+
{
8+
float sum = 0.0f;
9+
for (int i = 0; i < length; i++)
10+
{
11+
sum += values[i];
12+
//values[i] = 0; //error
13+
}
14+
return sum;
15+
}
16+
17+
int main()
18+
{
19+
// const float PI = 3.1415926f;
20+
// PI += 1.f; // error
21+
// const float values[4] = {1.1f, 2.2f, 3.3f, 4.4f};
22+
// values[0] = 1.0f; // error
23+
24+
float values[4] = {1.1f, 2.2f, 3.3f, 4.4f};
25+
float sum = array_sum(values, 4);
26+
27+
cout << "sum = " << sum << endl;
28+
return 0;
29+
}

week04/examples/index-bound.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
using namespace std;
3+
int main()
4+
{
5+
int num_array[5];
6+
7+
for(int idx = -1; idx <= 5; idx++) //out of bounds
8+
num_array[idx] = idx * idx;
9+
10+
for(int idx = -1; idx <= 5; idx++) //out of bounds
11+
cout << "num_array[" << idx << "] = " << num_array[idx] << endl;
12+
13+
return 0;
14+
}

week04/examples/initchar.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
#include <cstring>
3+
using namespace std;
4+
5+
int main()
6+
{
7+
char rabbit[16] = {'P', 'e', 't', 'e', 'r'};
8+
cout << "String length is " << strlen(rabbit) << endl;
9+
for(int i = 0; i < 16; i++)
10+
cout << i << ":" << +rabbit[i] << "(" << rabbit[i] << ")" << endl;
11+
12+
char bad_pig[9] = {'P', 'e', 'p', 'p', 'a', ' ', 'P', 'i', 'g'};
13+
char good_pig[10] = {'P', 'e', 'p', 'p', 'a', ' ', 'P', 'i', 'g', '\0'};
14+
15+
cout << "Rabbit is (" << rabbit << ")" << endl;
16+
cout << "Pig's bad name is (" << bad_pig << ")" << endl;
17+
cout << "Pig's good name is (" << good_pig << ")" << endl;
18+
19+
char name[10] = {'Y', 'u', '\0', 'S', '.', '0'};
20+
cout << strlen(name) << endl;
21+
22+
return 0;
23+
}

week04/examples/md-array.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// You must tell the function the bound of an array,
5+
// otherwise, elements cannot be accessed
6+
// if the array is a variable-length one, it may be difficult to know the bound
7+
void init_2d_array(float mat[][4], //error, arrays of unknown bound
8+
size_t rows, size_t cols)
9+
{
10+
for (int r = 0; r < rows; r++)
11+
for(int c = 0; c < cols; c++)
12+
mat[r][c] = r * c;
13+
}
14+
15+
int main()
16+
{
17+
int mat1[2][3] = {{11,12,13}, {14,15,16}};
18+
19+
int rows = 5;
20+
int cols = 4;
21+
//float mat2[rows][cols]; //uninitialized array
22+
float mat2[rows][4]; //uninitialized array
23+
24+
//init_2d_array(mat2, rows, cols);
25+
26+
for (int r = 0; r < rows; r++)
27+
for(int c = 0; c < cols; c++)
28+
mat2[r][c] = r * c;
29+
30+
31+
for (int r = 0; r < rows; r++)
32+
{
33+
for(int c = 0; c < cols; c++)
34+
cout << mat2[r][c] << " ";
35+
cout << endl;
36+
}
37+
return 0;
38+
}

week04/examples/stdstring.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
int main()
6+
{
7+
std::string str1 = "Hello";
8+
std::string str2 = "SUSTech";
9+
std::string result = str1 + ", " + str2;
10+
11+
cout << "result = " + result << endl;
12+
13+
cout << "The length is " << result.length() << endl;
14+
15+
cout << "str1 < str2 is " << (str1 < str2) << endl;
16+
17+
return 0;
18+
}

week04/examples/stringop.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
#include <cstring>
3+
using namespace std;
4+
5+
int main()
6+
{
7+
char str1[] = "Hello, \0CPP";
8+
char str2[] = "SUSTech";
9+
char result[128];
10+
11+
for(int i = 0; i < 16; i++)
12+
cout << i << ":" << +str1[i] << "(" << str1[i] << ")" << endl;
13+
14+
strcpy(result, str1);
15+
cout << "Result = " << result << endl;
16+
strcat(result, str2);
17+
cout << "Result = " << result << endl;
18+
19+
cout << "strcmp() = " << strcmp(str1, str2) << endl;
20+
21+
//strcat(str1, str2); //danger operation!
22+
//cout << "str1 = " << str1 << endl;
23+
}

week04/examples/variable-array.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
using namespace std;
3+
int main()
4+
{
5+
int num_array1[5] = {0,1}; // fixed length array, initialized to {0,1,0,0,0}
6+
cout << "sizeof(num_array1[5]) = " << sizeof(num_array1) << endl;
7+
8+
int len = 1;
9+
while ( len < 10 )
10+
{
11+
int num_array2[len]; //variable-length array
12+
cout << "len = " << len;
13+
cout << ", sizeof(num_array2)) = " << sizeof(num_array2) << endl;
14+
len ++;
15+
}
16+
}

0 commit comments

Comments
 (0)