Start Coding C++ from My Perspective

Introduction

As we all have known, learning to code and try to master it is becoming increasingly essential for contemporary people, while this world is embracing the era of coding.

Studying in SEIEE, studying C++ and deploying with it cannot be more common for students and teachers. As a freshman, I also experience challenges in C++.

My C++ schoolwork is almost about “program design” and “examination required”. Anyway, let’s have a look on our first schoolwork.

Schoolwork #1 - Code from Scratch

Question 1

Design a program. Realize that the user inputs two int values and two double values (the four values are positive), and the program outputs the average of the four numbers (in double), and then round the average to an integer to output. You can refer to the sample below.

1.1 Sample

1
2
3
4
5
6
Input: 12
Input: 4
Input: 0.73
Input: 94.182
Output: 27.728
Output: 28

1.2 Analyses & Solution

It is easy for most programmers. Just use what has been learnt and code according to math theories.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
///
/// @file 01-average-and-rounding.cc
/// @author Lucis Urbe
/// @copyright Shanghai Jiao Tong University, 2019
/// @date December 31, 2023
/// @version 0.2
/// @brief The use of integers, math library and standard I/O.
///

// The first step for C++ programming is definitely library including.
// If you want to use cout, cin to see how the program works, you must write the line below.
#include <iostream>
// With cmath you can do something mathematical, like calculating sine or use of function round().
#include <cmath>

// Uncomment the line below and you can just write std::cout without "std::".
// using namespace std;

int main() {
// The second step is to define variables according to the question.
unsigned int i_1, i_2, i_avg;
double d_1, d_2, d_avg; // There is no unsigned double...

// The third step: tell the user to input the four numbers.
std::cout << "Input: ";
std::cin >> i_1;
std::cout << "Input: ";
std::cin >> i_2;
std::cout << "Input: ";
std::cin >> d_1;
std::cout << "Input: ";
std::cin >> d_2;

// The fourth step is to calculate the average of the four numbers and output it.
d_avg = ((double)i_1 + (double)i_2 + d_1 + d_2) / 4;
std::cout << "Output: " << d_avg << std::endl;

// The fifth step is to round the average to an integer and output it.
i_avg = round(d_avg);
std::cout << "Output: " << i_avg << std::endl;

// Finally, we complete the program with "return 0;". Perfect!
return 0;
}

It is always said that it is the hardest to start a new area from scratch, such as the well-known AI question A+B Problem.

Question 2

Design a program. The user input two letters to have them converted to 2-digit code. For instance, letter A or a stands for 01, and letter B or b stands for 02… letter Z or z stands for 26. The program should output two 2-digit codes together. You can refer to the sample below.

2.1 Sample

1
2
3
Input: C
Input: y
Output: 0325

2.2 Analysis & Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
///
/// @file 02-ascii-integers.cc
/// @author Lucis Urbe
/// @copyright Shanghai Jiao Tong University, 2019
/// @date January 1, 2024
/// @version 0.2
/// @brief The use of integers, ASCII and standard I/O.
///

#include <iostream>

int main() {
// Step 1: define the variables
// We need to get input of the two letters from users, so just define two characters.
char c_1, c_2;

// Step 2: tell the user to input two letters and get them.
std::cout << "Input: ";
std::cin >> c_1; // You may think that if cin.get(c_1); is also OK.
std::cout << "Input: ";
std::cin >> c_2; // You may use std::cin.get(c_2); here, but it actually doesn't receive the right letter inputed. Why?
// That's how std::cin.get() works different from std::cin.
// As for std::cin, it identify the end of user's input by receiving an enter.
// If your input contains space character or tab character, it will be the reference to the end of the input for a variable given.
// Like by the code "int a, b; std::cin >> a >> b;" and when the program runs, you inputed "123 4 26"(separated by a space and then a tab) and press enter.
// We can conclude that a = 123, b = 4 and the rest stays at a portion of computer's memory called buffer.
// But std::cin.get() only gets one character from the user, so any of the character inputed can be accepted, therefore no need for a reference.
// If you run the code "char a, b; std::cin.get(a); std::cin.get(b);" and input "CDEFG", then a = 'C' and b = 'D'.
// But if you input "C defg", then a = 'C', b = ' '. Even, if you just input "C" and press the enter, a = 'C' and b = '\n', an enter.
// So you must have to decide when you need to use std::cin.get(). More commonly, we consider using std::cin first.

// Step 3: According to the rule of ASCII, assuming that all computers running this code will have a continual alphabetical charset.
// We first judge the first character and process it with two ways: uppercase and lowercase.
if (c_1 >= 'a' && c_1 <= 'z')
c_1 = c_1 - 'a' + 1;
// Why do this thing?
// Because the interior code number (ASCII code) for letters don't start from 0, but some another number.
// We have the character minus by the first character, 'a', then we get the difference between the character and 'a'.
// We plus 1, so the code for 'a' would be 1, not 0.
// We do the similar things below.
else if (c_1 >= 'A' && c_1 <= 'Z')
c_1 = c_1 - 'A' + 1;

if (c_2 >= 'a' && c_2 <= 'z')
c_2 = c_2 - 'a' + 1;
else if (c_2 >= 'A' && c_2 <= 'Z')
c_2 = c_2 - 'A' + 1;

// Step 4: output the final numbers to the user.
// Because the number must be 2-digit, we need to judge if c_1 and c_2 is smaller than 10.
// If so, we need to voluntarily add a '0' ahead.
if (c_1 < 10) {
if (c_2 < 10)
std::cout << "Output: 0" << (int)c_1 << '0' << (int)c_2 << std::endl;
else
std::cout << "Output: 0" << (int)c_1 << (int)c_2 << std::endl;
}
else if (c_2 < 10)
std::cout << "Output: " << (int)c_1 << '0' << (int)c_2 << std::endl;
else
std::cout << "Output: " << (int)c_1 << (int)c_2 << std::endl;

return 0;
}

