-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
205 lines (192 loc) · 118 KB
/
search.xml
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>Array Manipulation and Traversal</title>
<url>/2023/11/27/Array-Manipulation-and-Traversal/</url>
<content><![CDATA[<h4 id="Array-Manipulation-and-Traversal"><a href="#Array-Manipulation-and-Traversal" class="headerlink" title="Array Manipulation and Traversal:"></a>Array Manipulation and Traversal:</h4><p>a. Calculate the average of the scores array.<br>b. Convert all characters in the name to uppercase.<br>c. Find the highest temperature in the temperatures array.<br>d. Print the days of the week in reverse order.<br>e. Transpose the matrix (swap rows and columns).</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><cstring></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">// a. Calculate the average of the scores array.</span></span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> numScores = <span class="number">5</span>;</span><br><span class="line"> <span class="type">double</span> scores[numScores] = {<span class="number">90.5</span>, <span class="number">85.0</span>, <span class="number">92.5</span>, <span class="number">88.0</span>, <span class="number">95.5</span>};</span><br><span class="line"> <span class="type">double</span> sum = <span class="number">0.0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < numScores; ++i) {</span><br><span class="line"> sum += scores[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="type">double</span> average = sum / numScores;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// b. Convert all characters in the name to uppercase.</span></span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> nameSize = <span class="number">20</span>;</span><br><span class="line"> <span class="type">char</span> name[nameSize];</span><br><span class="line"> std::cout << <span class="string">"Enter your name: "</span>;</span><br><span class="line"> std::cin.<span class="built_in">getline</span>(name, nameSize);</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < <span class="built_in">strlen</span>(name); ++i) {</span><br><span class="line"> name[i] = <span class="built_in">toupper</span>(name[i]);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// c. Find the highest temperature in the temperatures array.</span></span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> numTemperatures = <span class="number">7</span>;</span><br><span class="line"> <span class="type">int</span> temperatures[numTemperatures];</span><br><span class="line"> std::cout << <span class="string">"Enter temperatures for the week:\n"</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < numTemperatures; ++i) {</span><br><span class="line"> std::cout << <span class="string">"Day "</span> << (i + <span class="number">1</span>) << <span class="string">": "</span>;</span><br><span class="line"> std::cin >> temperatures[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="type">int</span> maxTemperature = temperatures[<span class="number">0</span>];</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i < numTemperatures; ++i) {</span><br><span class="line"> <span class="keyword">if</span> (temperatures[i] > maxTemperature) {</span><br><span class="line"> maxTemperature = temperatures[i];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Clear the input buffer</span></span><br><span class="line"> std::cin.<span class="built_in">ignore</span>(std::numeric_limits<std::streamsize>::<span class="built_in">max</span>(), <span class="string">'\n'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// d. Print the days of the week in reverse order.</span></span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> numDays = <span class="number">7</span>;</span><br><span class="line"> std::string daysOfWeek[numDays] = {<span class="string">"Sunday"</span>, <span class="string">"Monday"</span>, <span class="string">"Tuesday"</span>, <span class="string">"Wednesday"</span>, <span class="string">"Thursday"</span>, <span class="string">"Friday"</span>, <span class="string">"Saturday"</span>};</span><br><span class="line"> std::cout << <span class="string">"Days of the week in reverse order:\n"</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = numDays - <span class="number">1</span>; i >= <span class="number">0</span>; --i) {</span><br><span class="line"> std::cout << daysOfWeek[i] << <span class="string">" "</span>;</span><br><span class="line"> }</span><br><span class="line"> std::cout << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// e. Transpose the matrix (swap rows and columns).</span></span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> numRows = <span class="number">3</span>;</span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> numCols = <span class="number">3</span>;</span><br><span class="line"> <span class="type">int</span> matrix[numRows][numCols];</span><br><span class="line"></span><br><span class="line"> std::cout << <span class="string">"Enter values for the 3x3 matrix:\n"</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < numRows; ++i) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j < numCols; ++j) {</span><br><span class="line"> std::cout << <span class="string">"Matrix["</span> << i << <span class="string">"]["</span> << j << <span class="string">"]: "</span>;</span><br><span class="line"> std::cin >> matrix[i][j];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Transpose the matrix</span></span><br><span class="line"> <span class="type">int</span> transposedMatrix[numCols][numRows];</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < numRows; ++i) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j < numCols; ++j) {</span><br><span class="line"> transposedMatrix[j][i] = matrix[i][j];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Output information</span></span><br><span class="line"> std::cout << <span class="string">"\nAverage of Scores: "</span> << average << std::fixed << std::<span class="built_in">setprecision</span>(<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line"> std::cout << <span class="string">"\nUppercase Name: "</span> << name;</span><br><span class="line"></span><br><span class="line"> std::cout << <span class="string">"\nHighest Temperature: "</span> << maxTemperature;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>C++ combined list</title>
<url>/2023/11/26/C-combined-list/</url>
<content><![CDATA[<h4 id="Task-4-Combining-Data-Structures"><a href="#Task-4-Combining-Data-Structures" class="headerlink" title="Task 4: Combining Data Structures"></a>Task 4: Combining Data Structures</h4><ol>
<li>Create a stack of strings named <strong>stringStack</strong>.</li>
<li>Push at least three strings onto the stack.</li>
<li>Create a queue of floats named <strong>floatQueue</strong>.</li>
<li>Enqueue at least two float values into the queue.</li>
<li>Create a linked list of characters named <strong>charList</strong>.</li>
<li>Insert at least four characters into the linked list.</li>
<li>Combine the elements of the stack, queue, and linked list into a new data structure (e.g., another linked list, a stack, or a queue).</li>
<li>Print the elements of the new data structure.</li>
</ol>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stack></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><list></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">// 1. Create a stack of strings named stringStack.</span></span><br><span class="line"> std::stack<std::string> stringStack;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 2. Push at least three strings onto the stack.</span></span><br><span class="line"> stringStack.<span class="built_in">push</span>(<span class="string">"Apple"</span>);</span><br><span class="line"> stringStack.<span class="built_in">push</span>(<span class="string">"Banana"</span>);</span><br><span class="line"> stringStack.<span class="built_in">push</span>(<span class="string">"Orange"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 3. Create a queue of floats named floatQueue.</span></span><br><span class="line"> std::queue<<span class="type">float</span>> floatQueue;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 4. Enqueue at least two float values into the queue.</span></span><br><span class="line"> floatQueue.<span class="built_in">push</span>(<span class="number">3.14f</span>);</span><br><span class="line"> floatQueue.<span class="built_in">push</span>(<span class="number">2.718f</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 5. Create a linked list of characters named charList.</span></span><br><span class="line"> std::list<<span class="type">char</span>> charList;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 6. Insert at least four characters into the linked list.</span></span><br><span class="line"> charList.<span class="built_in">push_back</span>(<span class="string">'A'</span>);</span><br><span class="line"> charList.<span class="built_in">push_back</span>(<span class="string">'B'</span>);</span><br><span class="line"> charList.<span class="built_in">push_back</span>(<span class="string">'C'</span>);</span><br><span class="line"> charList.<span class="built_in">push_back</span>(<span class="string">'D'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 7. Combine the elements of the stack, queue, and linked list into a new data structure.</span></span><br><span class="line"> std::list<std::string> combinedList;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Combine elements from the stack</span></span><br><span class="line"> <span class="keyword">while</span> (!stringStack.<span class="built_in">empty</span>()) {</span><br><span class="line"> combinedList.<span class="built_in">push_back</span>(stringStack.<span class="built_in">top</span>());</span><br><span class="line"> stringStack.<span class="built_in">pop</span>();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Combine elements from the queue</span></span><br><span class="line"> <span class="keyword">while</span> (!floatQueue.<span class="built_in">empty</span>()) {</span><br><span class="line"> combinedList.<span class="built_in">push_back</span>(std::<span class="built_in">to_string</span>(floatQueue.<span class="built_in">front</span>()));</span><br><span class="line"> floatQueue.<span class="built_in">pop</span>();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Combine elements from the linked list</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">char</span> ch : charList) {</span><br><span class="line"> combinedList.<span class="built_in">push_back</span>(std::<span class="built_in">string</span>(<span class="number">1</span>, ch));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 8. Print the elements of the new data structure.</span></span><br><span class="line"> std::cout << <span class="string">"Combined List: "</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">const</span> std::string& element : combinedList) {</span><br><span class="line"> std::cout << element << <span class="string">" "</span>;</span><br><span class="line"> }</span><br><span class="line"> std::cout << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>C++ create,duplicate queue</title>
<url>/2023/11/25/C-create-duplicate-queue/</url>
<content><![CDATA[<h4 id="Task-2-Queue-Operations"><a href="#Task-2-Queue-Operations" class="headerlink" title="Task 2: Queue Operations"></a>Task 2: Queue Operations</h4><ol>
<li>Create a queue of characters named <strong>charQueue</strong>.</li>
<li>Enqueue at least four characters into the queue.</li>
<li>Print the elements of the queue.</li>
<li>Dequeue one element and print the updated queue.</li>
<li>Check if the queue is empty.</li>
<li>Implement a function to duplicate the elements in the queue.</li>
<li>Print the duplicated queue.</li>
</ol>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><queue></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Function to print queue elements</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">printQueue</span><span class="params">(<span class="type">const</span> std::queue<<span class="type">char</span>>& q)</span> </span>{</span><br><span class="line"> std::cout << <span class="string">"Queue elements: "</span>;</span><br><span class="line"> <span class="comment">// Since queue doesn't provide direct access to elements,</span></span><br><span class="line"> <span class="comment">// we need to use a copy for printing without modifying the original queue</span></span><br><span class="line"> std::queue<<span class="type">char</span>> temp = q;</span><br><span class="line"> <span class="keyword">while</span> (!temp.<span class="built_in">empty</span>()) {</span><br><span class="line"> std::cout << temp.<span class="built_in">front</span>() << <span class="string">" "</span>;</span><br><span class="line"> temp.<span class="built_in">pop</span>();</span><br><span class="line"> }</span><br><span class="line"> std::cout << std::endl;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// Function to duplicate the elements in the queue</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">duplicateQueue</span><span class="params">(std::queue<<span class="type">char</span>>& q)</span> </span>{</span><br><span class="line"> std::queue<<span class="type">char</span>> temp;</span><br><span class="line"> <span class="keyword">while</span> (!q.<span class="built_in">empty</span>()) {</span><br><span class="line"> <span class="type">char</span> frontElement = q.<span class="built_in">front</span>();</span><br><span class="line"> temp.<span class="built_in">push</span>(frontElement);</span><br><span class="line"> temp.<span class="built_in">push</span>(frontElement);</span><br><span class="line"> q.<span class="built_in">pop</span>();</span><br><span class="line"> }</span><br><span class="line"> q = temp; <span class="comment">// Assign the duplicated queue back to the original queue</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">// 1. Create a queue of characters named charQueue.</span></span><br><span class="line"> std::queue<<span class="type">char</span>> charQueue;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 2. Enqueue at least four characters into the queue.</span></span><br><span class="line"> charQueue.<span class="built_in">push</span>(<span class="string">'A'</span>);</span><br><span class="line"> charQueue.<span class="built_in">push</span>(<span class="string">'B'</span>);</span><br><span class="line"> charQueue.<span class="built_in">push</span>(<span class="string">'C'</span>);</span><br><span class="line"> charQueue.<span class="built_in">push</span>(<span class="string">'D'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 3. Print the elements of the queue.</span></span><br><span class="line"> <span class="built_in">printQueue</span>(charQueue);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 4. Dequeue one element and print the updated queue.</span></span><br><span class="line"> charQueue.<span class="built_in">pop</span>();</span><br><span class="line"> <span class="built_in">printQueue</span>(charQueue);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 5. Check if the queue is empty.</span></span><br><span class="line"> <span class="keyword">if</span> (charQueue.<span class="built_in">empty</span>()) {</span><br><span class="line"> std::cout << <span class="string">"Queue is empty."</span> << std::endl;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> std::cout << <span class="string">"Queue is not empty."</span> << std::endl;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 6. Implement a function to duplicate the elements in the queue.</span></span><br><span class="line"> <span class="built_in">duplicateQueue</span>(charQueue);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 7. Print the duplicated queue.</span></span><br><span class="line"> <span class="built_in">printQueue</span>(charQueue);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>C++ create, print,reverse stack</title>
<url>/2023/11/25/C-create-print-reverse-stack/</url>
<content><![CDATA[<h4 id="TASK-1-Stack-Operations"><a href="#TASK-1-Stack-Operations" class="headerlink" title="TASK 1 Stack Operations"></a>TASK 1 Stack Operations</h4><ol>
<li>Create a stack of integers named <strong>intStack</strong>.</li>
<li>Push at least five integers onto the stack.</li>
<li>Print the elements of the stack.</li>
<li>Pop two elements from the stack and print the updated stack. </li>
<li>Check if the stack is empty.</li>
<li>Implement a function to reverse the elements in the stack. </li>
<li>Print the reversed stack.</li>
</ol>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stack></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Function to print stack elements</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">printStack</span><span class="params">(<span class="type">const</span> stack<<span class="type">int</span>>& s)</span></span>{</span><br><span class="line"> std::cout << <span class="string">"Stack elements: "</span>;</span><br><span class="line"> std::stack<<span class="type">int</span>> temp = s;</span><br><span class="line"> <span class="keyword">while</span> (!temp.<span class="built_in">empty</span>())</span><br><span class="line"> {</span><br><span class="line"> std::cout << temp.<span class="built_in">top</span>() << <span class="string">" "</span>;</span><br><span class="line"> temp.<span class="built_in">pop</span>();</span><br><span class="line"> <span class="comment">/* code */</span></span><br><span class="line"> }</span><br><span class="line"> std::cout << std::endl;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">reverseStack</span><span class="params">(stack<<span class="type">int</span>>& s)</span></span>{</span><br><span class="line"> std::stack<<span class="type">int</span>> temp;</span><br><span class="line"> <span class="keyword">while</span> (!s.<span class="built_in">empty</span>())</span><br><span class="line"> {</span><br><span class="line"> temp.<span class="built_in">push</span>(s.<span class="built_in">top</span>());</span><br><span class="line"> s.<span class="built_in">pop</span>();</span><br><span class="line"> <span class="comment">/* code */</span></span><br><span class="line"> }</span><br><span class="line"> s = temp;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> <span class="type">const</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="comment">// create a stack of integers named intStack.</span></span><br><span class="line"> std::stack<<span class="type">int</span>> intStack;</span><br><span class="line"> <span class="comment">// push at least five integers onto the stack.</span></span><br><span class="line"> intStack.<span class="built_in">push</span>(<span class="number">10</span>);</span><br><span class="line"> intStack.<span class="built_in">push</span>(<span class="number">20</span>);</span><br><span class="line"> intStack.<span class="built_in">push</span>(<span class="number">30</span>);</span><br><span class="line"> intStack.<span class="built_in">push</span>(<span class="number">40</span>);</span><br><span class="line"> intStack.<span class="built_in">push</span>(<span class="number">50</span>);</span><br><span class="line"></span><br><span class="line"> <span class="built_in">printStack</span>(intStack);</span><br><span class="line"> <span class="comment">// pop two elements from the stack and print the updated stack.</span></span><br><span class="line"></span><br><span class="line"> intStack.<span class="built_in">pop</span>();</span><br><span class="line"> intStack.<span class="built_in">pop</span>();</span><br><span class="line"> <span class="built_in">printStack</span>(intStack);</span><br><span class="line"> <span class="comment">// check if the stack is empty.</span></span><br><span class="line"> <span class="keyword">if</span>(intStack.<span class="built_in">empty</span>()){</span><br><span class="line"> std::cout << <span class="string">"stack is empty"</span> << std::endl;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> std::cout << <span class="string">"stack is not empty"</span> << std::endl;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// implement a function to reverse the elemnts in ths stack.</span></span><br><span class="line"> <span class="built_in">reverseStack</span>(intStack);</span><br><span class="line"> <span class="comment">// print the reversed stack.</span></span><br><span class="line"> <span class="built_in">printStack</span>(intStack);</span><br><span class="line"></span><br><span class="line"> <span class="comment">/* code */</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>C++ singly linked list</title>
<url>/2023/11/25/C-singly-linked-list/</url>
<content><![CDATA[<ol>
<li>Define a structure for a singly linked list node with an integer value.</li>
<li>Create a linked list named <strong>intList</strong> with at least five nodes.</li>
<li>Print the elements of the linked list.</li>
<li>Implement a function to insert a new node at the end of the linked list.</li>
<li>Print the updated linked list.</li>
<li>Implement a function to delete a node with a specific value from the linked list. </li>
<li>Print the updated linked list.</li>
</ol>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 1. Define a structure for a singly linked list node with an integer value.</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">ListNode</span> {</span><br><span class="line"> <span class="type">int</span> data;</span><br><span class="line"> ListNode* next;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Constructor to initialize the node</span></span><br><span class="line"> <span class="built_in">ListNode</span>(<span class="type">int</span> value) : <span class="built_in">data</span>(value), <span class="built_in">next</span>(<span class="literal">nullptr</span>) {}</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="comment">// Function to print the elements of the linked list.</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">printList</span><span class="params">(ListNode* head)</span> </span>{</span><br><span class="line"> std::cout << <span class="string">"Linked List: "</span>;</span><br><span class="line"> <span class="keyword">while</span> (head != <span class="literal">nullptr</span>) {</span><br><span class="line"> std::cout << head->data << <span class="string">" "</span>;</span><br><span class="line"> head = head->next;</span><br><span class="line"> }</span><br><span class="line"> std::cout << std::endl;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 4. Implement a function to insert a new node at the end of the linked list.</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">insertAtEnd</span><span class="params">(ListNode*& head, <span class="type">int</span> value)</span> </span>{</span><br><span class="line"> ListNode* newNode = <span class="keyword">new</span> ListNode;</span><br><span class="line"> newNode->data = value;</span><br><span class="line"> std::cout << <span class="string">"head: "</span> << head << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (head == <span class="literal">nullptr</span>) {</span><br><span class="line"> <span class="comment">// If the list is empty, the new node becomes the head.</span></span><br><span class="line"> head = newNode;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">// Traverse the list to find the last node and append the new node.</span></span><br><span class="line"> ListNode* temp = head;</span><br><span class="line"> std::cout << <span class="string">"temp->next: "</span> <<temp->next << std::endl;</span><br><span class="line"> <span class="keyword">while</span> (temp->next != <span class="literal">nullptr</span>) {</span><br><span class="line"> temp = temp->next;</span><br><span class="line"> }</span><br><span class="line"> temp->next = newNode;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 6. Implement a function to delete a node with a specific value from the linked list.</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">deleteNode</span><span class="params">(ListNode*& head, <span class="type">int</span> value)</span> </span>{</span><br><span class="line"> <span class="comment">// Handle the case where the node to be deleted is the head.</span></span><br><span class="line"> <span class="keyword">if</span> (head != <span class="literal">nullptr</span> && head->data == value) {</span><br><span class="line"> ListNode* temp = head;</span><br><span class="line"> head = head->next;</span><br><span class="line"> <span class="keyword">delete</span> temp;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Traverse the list to find the node with the specified value.</span></span><br><span class="line"> ListNode* current = head;</span><br><span class="line"> ListNode* prev = <span class="literal">nullptr</span>;</span><br><span class="line"> <span class="keyword">while</span> (current != <span class="literal">nullptr</span> && current->data != value) {</span><br><span class="line"> prev = current;</span><br><span class="line"> current = current->next;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// If the node is found, unlink it from the list and delete it.</span></span><br><span class="line"> <span class="keyword">if</span> (current != <span class="literal">nullptr</span>) {</span><br><span class="line"> prev->next = current->next;</span><br><span class="line"> <span class="keyword">delete</span> current;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">// 2. Create a linked list named intList with at least five nodes.</span></span><br><span class="line"> ListNode* intList = <span class="literal">nullptr</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i <= <span class="number">5</span>; ++i) {</span><br><span class="line"> <span class="built_in">insertAtEnd</span>(intList, i * <span class="number">10</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 3. Print the elements of the linked list.</span></span><br><span class="line"> <span class="built_in">printList</span>(intList);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 5. Insert a new node at the end of the linked list.</span></span><br><span class="line"> <span class="built_in">insertAtEnd</span>(intList, <span class="number">60</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 7. Delete a node with a specific value from the linked list.</span></span><br><span class="line"> <span class="built_in">deleteNode</span>(intList, <span class="number">30</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Print the updated linked list.</span></span><br><span class="line"> <span class="built_in">printList</span>(intList);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>Declaring and Accessing Arrays</title>
<url>/2023/11/27/Declaring-and-Accessing-Arrays/</url>
<content><![CDATA[<h4 id="Declaring-and-Accessing-Arrays"><a href="#Declaring-and-Accessing-Arrays" class="headerlink" title="Declaring and Accessing Arrays:"></a>Declaring and Accessing Arrays:</h4><p>a. Initialize the scores array with some sample test scores.<br>b. Input the name into the name array using cin.<br>c. Set values in the temperatures array using a loop.<br>d. Initialize the daysOfWeek array with the names of the days.<br>e. Assign values to the elements of the matrix using nested loops.</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><string></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">// a. Initialize the scores array with some sample test scores.</span></span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> numScores = <span class="number">5</span>;</span><br><span class="line"> <span class="type">double</span> scores[numScores] = {<span class="number">90.5</span>, <span class="number">85.0</span>, <span class="number">92.5</span>, <span class="number">88.0</span>, <span class="number">95.5</span>};</span><br><span class="line"></span><br><span class="line"> <span class="comment">// b. Input the name into the name array using cin.</span></span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> nameSize = <span class="number">20</span>;</span><br><span class="line"> <span class="type">char</span> name[nameSize];</span><br><span class="line"> std::cout << <span class="string">"Enter your name: "</span>;</span><br><span class="line"> std::cin.<span class="built_in">getline</span>(name, nameSize);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// c. Set values in the temperatures array using a loop.</span></span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> numTemperatures = <span class="number">7</span>;</span><br><span class="line"> <span class="type">int</span> temperatures[numTemperatures];</span><br><span class="line"> std::cout << <span class="string">"Enter temperatures for the week:\n"</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < numTemperatures; ++i) {</span><br><span class="line"> std::cout << <span class="string">"Day "</span> << (i + <span class="number">1</span>) << <span class="string">": "</span>;</span><br><span class="line"> std::cin >> temperatures[i];</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Clear the input buffer</span></span><br><span class="line"> std::cin.<span class="built_in">ignore</span>(std::numeric_limits<std::streamsize>::<span class="built_in">max</span>(), <span class="string">'\n'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// d. Initialize the daysOfWeek array with the names of the days.</span></span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> numDays = <span class="number">7</span>;</span><br><span class="line"> std::string daysOfWeek[numDays] = {<span class="string">"Sunday"</span>, <span class="string">"Monday"</span>, <span class="string">"Tuesday"</span>, <span class="string">"Wednesday"</span>, <span class="string">"Thursday"</span>, <span class="string">"Friday"</span>, <span class="string">"Saturday"</span>};</span><br><span class="line"></span><br><span class="line"> <span class="comment">// e. Assign values to the elements of the matrix using nested loops.</span></span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> numRows = <span class="number">3</span>;</span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> numCols = <span class="number">3</span>;</span><br><span class="line"> <span class="type">int</span> matrix[numRows][numCols];</span><br><span class="line"></span><br><span class="line"> std::cout << <span class="string">"Enter values for the 3x3 matrix:\n"</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < numRows; ++i) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j < numCols; ++j) {</span><br><span class="line"> std::cout << <span class="string">"Matrix["</span> << i << <span class="string">"]["</span> << j << <span class="string">"]: "</span>;</span><br><span class="line"> std::cin >> matrix[i][j];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Output information</span></span><br><span class="line"> std::cout << <span class="string">"\nScores: "</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < numScores; ++i) {</span><br><span class="line"> std::cout << scores[i] << <span class="string">" "</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> std::cout << <span class="string">"\nName: "</span> << name;</span><br><span class="line"></span><br><span class="line"> std::cout << <span class="string">"\nTemperatures: "</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < numTemperatures; ++i) {</span><br><span class="line"> std::cout << temperatures[i] << <span class="string">" "</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> std::cout << <span class="string">"\nDays of the Week: "</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < numDays; ++i) {</span><br><span class="line"> std::cout << daysOfWeek[i] << <span class="string">" "</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> std::cout << <span class="string">"\nMatrix:\n"</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < numRows; ++i) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j < numCols; ++j) {</span><br><span class="line"> std::cout << matrix[i][j] << <span class="string">" "</span>;</span><br><span class="line"> }</span><br><span class="line"> std::cout << std::endl;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>Hello World</title>
<url>/2023/11/03/Hello%20World/</url>
<content><![CDATA[<p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>
]]></content>
<categories>
<category>other</category>
</categories>
<tags>
<tag>Hexo</tag>
</tags>
</entry>
<entry>
<title>Intro Pointers and Their Role:</title>
<url>/2023/11/27/Intro-Pointers-and-Their-Role/</url>
<content><![CDATA[<h4 id="Introduction-to-Pointers-and-Their-Role"><a href="#Introduction-to-Pointers-and-Their-Role" class="headerlink" title="Introduction to Pointers and Their Role:"></a>Introduction to Pointers and Their Role:</h4><p>a. Declare a pointer variable to an integer named ptr.<br>b. Assign the address of an integer variable to the pointer ptr.<br>c. Use the pointer to modify the value of the integer variable.<br>d. Create a pointer to an array and access its elements.<br>e. Write a function that takes a pointer to an integer and doubles its value</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// e. Write a function that takes a pointer to an integer and doubles its value.</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">doubleValue</span><span class="params">(<span class="type">int</span>* ptr)</span> </span>{</span><br><span class="line"> *ptr *= <span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">// a. Declare a pointer variable to an integer named ptr.</span></span><br><span class="line"> <span class="type">int</span>* ptr;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// b. Assign the address of an integer variable to the pointer ptr.</span></span><br><span class="line"> <span class="type">int</span> number = <span class="number">42</span>;</span><br><span class="line"> ptr = &number;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// c. Use the pointer to modify the value of the integer variable.</span></span><br><span class="line"> std::cout << <span class="string">"Original value: "</span> << number << std::endl;</span><br><span class="line"> *ptr = <span class="number">99</span>;</span><br><span class="line"> std::cout << <span class="string">"Modified value using pointer: "</span> << number << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// d. Create a pointer to an array and access its elements.</span></span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> arraySize = <span class="number">5</span>;</span><br><span class="line"> <span class="type">int</span> intArray[arraySize] = {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>};</span><br><span class="line"> <span class="type">int</span>* arrayPtr = intArray;</span><br><span class="line"></span><br><span class="line"> std::cout << <span class="string">"Accessing elements of the array using a pointer:\n"</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < arraySize; ++i) {</span><br><span class="line"> std::cout << <span class="string">"Element "</span> << i << <span class="string">": "</span> << *(arrayPtr + i) << std::endl;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// e. Call the function to double the value using a pointer.</span></span><br><span class="line"> <span class="type">int</span> valueToDouble = <span class="number">7</span>;</span><br><span class="line"> <span class="type">int</span>* valuePtr = &valueToDouble;</span><br><span class="line"></span><br><span class="line"> std::cout << <span class="string">"\nOriginal value before function call: "</span> << valueToDouble << std::endl;</span><br><span class="line"> <span class="built_in">doubleValue</span>(valuePtr);</span><br><span class="line"> std::cout << <span class="string">"Value after doubling using function: "</span> << valueToDouble << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category>backend</category>
</categories>
</entry>
<entry>
<title>Leetcode NO.1 Two Sum</title>
<url>/2023/11/03/Leetcode%20NO.1%20Two%20Sum/</url>
<content><![CDATA[<h2 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h2><p>Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.</p>
<p>You may assume that each input would have exactly on solution ,and you may not use the same element twice.</p>
<p>You can return the answer in any order.</p>
<p>Example :</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="title class_">Input</span> :nums = [<span class="number">2</span>,<span class="number">7</span>,<span class="number">11</span>,<span class="number">15</span>],target = <span class="number">9</span></span><br><span class="line"><span class="title class_">Output</span>: [<span class="number">0</span>,<span class="number">1</span>]</span><br><span class="line"><span class="title class_">Explanation</span> : <span class="title class_">Because</span> nums[<span class="number">0</span>] + nums[<span class="number">1</span>] == <span class="number">9</span> , we <span class="keyword">return</span> [<span class="number">0</span>,<span class="number">1</span>].</span><br></pre></td></tr></table></figure>
<h2 id="Solution"><a href="#Solution" class="headerlink" title="Solution"></a>Solution</h2><h6 id="Solution-1"><a href="#Solution-1" class="headerlink" title="Solution 1:"></a>Solution 1:</h6><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> {<span class="type">number[]</span>} <span class="variable">nums</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> {<span class="type">number</span>} <span class="variable">target</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> {<span class="type">number[]</span>}</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> twoSum = <span class="keyword">function</span>(<span class="params">nums, target</span>) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">let</span> i=<span class="number">0</span>;i<nums.<span class="property">length</span>;i++){</span><br><span class="line"> <span class="keyword">if</span>(nums.<span class="title function_">indexOf</span>(target-nums[i])!=-<span class="number">1</span>&&i!=nums.<span class="title function_">indexOf</span>(target-nums[i])){</span><br><span class="line"> <span class="keyword">return</span> [i,nums.<span class="title function_">indexOf</span>(target-nums[i])]</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<h6 id="Explanation"><a href="#Explanation" class="headerlink" title="Explanation:"></a>Explanation:</h6><p>This solution use the method, indexOf, to find the indice of the target.</p>
<h6 id="Solution-2"><a href="#Solution-2" class="headerlink" title="Solution 2:"></a>Solution 2:</h6><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> {<span class="type">number[]</span>} <span class="variable">nums</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> {<span class="type">number</span>} <span class="variable">target</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> {<span class="type">number[]</span>}</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> twoSum = <span class="keyword">function</span>(<span class="params">nums, target</span>) {</span><br><span class="line"> <span class="keyword">let</span> diff = <span class="keyword">new</span> <span class="title class_">Map</span>()</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">let</span> i=<span class="number">0</span>;i<nums.<span class="property">length</span>;i++){</span><br><span class="line"> <span class="keyword">if</span>(diff.<span class="title function_">has</span>(target-nums[i])){</span><br><span class="line"> <span class="keyword">return</span> [i,diff.<span class="title function_">get</span>(target-nums[i])]</span><br><span class="line"> }</span><br><span class="line"> diff.<span class="title function_">set</span>(nums[i],i)</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<h6 id="Explanation-1"><a href="#Explanation-1" class="headerlink" title="Explanation:"></a>Explanation:</h6><p>This solution use a data structure-map.It looks like the Object (key-value) in javascript. This solution is the best currently.</p>
]]></content>
<categories>
<category>algorithm</category>
</categories>
<tags>
<tag>Leetcode</tag>
<tag>Two Sum</tag>
<tag>Javascript</tag>
</tags>
</entry>
<entry>
<title>Leetcode NO.2 Palindrome Number</title>
<url>/2023/11/03/Leetcode%20NO.2%20Palindrome%20Number/</url>
<content><![CDATA[<h2 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h2><p>Givren an integer x , return true if x is a palindrome, and false otherwise.</p>
<p>Example 1:</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="title class_">Input</span>: x=<span class="number">121</span></span><br><span class="line"><span class="title class_">Output</span>: <span class="literal">true</span></span><br><span class="line"><span class="title class_">Explanation</span>: <span class="number">121</span> reads <span class="keyword">as</span> <span class="number">121</span> <span class="keyword">from</span> left to right and <span class="keyword">from</span> right to left.</span><br></pre></td></tr></table></figure>
<p>Example 2:</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="title class_">Input</span>: x = -<span class="number">121</span></span><br><span class="line"><span class="title class_">Output</span>: <span class="literal">false</span></span><br><span class="line"><span class="title class_">Explanation</span>: <span class="title class_">From</span> left to right, it reads -<span class="number">121.</span> <span class="title class_">From</span> right to left, it becomes <span class="number">121</span>-. <span class="title class_">Therefore</span> it is not a palindrome.</span><br></pre></td></tr></table></figure>
<p>Example 3:</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="title class_">Inout</span>: x = <span class="number">10</span></span><br><span class="line"><span class="title class_">Output</span>: <span class="literal">false</span></span><br><span class="line"><span class="title class_">Explanationg</span>: <span class="title class_">Reads</span> <span class="number">01</span> <span class="keyword">from</span> right to left. <span class="title class_">Therefore</span> it is not a palindrome.</span><br></pre></td></tr></table></figure>
<h2 id="Soulution"><a href="#Soulution" class="headerlink" title="Soulution"></a>Soulution</h2><h6 id="Solution-1"><a href="#Solution-1" class="headerlink" title="Solution 1:"></a>Solution 1:</h6><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> {<span class="type">number</span>} <span class="variable">x</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> {<span class="type">boolean</span>}</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> isPalindrome = <span class="keyword">function</span>(<span class="params">x</span>) {</span><br><span class="line"> <span class="keyword">const</span> y = x.<span class="title function_">toString</span>().<span class="title function_">split</span>(<span class="string">''</span>).<span class="title function_">reverse</span>().<span class="title function_">join</span>(<span class="string">''</span>);</span><br><span class="line"> <span class="keyword">if</span>(x==<span class="title class_">Number</span>(y)) <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<h6 id="Explanation"><a href="#Explanation" class="headerlink" title="Explanation:"></a>Explanation:</h6><p>The javascript method of array-reverse is used in this solution. And you need to change the data format from number to string first. Then split it into an array. But it has a risk about integer overflow when the x is reversed.</p>
<h6 id="Solution-2"><a href="#Solution-2" class="headerlink" title="Solution 2:"></a>Solution 2:</h6><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> {<span class="type">number</span>} <span class="variable">x</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> {<span class="type">boolean</span>}</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> isPalindrome = <span class="keyword">function</span>(<span class="params">x</span>) {</span><br><span class="line"> <span class="keyword">if</span>(x<<span class="number">0</span>||x%<span class="number">10</span>==<span class="number">0</span>&&x!=<span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line"> <span class="keyword">let</span> revertNumber = <span class="number">0</span></span><br><span class="line"> <span class="keyword">while</span>(x<revertNumber){</span><br><span class="line"> revertNumber = x*<span class="number">10</span>+x%<span class="number">10</span></span><br><span class="line"> x = x/<span class="number">10</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> x===revertNumber||x===revertNumber/<span class="number">10</span></span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<h6 id="Explanation-1"><a href="#Explanation-1" class="headerlink" title="Explanation:"></a>Explanation:</h6><p>This solution avoid the risk of integer overflow. The key is to find the revertNumber using a loop. If x is an odd number and x is equal to revertNumber/10(cause revertNumber has the middle number, so we must remove the middle number) after the while-loop , the function returns true. If x is an even number , we just judge whether x is equal to revertNumber after the while-loop.</p>
]]></content>
<categories>
<category>algorithm</category>
</categories>
<tags>
<tag>Leetcode</tag>
<tag>Javascript</tag>
<tag>Palindrome Number</tag>
</tags>
</entry>
<entry>
<title>Passing Arguments</title>
<url>/2023/11/27/Passing-Arguments/</url>
<content><![CDATA[<h4 id="Passing-Arguments-By-Value-By-Reference"><a href="#Passing-Arguments-By-Value-By-Reference" class="headerlink" title="Passing Arguments (By Value, By Reference):"></a>Passing Arguments (By Value, By Reference):</h4><p>a. Write a function incrementByValue that takes an integer parameter and increments it by 1. Call the function and observe the changes.<br>b. Create a function multiplyByTwo that takes a reference to an integer as a parameter and multiplies it by 2. Print the result.<br>c. Define a function swapValues that takes two integers by reference and swaps their values. Print the values before and after swapping.<br>d. Write a function modifyArray that takes an array of doubles and its size as parameters. Multiply each element by 1.5 and print the modified array.<br>e. Create a function appendText that takes a reference to a string and appends “ (Modified)” to it. Print the modified string.</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><string></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// a. Increment an integer by value</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">incrementByValue</span><span class="params">(<span class="type">int</span> num)</span> </span>{</span><br><span class="line"> num += <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// b. Multiply an integer by two using reference</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">multiplyByTwo</span><span class="params">(<span class="type">int</span> &num)</span> </span>{</span><br><span class="line"> num *= <span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// c. Swap values of two integers using reference</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">swapValues</span><span class="params">(<span class="type">int</span> &a, <span class="type">int</span> &b)</span> </span>{</span><br><span class="line"> std::cout << <span class="string">"Before swapping: a = "</span> << a << <span class="string">", b = "</span> << b << std::endl;</span><br><span class="line"> <span class="type">int</span> temp = a;</span><br><span class="line"> a = b;</span><br><span class="line"> b = temp;</span><br><span class="line"> std::cout << <span class="string">"After swapping: a = "</span> << a << <span class="string">", b = "</span> << b << std::endl;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// d. Modify each element of an array of doubles</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">modifyArray</span><span class="params">(<span class="type">double</span> arr[], <span class="type">int</span> size)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < size; ++i) {</span><br><span class="line"> arr[i] *= <span class="number">1.5</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// e. Append text to a string using reference</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">appendText</span><span class="params">(std::string &text)</span> </span>{</span><br><span class="line"> text += <span class="string">" (Modified)"</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">// a. Increment by value</span></span><br><span class="line"> <span class="type">int</span> numA = <span class="number">5</span>;</span><br><span class="line"> <span class="built_in">incrementByValue</span>(numA);</span><br><span class="line"> std::cout << <span class="string">"After incrementByValue: "</span> << numA << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// b. Multiply by two using reference</span></span><br><span class="line"> <span class="type">int</span> numB = <span class="number">8</span>;</span><br><span class="line"> <span class="built_in">multiplyByTwo</span>(numB);</span><br><span class="line"> std::cout << <span class="string">"After multiplyByTwo: "</span> << numB << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// c. Swap values using reference</span></span><br><span class="line"> <span class="type">int</span> a = <span class="number">10</span>, b = <span class="number">20</span>;</span><br><span class="line"> <span class="built_in">swapValues</span>(a, b);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// d. Modify array of doubles</span></span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> arraySize = <span class="number">3</span>;</span><br><span class="line"> <span class="type">double</span> arr[arraySize] = {<span class="number">1.0</span>, <span class="number">2.0</span>, <span class="number">3.0</span>};</span><br><span class="line"> <span class="built_in">modifyArray</span>(arr, arraySize);</span><br><span class="line"> std::cout << <span class="string">"Modified Array: "</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < arraySize; ++i) {</span><br><span class="line"> std::cout << arr[i] << <span class="string">" "</span>;</span><br><span class="line"> }</span><br><span class="line"> std::cout << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// e. Append text to a string using reference</span></span><br><span class="line"> std::string text = <span class="string">"Original Text"</span>;</span><br><span class="line"> <span class="built_in">appendText</span>(text);</span><br><span class="line"> std::cout << <span class="string">"Modified Text: "</span> << text << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category>frontend</category>
<category>backend</category>
</categories>
</entry>
<entry>
<title>Return Types and Void Functions</title>
<url>/2023/11/27/Return-Types-and-Void-Functions/</url>
<content><![CDATA[<h4 id="Return-Types-and-Void-Functions"><a href="#Return-Types-and-Void-Functions" class="headerlink" title="Return Types and Void Functions:"></a>Return Types and Void Functions:</h4><p>a. Define a function calculateArea that takes the radius of a circle as a parameter and returns the area of the circle.<br>b. Write a function divideNumbers that takes two doubles as parameters and returns the result of their division.<br>c. Create a function printRectangle that takes two integers (width and height) as parameters and prints a rectangle of asterisks. The function should be of type void.<br>d. Define a function findRoots that takes three parameters (a, b, c) representing a quadratic equation. Return the roots of the equation (use the quadratic formula). If there are no real roots, return an error message.<br>e. Write a function displayMessage that takes a string as a parameter and displays it on the screen. The function should have a return type of void.</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><cmath></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// a. Calculate the area of a circle</span></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">calculateArea</span><span class="params">(<span class="type">double</span> radius)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="number">3.14</span> * radius * radius;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// b. Divide two numbers</span></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">divideNumbers</span><span class="params">(<span class="type">double</span> a, <span class="type">double</span> b)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (b != <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">return</span> a / b;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> std::cerr << <span class="string">"Error: Division by zero."</span> << std::endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// Returning 0 as an error indicator; you may choose a different approach</span></span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// c. Print a rectangle of asterisks</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">printRectangle</span><span class="params">(<span class="type">int</span> width, <span class="type">int</span> height)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < height; ++i) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j < width; ++j) {</span><br><span class="line"> std::cout << <span class="string">"* "</span>;</span><br><span class="line"> }</span><br><span class="line"> std::cout << std::endl;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// d. Find roots of a quadratic equation</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">findRoots</span><span class="params">(<span class="type">double</span> a, <span class="type">double</span> b, <span class="type">double</span> c)</span> </span>{</span><br><span class="line"> <span class="type">double</span> discriminant = b * b - <span class="number">4</span> * a * c;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span> (discriminant > <span class="number">0</span>) {</span><br><span class="line"> <span class="type">double</span> root1 = (-b + <span class="built_in">sqrt</span>(discriminant)) / (<span class="number">2</span> * a);</span><br><span class="line"> <span class="type">double</span> root2 = (-b - <span class="built_in">sqrt</span>(discriminant)) / (<span class="number">2</span> * a);</span><br><span class="line"> std::cout << <span class="string">"Root 1: "</span> << root1 << <span class="string">"\nRoot 2: "</span> << root2 << std::endl;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (discriminant == <span class="number">0</span>) {</span><br><span class="line"> <span class="type">double</span> root = -b / (<span class="number">2</span> * a);</span><br><span class="line"> std::cout << <span class="string">"Double root: "</span> << root << std::endl;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> std::cerr << <span class="string">"No real roots."</span> << std::endl;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// e. Display a message</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">displayMessage</span><span class="params">(<span class="type">const</span> std::string& message)</span> </span>{</span><br><span class="line"> std::cout << message << std::endl;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">// Example usages of the functions</span></span><br><span class="line"> <span class="type">double</span> circleRadius = <span class="number">5.0</span>;</span><br><span class="line"> std::cout << <span class="string">"Area of circle with radius "</span> << circleRadius << <span class="string">": "</span> << <span class="built_in">calculateArea</span>(circleRadius) << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="type">double</span> dividend = <span class="number">10.0</span>, divisor = <span class="number">2.0</span>;</span><br><span class="line"> std::cout << <span class="string">"Division result: "</span> << <span class="built_in">divideNumbers</span>(dividend, divisor) << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="type">int</span> rectangleWidth = <span class="number">4</span>, rectangleHeight = <span class="number">3</span>;</span><br><span class="line"> std::cout << <span class="string">"Rectangle of asterisks:\n"</span>;</span><br><span class="line"> <span class="built_in">printRectangle</span>(rectangleWidth, rectangleHeight);</span><br><span class="line"></span><br><span class="line"> <span class="type">double</span> quadraticA = <span class="number">1.0</span>, quadraticB = <span class="number">-3.0</span>, quadraticC = <span class="number">2.0</span>;</span><br><span class="line"> <span class="built_in">findRoots</span>(quadraticA, quadraticB, quadraticC);</span><br><span class="line"></span><br><span class="line"> std::string message = <span class="string">"Hello, world!"</span>;</span><br><span class="line"> <span class="built_in">displayMessage</span>(message);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>c++ Definition Functions</title>
<url>/2023/11/27/c-Definition-Functions/</url>
<content><![CDATA[<h4 id="Definition-and-Declaration-of-Functions"><a href="#Definition-and-Declaration-of-Functions" class="headerlink" title="Definition and Declaration of Functions:"></a>Definition and Declaration of Functions:</h4><p>a. Define a function called greet that prints “Hello, World!” to the console.<br>b. Create a function named calculateSum that takes two integers as parameters and returns their sum.<br>c. Write a function printArray that takes an array of integers and its size as parameters and prints all the elements.<br>d. Define a function findMax that takes two doubles as parameters and returns the larger value.<br>e. Create a function isEven that takes an integer as a parameter and returns true if it’s even, otherwise false.</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">greet</span><span class="params">()</span> </span>{</span><br><span class="line"> std::cout << <span class="string">"Hello, World!"</span> << std::endl;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// Declaration (not necessary in this case, as the function is defined above)</span></span><br><span class="line"><span class="comment">// void greet();</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">calculateSum</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> a + b;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// Declaration (not necessary in this case, as the function is defined above)</span></span><br><span class="line"><span class="comment">// int calculateSum(int a, int b);</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">printArray</span><span class="params">(<span class="type">int</span> arr[], <span class="type">int</span> size)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i < size; ++i) {</span><br><span class="line"> std::cout << arr[i] << <span class="string">" "</span>;</span><br><span class="line"> }</span><br><span class="line"> std::cout << std::endl;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// Declaration (not necessary in this case, as the function is defined above)</span></span><br><span class="line"><span class="comment">// void printArray(int arr[], int size);</span></span><br><span class="line"><span class="function"><span class="type">double</span> <span class="title">findMax</span><span class="params">(<span class="type">double</span> a, <span class="type">double</span> b)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> (a > b) ? a : b;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// Declaration (not necessary in this case, as the function is defined above)</span></span><br><span class="line"><span class="comment">// double findMax(double a, double b);</span></span><br><span class="line"><span class="function"><span class="type">bool</span> <span class="title">isEven</span><span class="params">(<span class="type">int</span> num)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> (num % <span class="number">2</span> == <span class="number">0</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// Declaration (not necessary in this case, as the function is defined above)</span></span><br><span class="line"><span class="comment">// bool isEven(int num);</span></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>YOOBEE-WEEK2-ACTIVITY</title>
<url>/2023/11/17/YOOBEE-WEEK2-ACTIVITY/</url>
<content><![CDATA[<h2 id="Control-structure"><a href="#Control-structure" class="headerlink" title="Control structure"></a>Control structure</h2><h3 id="a-Write-a-C-program-to-check-whether-a-given-number-is-even-or-odd-using-the-if-statement"><a href="#a-Write-a-C-program-to-check-whether-a-given-number-is-even-or-odd-using-the-if-statement" class="headerlink" title="a.Write a C++ program to check whether a given number is even or odd using the if statement."></a>a.Write a C++ program to check whether a given number is even or odd using the if statement.</h3><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="type">int</span> number;</span><br><span class="line"> </span><br><span class="line"> std::cout << <span class="string">"enter a number"</span> << std::endl;</span><br><span class="line"> </span><br><span class="line"> std::cin >> number;</span><br><span class="line"> </span><br><span class="line"> std::cout << <span class="string">"you input "</span> << number << std::endl;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span>(number%<span class="number">2</span>==<span class="number">0</span>){</span><br><span class="line"> std::cout << number << <span class="string">" is even"</span> << std::endl;</span><br><span class="line"> } <span class="keyword">else</span>{</span><br><span class="line"> std::cout << number << <span class="string">" is odd"</span> << std::endl;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="b-Create-a-program-that-prompts-the-user-for-their-age-and-displays-whether-they-are-a-child-teenager-or-adult-using-the-if-else-statement"><a href="#b-Create-a-program-that-prompts-the-user-for-their-age-and-displays-whether-they-are-a-child-teenager-or-adult-using-the-if-else-statement" class="headerlink" title="b. Create a program that prompts the user for their age and displays whether they are a child, teenager, or adult using the if-else statement."></a>b. Create a program that prompts the user for their age and displays whether they are a child, teenager, or adult using the if-else statement.</h3><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="type">int</span> number;</span><br><span class="line"> </span><br><span class="line"> std::cout << <span class="string">"enter his/her age"</span> << std::endl;</span><br><span class="line"> </span><br><span class="line"> std::cin >> number;</span><br><span class="line"> </span><br><span class="line"> std::cout << <span class="string">"you input "</span> << number << std::endl;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span>(number<=<span class="number">8</span>){</span><br><span class="line"> std::cout << <span class="string">" his/her is a child"</span> << std::endl;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span>(number><span class="number">8</span>&&number<=<span class="number">18</span>){</span><br><span class="line"> std::cout << <span class="string">" his/her is a teenager"</span> << std::endl;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> std::cout << <span class="string">" his/her is an adult"</span> << std::endl;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="c-Implement-a-menu-driven-program-that-performs-basic-arithmetic-operations-addition-subtraction-multiplication-division-based-on-user-input-Use-a-switch-statement-for-the-menu-selection"><a href="#c-Implement-a-menu-driven-program-that-performs-basic-arithmetic-operations-addition-subtraction-multiplication-division-based-on-user-input-Use-a-switch-statement-for-the-menu-selection" class="headerlink" title="c. Implement a menu-driven program that performs basic arithmetic operations (addition, subtraction, multiplication, division) based on user input. Use a switch statement for the menu selection."></a>c. Implement a menu-driven program that performs basic arithmetic operations (addition, subtraction, multiplication, division) based on user input. Use a switch statement for the menu selection.</h3><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// using namespace std;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="type">char</span> operation;</span><br><span class="line"> <span class="type">int</span> number1, number2;</span><br><span class="line"> </span><br><span class="line"> std::cout << <span class="string">"menu \n"</span>;</span><br><span class="line"> std::cout << <span class="string">"1, addition"</span> ;</span><br><span class="line"> std::cout << <span class="string">"2, subtraction"</span> ;</span><br><span class="line"> std::cout << <span class="string">"3, multiplication"</span> ;</span><br><span class="line"> std::cout << <span class="string">"4, division"</span> ;</span><br><span class="line"> </span><br><span class="line"> std::cout << <span class="string">"please select the type of operation(1-4)"</span> ;</span><br><span class="line"> std::cin >> operation;</span><br><span class="line"> </span><br><span class="line"> std::cout << <span class="string">"please input the first number"</span> ;</span><br><span class="line"> std::cin >> number1;</span><br><span class="line"> std::cout << <span class="string">"please input the second number"</span> ;</span><br><span class="line"> std::cin >> number2;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">switch</span>(operation){</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'1'</span>:</span><br><span class="line"> std::cout << <span class="string">"the result is "</span><< number1+number2 << std::endl;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'2'</span>:</span><br><span class="line"> std::cout << <span class="string">"the result is "</span><< number1-number2 << std::endl;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'3'</span>:</span><br><span class="line"> std::cout << <span class="string">"the result is "</span><< number1*number2 << std::endl;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'4'</span>:</span><br><span class="line"> <span class="keyword">if</span>(number2==<span class="number">0</span>){</span><br><span class="line"> std::cout << <span class="string">"Error: Division by zero is undefined.\n"</span>;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> std::cout << <span class="string">"the result is "</span><< number1/number2 << std::endl;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">default</span>:</span><br><span class="line"> std::cout << <span class="string">"Please input a number between 1-4.\n"</span>;</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="Conditional-statements"><a href="#Conditional-statements" class="headerlink" title="Conditional statements"></a>Conditional statements</h2><h3 id="a-Write-a-program-that-takes-three-integer-inputs-and-finds-the-largest-of-the-three-using-if-else-statements"><a href="#a-Write-a-program-that-takes-three-integer-inputs-and-finds-the-largest-of-the-three-using-if-else-statements" class="headerlink" title="a. Write a program that takes three integer inputs and finds the largest of the three using if-else statements."></a>a. Write a program that takes three integer inputs and finds the largest of the three using if-else statements.</h3><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// using namespace std;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="type">int</span> number1, number2, number3;</span><br><span class="line"> <span class="type">int</span> temp;</span><br><span class="line"> </span><br><span class="line"> std::cout << <span class="string">"please input the first number"</span> ;</span><br><span class="line"> std::cin >> number1;</span><br><span class="line"> std::cout << <span class="string">"please input the second number"</span> ;</span><br><span class="line"> std::cin >> number2;</span><br><span class="line"> <span class="keyword">if</span>(number2>=number1){</span><br><span class="line"> temp=number2;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> temp=number2;</span><br><span class="line"> }</span><br><span class="line"> std::cout << <span class="string">"please input the third number"</span> ;</span><br><span class="line"> std::cin >> number3;</span><br><span class="line"> <span class="keyword">if</span>(temp>=number3){</span><br><span class="line"> std::cout << <span class="string">"the largest number is "</span><< temp << std::endl;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> std::cout << <span class="string">"the largest number is "</span><< number3 << std::endl;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="b-Create-a-simple-calculator-program-that-allows-the-user-to-input-two-numbers-and-an-arithmetic-operator-or-to-perform-the-operation-using-if-else-statements"><a href="#b-Create-a-simple-calculator-program-that-allows-the-user-to-input-two-numbers-and-an-arithmetic-operator-or-to-perform-the-operation-using-if-else-statements" class="headerlink" title="b. Create a simple calculator program that allows the user to input two numbers and an arithmetic operator (+, -, *, or /) to perform the operation using if-else statements."></a>b. Create a simple calculator program that allows the user to input two numbers and an arithmetic operator (+, -, *, or /) to perform the operation using if-else statements.</h3><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// using namespace std;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="type">int</span> number1, number2;</span><br><span class="line"> <span class="type">char</span> operation;</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> std::cout << <span class="string">"please input the first number"</span> ;</span><br><span class="line"> std::cin >> number1;</span><br><span class="line"> std::cout << <span class="string">"please input an arithmetic operator"</span> ;</span><br><span class="line"> std::cin >> operation ;</span><br><span class="line"> std::cout << <span class="string">"please input the second number"</span> ;</span><br><span class="line"> std::cin >> number2;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span>(operation==<span class="string">'+'</span>){</span><br><span class="line"> std::cout << <span class="string">"the result is "</span><< number1 + number2 << std::endl;</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>(operation==<span class="string">'-'</span>){</span><br><span class="line"> std::cout << <span class="string">"the result is "</span><< number1 - number2 << std::endl;</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>(operation==<span class="string">'*'</span>){</span><br><span class="line"> std::cout << <span class="string">"the result is "</span><< number1 * number2 << std::endl;</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>(operation==<span class="string">'/'</span>){</span><br><span class="line"> <span class="keyword">if</span>(number2==<span class="number">0</span>){</span><br><span class="line"> std::cout << <span class="string">"Error: Division by zero is undefined"</span><< std::endl;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> std::cout << <span class="string">"the result is "</span><< number1/number2 << std::endl;</span><br><span class="line"> }</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> std::cout << <span class="string">"please input a correct arithmetic operator"</span> << std::endl;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="c-Write-a-program-to-determine-the-type-of-a-given-character-uppercase-letter-lowercase-letter-digit-special-character-using-if-else-statements"><a href="#c-Write-a-program-to-determine-the-type-of-a-given-character-uppercase-letter-lowercase-letter-digit-special-character-using-if-else-statements" class="headerlink" title="c. Write a program to determine the type of a given character (uppercase letter, lowercase letter, digit, special character) using if-else statements."></a>c. Write a program to determine the type of a given character (uppercase letter, lowercase letter, digit, special character) using if-else statements.</h3><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="type">char</span> inputChar;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Prompt the user to enter a character</span></span><br><span class="line"> std::cout << <span class="string">"Enter a character: "</span>;</span><br><span class="line"> std::cin >> inputChar;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Check the type of the character using if-else statements</span></span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">isalpha</span>(inputChar)) {</span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">isupper</span>(inputChar)) {</span><br><span class="line"> std::cout << <span class="string">"Uppercase letter\n"</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> std::cout << <span class="string">"Lowercase letter\n"</span>;</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">isdigit</span>(inputChar)) {</span><br><span class="line"> std::cout << <span class="string">"Digit\n"</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> std::cout << <span class="string">"Special character\n"</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="Looping-Statements-while-do-while-for"><a href="#Looping-Statements-while-do-while-for" class="headerlink" title="Looping Statements(while, do-while, for)"></a>Looping Statements(while, do-while, for)</h2><h3 id="a-Write-a-C-program-to-print-the-first-10-natural-numbers-using-a-while-loop"><a href="#a-Write-a-C-program-to-print-the-first-10-natural-numbers-using-a-while-loop" class="headerlink" title="a. Write a C++ program to print the first 10 natural numbers using a while loop."></a>a. Write a C++ program to print the first 10 natural numbers using a while loop.</h3><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="type">int</span> number = <span class="number">1</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">while</span>(number<=<span class="number">10</span>){</span><br><span class="line"> std::cout << number << std::endl;</span><br><span class="line"> number++;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="b-Create-a-program-that-takes-an-integer-input-from-the-user-and-calculates-the-factorial-of-that-number-using-a-for-loop"><a href="#b-Create-a-program-that-takes-an-integer-input-from-the-user-and-calculates-the-factorial-of-that-number-using-a-for-loop" class="headerlink" title="b. Create a program that takes an integer input from the user and calculates the factorial of that number using a for loop."></a>b. Create a program that takes an integer input from the user and calculates the factorial of that number using a for loop.</h3><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="type">int</span> integer;</span><br><span class="line"> <span class="type">long</span> <span class="type">long</span> factorial=<span class="number">1</span>;</span><br><span class="line"> std::cout << <span class="string">"Please enter a number"</span> << std::endl;</span><br><span class="line"> std::cin >> integer;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span>(integer<<span class="number">0</span>){</span><br><span class="line"> std::cout << <span class="string">"Please enter a correct number"</span> << std::endl;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i <=integer; i++) {</span><br><span class="line"> <span class="comment">/* code */</span></span><br><span class="line"> factorial = factorial*i;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> std::cout << <span class="string">"the result of factorial("</span><< integer <<<span class="string">") is "</span> << factorial << std::endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="c-Write-a-program-that-uses-a-do-while-loop-to-ask-the-user-for-a-number-until-the-user-enters-a-negative-number"><a href="#c-Write-a-program-that-uses-a-do-while-loop-to-ask-the-user-for-a-number-until-the-user-enters-a-negative-number" class="headerlink" title="c. Write a program that uses a do-while loop to ask the user for a number until the user enters a negative number."></a>c. Write a program that uses a do-while loop to ask the user for a number until the user enters a negative number.</h3><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="type">int</span> integer;</span><br><span class="line"> <span class="keyword">do</span>{</span><br><span class="line"> std::cout << <span class="string">"Please enter a number"</span> << std::endl;</span><br><span class="line"> std::cin >> integer;</span><br><span class="line"> }<span class="keyword">while</span>(integer>=<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="Break-and-Continue-Statements"><a href="#Break-and-Continue-Statements" class="headerlink" title="Break and Continue Statements"></a>Break and Continue Statements</h2><h3 id="a-Write-a-program-to-find-the-sum-of-all-positive-integers-entered-by-the-user-until-they-enter-a-negative-number-Use-a-break-statement-to-terminate-the-loop"><a href="#a-Write-a-program-to-find-the-sum-of-all-positive-integers-entered-by-the-user-until-they-enter-a-negative-number-Use-a-break-statement-to-terminate-the-loop" class="headerlink" title="a. Write a program to find the sum of all positive integers entered by the user until they enter a negative number. Use a break statement to terminate the loop."></a>a. Write a program to find the sum of all positive integers entered by the user until they enter a negative number. Use a break statement to terminate the loop.</h3><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="type">int</span> number;</span><br><span class="line"> <span class="type">int</span> sum = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="comment">// Prompt the user to enter an integer</span></span><br><span class="line"> std::cout << <span class="string">"Enter an integer (negative to exit): "</span>;</span><br><span class="line"> std::cin >> number;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Check if the entered number is negative</span></span><br><span class="line"> <span class="keyword">if</span> (number < <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">break</span>; <span class="comment">// Exit the loop if the number is negative</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Add the positive number to the sum</span></span><br><span class="line"> sum += number;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Display the sum of positive integers</span></span><br><span class="line"> std::cout << <span class="string">"Sum of positive integers: "</span> << sum << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="b-Implement-a-program-that-prints-all-odd-numbers-from-1-to-50-but-skips-the-number-13-using-the-continue-statement"><a href="#b-Implement-a-program-that-prints-all-odd-numbers-from-1-to-50-but-skips-the-number-13-using-the-continue-statement" class="headerlink" title="b. Implement a program that prints all odd numbers from 1 to 50, but skips the number 13 using the continue statement."></a>b. Implement a program that prints all odd numbers from 1 to 50, but skips the number 13 using the continue statement.</h3><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i <= <span class="number">50</span>; i += <span class="number">2</span>) {</span><br><span class="line"> <span class="keyword">if</span> (i == <span class="number">13</span>) {</span><br><span class="line"> <span class="keyword">continue</span>; <span class="comment">// Skip the number 13</span></span><br><span class="line"> }</span><br><span class="line"> std::cout << i << <span class="string">" "</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> std::cout << std::endl;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="c-Create-a-simple-guessing-game-where-the-program-generates-a-random-number-between-1-and-100-and-the-user-has-to-guess-the-number-Use-a-break-statement-to-exit-the-loop-when-the-user-guesses-the-correct-number"><a href="#c-Create-a-simple-guessing-game-where-the-program-generates-a-random-number-between-1-and-100-and-the-user-has-to-guess-the-number-Use-a-break-statement-to-exit-the-loop-when-the-user-guesses-the-correct-number" class="headerlink" title="c. Create a simple guessing game where the program generates a random number between 1 and 100, and the user has to guess the number. Use a break statement to exit the loop when the user guesses the correct number."></a>c. Create a simple guessing game where the program generates a random number between 1 and 100, and the user has to guess the number. Use a break statement to exit the loop when the user guesses the correct number.</h3><figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><cstdlib></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><ctime></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">// Seed the random number generator with the current time</span></span><br><span class="line"> std::<span class="built_in">srand</span>(<span class="built_in">static_cast</span><<span class="type">unsigned</span> <span class="type">int</span>>(std::<span class="built_in">time</span>(<span class="literal">nullptr</span>)));</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Generate a random number between 1 and 100</span></span><br><span class="line"> <span class="type">int</span> secretNumber = std::<span class="built_in">rand</span>() % <span class="number">100</span> + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"> <span class="type">int</span> guess;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Main game loop</span></span><br><span class="line"> <span class="keyword">while</span> (<span class="literal">true</span>) {</span><br><span class="line"> <span class="comment">// Prompt the user to guess the number</span></span><br><span class="line"> std::cout << <span class="string">"Guess the number between 1 and 100: "</span>;</span><br><span class="line"> std::cin >> guess;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Check if the guess is correct</span></span><br><span class="line"> <span class="keyword">if</span> (guess == secretNumber) {</span><br><span class="line"> std::cout << <span class="string">"Congratulations! You guessed the correct number."</span> << std::endl;</span><br><span class="line"> <span class="keyword">break</span>; <span class="comment">// Exit the loop if the guess is correct</span></span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">// Provide a hint and continue the loop</span></span><br><span class="line"> std::cout << <span class="string">"Wrong guess! Try again."</span> << std::endl;</span><br><span class="line"> <span class="keyword">if</span> (guess < secretNumber) {</span><br><span class="line"> std::cout << <span class="string">"Hint: The secret number is greater."</span> << std::endl;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> std::cout << <span class="string">"Hint: The secret number is smaller."</span> << std::endl;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>algorithm</category>
</categories>
<tags>
<tag>C++</tag>
</tags>
</entry>
</search>