This question is aimed at examining if you have mastered the usage of if and else, with the consideration of getting to know ASCII.

Question 3

Design a program. Input the rectangular coordinates of four points and decide if they can be concyclic (on the same circle). You can refer to the sample and follow the instructions below.

3.1 Sample

1
2
3
4
5
Input: 0 1
Input: 1 0
Input: 0 -1
Input: -1 0
Output: Yes

3.2 Instructions

  • We assert that the input from the user is correct, and can only be integral numbers. You can directly define int variables. Do not define float or double for user’s input, because it will cause mathematical inaccuracy.

  • Note to avoid the inaccuracy caused by division of integers and extraction of a root.

  • You can refer to a math theory: If we have (cosCAD)2=(cosCBD)2\left(\cos\angle CAD\right)^2 = \left(\cos\angle CBD\right)^2, and if point AA and point BB are on the same side of line segment CDCD, CAD\angle CAD and ACD\angle ACD should be both obtuse or both acute; if on the different sides, one of them should be obtuse and the other should be acute. All conditions above satisfying, we can conclude that points A,B,C,DA, B, C, D are concyclic.

  • Refer to the law of cosines.

3.3 Analysis & Solution

If you deploy the theory above, you must consider if any line segment is vertical or horizontal. I am acquiring a more stupid way to work out this question. Just take a look and never use such way anymore.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
///
/// @file 03-four-cocyclic-points.cc
/// @author Lucis Urbe
/// @copyright Shanghai Jiao Tong University, 2019
/// @date January 2, 2024
/// @version 0.2
/// @brief The use of math theorem, permutation and logic.
///

#include <iostream>
#include <cmath>

using namespace std;

int main() {
// define the variables (for four points and lengths of line segments)
int i_x1, i_y1;
int i_x2, i_y2;
int i_x3, i_y3;
int i_x4, i_y4;
double k[6];

// instruct to input four coordinations
std::cout << "Input: ";
std::cin >> i_x1 >> i_y1;
std::cout << "Input: ";
std::cin >> i_x2 >> i_y2;
std::cout << "Input: ";
std::cin >> i_x3 >> i_y3;
std::cout << "Input: ";
std::cin >> i_x4 >> i_y4;

// calculate the lengths of line segments
k[0] = sqrt(double((i_x1 - i_x2) * (i_x1 - i_x2) + (i_y1 - i_y2) * (i_y1 - i_y2)));
k[1] = sqrt(double((i_x1 - i_x3) * (i_x1 - i_x3) + (i_y1 - i_y3) * (i_y1 - i_y3)));
k[2] = sqrt(double((i_x1 - i_x4) * (i_x1 - i_x4) + (i_y1 - i_y4) * (i_y1 - i_y4)));
k[3] = sqrt(double((i_x2 - i_x3) * (i_x2 - i_x3) + (i_y2 - i_y3) * (i_y2 - i_y3)));
k[4] = sqrt(double((i_x2 - i_x4) * (i_x2 - i_x4) + (i_y2 - i_y4) * (i_y2 - i_y4)));
k[5] = sqrt(double((i_x3 - i_x4) * (i_x3 - i_x4) + (i_y3 - i_y4) * (i_y3 - i_y4)));

// use Ptolemy's theorem and repeatedly exchange the sequence of the parameters of the lengths until the result works out.
bool success_flag = false;
for (int a = 0; a < 6; a++) {
for (int b = 0; b < 6; b++) {
if (b != a)
for (int c = 0; c < 6; c++) {
if (c != b && c != a)
for (int d = 0; d < 6; d++) {
if (d != c && d != b && d != a)
for (int e = 0; e < 6; e++) {
if (e != d && e != c && e != b && e != a)
for (int f = 0; f < 6; f++)
if (f != e && f != d && f != c && f != b && f != a)
if (fabs(k[a] * k[b] - k[c] * k[d] - k[e] * k[f]) <= 1.0E-10) {
std::cout << "Output: Yes" << std::endl;
success_flag = true;
break;
}
if (success_flag)
break;
}
if (success_flag)
break;
}
if (success_flag)
break;
}
if (success_flag)
break;
}
if (success_flag)
break;
}

if (!success_flag)
std::cout << "Output: No" << std::endl;
return 0;
}

Start Coding C++ from My Perspective
https://lucisurbe.pages.dev/2020/01/09/Start-Coding-C-from-My-Perspective/
Author
Lucis Urbe
Posted on
January 9, 2020
Licensed under