-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.xml
executable file
·894 lines (677 loc) · 104 KB
/
feed.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.5">Jekyll</generator><link href="http://localhost:4000/cplusplusstudent/feed.xml" rel="self" type="application/atom+xml" /><link href="http://localhost:4000/cplusplusstudent/" rel="alternate" type="text/html" /><updated>2020-08-10T18:54:04+05:30</updated><id>http://localhost:4000/cplusplusstudent/feed.xml</id><title type="html">The C++ Student</title><subtitle>I'm just a student who wants to delve deeper into the world of Computing, with C++ as my base.
</subtitle><author><name>Nischay Hegde</name><email>[email protected]</email></author><entry><title type="html">IOCCC (1985)</title><link href="http://localhost:4000/cplusplusstudent/c/2017/11/17/IOCCC3/" rel="alternate" type="text/html" title="IOCCC (1985)" /><published>2017-11-17T00:30:00+05:30</published><updated>2017-11-17T00:30:00+05:30</updated><id>http://localhost:4000/cplusplusstudent/c/2017/11/17/IOCCC3</id><content type="html" xml:base="http://localhost:4000/cplusplusstudent/c/2017/11/17/IOCCC3/"><ul id="markdown-toc">
<li><a href="#lycklamac" id="markdown-toc-lycklamac"><code class="highlighter-rouge">lycklama.c</code></a> <ul>
<li><a href="#analysing-the-code-and-removing-some-obfuscation" id="markdown-toc-analysing-the-code-and-removing-some-obfuscation">Analysing the code, and removing some obfuscation</a></li>
<li><a href="#decoding-the-mess" id="markdown-toc-decoding-the-mess">Decoding the mess</a></li>
</ul>
</li>
<li><a href="#the-copyright" id="markdown-toc-the-copyright">The copyright</a></li>
</ul>
<h2 id="lycklamac"><code class="highlighter-rouge">lycklama.c</code></h2>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#define o define
#o ___o write
#o ooo (unsigned)
#o o_o_ 1
#o _o_ char
#o _oo goto
#o _oo_ read
#o o_o for
#o o_ main
#o o__ if
#o oo_ 0
#o _o(_,__,___)(void)___o(_,__,ooo(___))
#o __o (o_o_&lt;&lt;((o_o_&lt;&lt;(o_o_&lt;&lt;o_o_))+(o_o_&lt;&lt;o_o_)))+(o_o_&lt;&lt;(o_o_&lt;&lt;(o_o_&lt;&lt;o_o_)))
o_(){_o_ _=oo_,__,___,____[__o];_oo ______;_____:___=__o-o_o_; _______:
_o(o_o_,____,__=(_-o_o_&lt;___?_-o_o_:___));o_o(;__;_o(o_o_,"\b",o_o_),__--);
_o(o_o_," ",o_o_);o__(--___)_oo _______;_o(o_o_,"\n",o_o_);______:o__(_=_oo_(
oo_,____,__o))_oo _____;}
</code></pre></div></div>
<p>This is the code.</p>
<h3 id="analysing-the-code-and-removing-some-obfuscation">Analysing the code, and removing some obfuscation</h3>
<p>Let’s analyse some of the <code class="highlighter-rouge">#define</code>s made.</p>
<ol>
<li><code class="highlighter-rouge">#o __o (o_o_&lt;&lt;((o_o_&lt;&lt;(o_o_&lt;&lt;o_o_))+(o_o_&lt;&lt;o_o_)))+(o_o_&lt;&lt;(o_o_&lt;&lt;(o_o_&lt;&lt;o_o_)))</code>
Let’s simplfiy this a bit more. Becuase of the <code class="highlighter-rouge">#define</code>, <code class="highlighter-rouge">o_o_</code> is just <code class="highlighter-rouge">1</code> and the <code class="highlighter-rouge">&lt;&lt;</code> operator is the left shift operator. So this basically becomes:</li>
</ol>
<p><code class="highlighter-rouge">#define __o (1&lt;&lt;((1&lt;&lt;(1&lt;&lt;1))+(1&lt;&lt;1)))+(1&lt;&lt;(1&lt;&lt;(1&lt;&lt;1)))</code></p>
<p>Now, because all the right shifts are just multiplying by two here, we can further simplify it as:</p>
<p><code class="highlighter-rouge">#define __o (1&lt;&lt;((1&lt;&lt;2)+2))+(1&lt;&lt;(1&lt;&lt;2))</code>, which is the same as <code class="highlighter-rouge">#define __o (1&lt;&lt;(4+2))+(1&lt;&lt;4)</code>.</p>
<p>Now, this simplifies to <code class="highlighter-rouge">#define __o (1&lt;&lt;6)+8</code>, which becomes <code class="highlighter-rouge">#define __o 20</code></p>
<p>We can remove all the crazy defines and make it look a bit simpler, after which it looks like this:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#define _o(_,__,___)(void)write(_,__,(unsigned)(___))
main()
{
char _=0,__,___,____[20];
goto ______;
_____:___=20-1;
_______:_o(1,____,__=(_-1&lt;___?_-1:___));
for(;__;_o(1,"\b",1),__--);
_o(1," ",1);
if(--___)goto _______;
_o(1,"\n",1);
______:
if(_=read(0,____,20))
_oo _____;}
</code></pre></div></div>
<p>If you have a keen eye, you will notice that there are three characters here, one array, and one <code class="highlighter-rouge">goto</code>.
Now, time for me to rename some values. I’ll just call them <code class="highlighter-rouge">char1</code>, <code class="highlighter-rouge">char2</code>, <code class="highlighter-rouge">char3</code>, and <code class="highlighter-rouge">arr</code>. I’ll call the <code class="highlighter-rouge">goto</code> label <code class="highlighter-rouge">label</code>. Becuase I have zero creatvity, like whatever I had done until now.</p>
<h3 id="decoding-the-mess">Decoding the mess</h3>
<p>This uses <code class="highlighter-rouge">write</code> and <code class="highlighter-rouge">read</code>, both UNIX system commands, to print out a hello world. At least, that’s what it’s supposed to do.</p>
<p>The “fixed” (read: changed some names) code:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;sys/syscall.h&gt;
#include &lt;unistd.h&gt;
#define _o(_,__,___)(void)write(_,__,(unsigned)(___))
int main()
{
char char1=0,char2,char3,arr[20];
goto label;
label2:char3=19;
label3:_o(1,arr,char2=(char1--&lt;char3?char1--:char3));
for(;char2;_o(1,"\b",1),char2--);
_o(1," ",1);
if(--char3)goto label3;
_o(1,"\n",1);
label:
if(char1=read(0,arr,20))
goto label2;
}
</code></pre></div></div>
<p>Apparently, you have to give it very long lines as arguments. But based on what I can see, it will basically write the whole line again, but will be <em>very</em> inefficient in doing so.</p>
<h2 id="the-copyright">The copyright</h2>
<p>Copyright (c) 1984, Landon Curt Noll.
All Rights Reserved. Permission for personal, educational or non-profit use is
granted provided this this copyright and notice are included in its entirety
and remains unaltered. All other uses must receive prior permission in writing
from both Landon Curt Noll and Larry Bassel.</p></content><author><name>Nischay Hegde</name><email>[email protected]</email></author><category term="IOCCC" /><summary type="html"></summary></entry><entry><title type="html">IOCCC (1984) Part 2</title><link href="http://localhost:4000/cplusplusstudent/c/2017/11/16/IOCCC2/" rel="alternate" type="text/html" title="IOCCC (1984) Part 2" /><published>2017-11-16T00:30:00+05:30</published><updated>2017-11-16T00:30:00+05:30</updated><id>http://localhost:4000/cplusplusstudent/c/2017/11/16/IOCCC2</id><content type="html" xml:base="http://localhost:4000/cplusplusstudent/c/2017/11/16/IOCCC2/"><ul id="markdown-toc">
<li><a href="#work-in-progress" id="markdown-toc-work-in-progress">Work in Progress</a></li>
<li><a href="#lamanc" id="markdown-toc-lamanc"><code class="highlighter-rouge">laman.c</code></a></li>
<li><a href="#the-copyright" id="markdown-toc-the-copyright">The copyright</a></li>
</ul>
<h2 id="work-in-progress">Work in Progress</h2>
<p>This page is a work in progress, that is, I will try to explain stuff, but I have not (yet) understood everything myself.</p>
<h2 id="lamanc"><code class="highlighter-rouge">laman.c</code></h2>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">a</span><span class="p">[</span><span class="mi">900</span><span class="p">];</span> <span class="n">b</span><span class="p">;</span><span class="n">c</span><span class="p">;</span><span class="n">d</span><span class="o">=</span><span class="mi">1</span> <span class="p">;</span><span class="n">e</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="n">f</span><span class="p">;</span> <span class="n">g</span><span class="p">;</span><span class="n">h</span><span class="p">;</span><span class="n">O</span><span class="p">;</span> <span class="n">main</span><span class="p">(</span><span class="n">k</span><span class="p">,</span>
<span class="n">l</span><span class="p">)</span><span class="kt">char</span><span class="o">*</span> <span class="o">*</span><span class="n">l</span><span class="p">;{</span><span class="n">g</span><span class="o">=</span> <span class="n">atoi</span><span class="p">(</span><span class="o">*</span> <span class="o">++</span><span class="n">l</span><span class="p">);</span> <span class="k">for</span><span class="p">(</span><span class="n">k</span><span class="o">=</span>
<span class="mi">0</span><span class="p">;</span><span class="n">k</span><span class="o">*</span><span class="n">k</span><span class="o">&lt;</span> <span class="n">g</span><span class="p">;</span><span class="n">b</span><span class="o">=</span><span class="n">k</span> <span class="o">++&gt;&gt;</span><span class="mi">1</span><span class="p">)</span> <span class="p">;</span><span class="k">for</span><span class="p">(</span><span class="n">h</span><span class="o">=</span> <span class="mi">0</span><span class="p">;</span><span class="n">h</span><span class="o">*</span><span class="n">h</span><span class="o">&lt;=</span>
<span class="n">g</span><span class="p">;</span><span class="o">++</span><span class="n">h</span><span class="p">);</span> <span class="o">--</span><span class="n">h</span><span class="p">;</span><span class="n">c</span><span class="o">=</span><span class="p">(</span> <span class="p">(</span><span class="n">h</span><span class="o">+=</span><span class="n">g</span><span class="o">&gt;</span><span class="n">h</span> <span class="o">*</span><span class="p">(</span><span class="n">h</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">&gt;&gt;</span><span class="mi">1</span><span class="p">;</span>
<span class="k">while</span><span class="p">(</span><span class="n">d</span> <span class="o">&lt;=</span><span class="n">g</span><span class="p">){</span> <span class="o">++</span><span class="n">O</span><span class="p">;</span><span class="k">for</span> <span class="p">(</span><span class="n">f</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">f</span><span class="o">&lt;</span> <span class="n">O</span><span class="o">&amp;&amp;</span><span class="n">d</span><span class="o">&lt;=</span><span class="n">g</span>
<span class="p">;</span><span class="o">++</span><span class="n">f</span><span class="p">)</span><span class="n">a</span><span class="p">[</span> <span class="n">b</span><span class="o">&lt;&lt;</span><span class="mi">5</span><span class="o">|</span><span class="n">c</span><span class="p">]</span> <span class="o">=</span><span class="n">d</span><span class="o">++</span><span class="p">,</span><span class="n">b</span><span class="o">+=</span> <span class="n">e</span><span class="p">;</span><span class="k">for</span><span class="p">(</span> <span class="n">f</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">f</span><span class="o">&lt;</span><span class="n">O</span>
<span class="o">&amp;&amp;</span><span class="n">d</span><span class="o">&lt;=</span><span class="n">g</span><span class="p">;</span> <span class="o">++</span><span class="n">f</span><span class="p">)</span><span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span><span class="mi">5</span><span class="o">|</span><span class="n">c</span><span class="p">]</span><span class="o">=</span> <span class="n">d</span><span class="o">++</span><span class="p">,</span><span class="n">c</span><span class="o">+=</span> <span class="n">e</span><span class="p">;</span><span class="n">e</span><span class="o">=</span> <span class="o">-</span><span class="n">e</span>
<span class="p">;}</span><span class="k">for</span><span class="p">(</span><span class="n">c</span> <span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">c</span><span class="o">&lt;</span><span class="n">h</span><span class="p">;</span> <span class="o">++</span><span class="n">c</span><span class="p">){</span> <span class="k">for</span><span class="p">(</span><span class="n">b</span><span class="o">=</span><span class="mi">0</span> <span class="p">;</span><span class="n">b</span><span class="o">&lt;</span><span class="n">k</span><span class="p">;</span><span class="o">++</span>
<span class="n">b</span><span class="p">){</span><span class="k">if</span><span class="p">(</span><span class="n">b</span> <span class="o">&lt;</span><span class="n">k</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span><span class="n">a</span><span class="p">[</span> <span class="n">b</span><span class="o">&lt;&lt;</span><span class="mi">5</span><span class="o">|</span><span class="n">c</span><span class="p">]</span> <span class="o">^=</span><span class="n">a</span><span class="p">[(</span><span class="n">k</span> <span class="o">-</span><span class="p">(</span><span class="n">b</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span>
<span class="o">&lt;&lt;</span><span class="mi">5</span><span class="o">|</span><span class="n">c</span><span class="p">]</span><span class="o">^=</span> <span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="o">&lt;&lt;</span><span class="mi">5</span> <span class="o">|</span><span class="n">c</span><span class="p">]</span><span class="o">^=</span><span class="n">a</span><span class="p">[</span> <span class="p">(</span><span class="n">k</span><span class="o">-</span><span class="p">(</span><span class="n">b</span><span class="o">+</span><span class="mi">1</span> <span class="p">))</span><span class="o">&lt;&lt;</span><span class="mi">5</span><span class="o">|</span><span class="n">c</span><span class="p">]</span>
<span class="p">;</span><span class="n">printf</span><span class="p">(</span> <span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="o">&lt;&lt;</span><span class="mi">5</span><span class="o">|</span><span class="n">c</span> <span class="p">]</span><span class="o">?</span><span class="s">"%-4d"</span> <span class="o">:</span><span class="s">" "</span> <span class="p">,</span><span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="o">&lt;&lt;</span><span class="mi">5</span>
<span class="o">|</span><span class="n">c</span><span class="p">]);}</span> <span class="n">putchar</span><span class="p">(</span> <span class="sc">'\n'</span><span class="p">);}}</span> <span class="cm">/*Mike Laman*/</span>
</code></pre></div></div>
<p>Would ya look at this beauty.
It is absolutely awesome!
Okay, enough talking. Let’s get to work.</p>
<p>After going through some beautification, this is what it looks like:</p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">a</span><span class="p">[</span><span class="mi">900</span><span class="p">];</span>
<span class="n">b</span><span class="p">;</span>
<span class="n">c</span><span class="p">;</span>
<span class="n">d</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="n">e</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="n">f</span><span class="p">;</span>
<span class="n">g</span><span class="p">;</span>
<span class="n">h</span><span class="p">;</span>
<span class="n">O</span><span class="p">;</span>
<span class="n">main</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">l</span><span class="p">)</span> <span class="kt">char</span> <span class="o">*</span> <span class="o">*</span> <span class="n">l</span><span class="p">;</span> <span class="p">{</span>
<span class="n">g</span> <span class="o">=</span> <span class="n">atoi</span><span class="p">(</span> <span class="o">*</span> <span class="o">++</span><span class="n">l</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="n">k</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">k</span> <span class="o">*</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="n">g</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="n">k</span><span class="o">++</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="n">h</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">h</span> <span class="o">*</span> <span class="n">h</span> <span class="o">&lt;=</span> <span class="n">g</span><span class="p">;</span> <span class="o">++</span><span class="n">h</span><span class="p">);</span>
<span class="o">--</span><span class="n">h</span><span class="p">;</span>
<span class="n">c</span> <span class="o">=</span> <span class="p">((</span><span class="n">h</span> <span class="o">+=</span> <span class="n">g</span> <span class="o">&gt;</span> <span class="n">h</span> <span class="o">*</span> <span class="p">(</span><span class="n">h</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">d</span> <span class="o">&lt;=</span> <span class="n">g</span><span class="p">)</span> <span class="p">{</span>
<span class="o">++</span><span class="n">O</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">f</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">f</span> <span class="o">&lt;</span> <span class="n">O</span> <span class="o">&amp;&amp;</span> <span class="n">d</span> <span class="o">&lt;=</span> <span class="n">g</span><span class="p">;</span> <span class="o">++</span><span class="n">f</span><span class="p">)</span> <span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="n">d</span><span class="o">++</span><span class="p">,</span> <span class="n">b</span> <span class="o">+=</span> <span class="n">e</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">f</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">f</span> <span class="o">&lt;</span> <span class="n">O</span> <span class="o">&amp;&amp;</span> <span class="n">d</span> <span class="o">&lt;=</span> <span class="n">g</span><span class="p">;</span> <span class="o">++</span><span class="n">f</span><span class="p">)</span> <span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="n">d</span><span class="o">++</span><span class="p">,</span> <span class="n">c</span> <span class="o">+=</span> <span class="n">e</span><span class="p">;</span>
<span class="n">e</span> <span class="o">=</span> <span class="o">-</span><span class="n">e</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">for</span> <span class="p">(</span><span class="n">c</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">c</span> <span class="o">&lt;</span> <span class="n">h</span><span class="p">;</span> <span class="o">++</span><span class="n">c</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span><span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">b</span> <span class="o">&lt;</span> <span class="n">k</span><span class="p">;</span> <span class="o">++</span>
<span class="n">b</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">b</span> <span class="o">&lt;</span> <span class="n">k</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span> <span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]</span> <span class="o">^=</span> <span class="n">a</span><span class="p">[(</span><span class="n">k</span> <span class="o">-</span> <span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]</span> <span class="o">^=</span> <span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]</span> <span class="o">^=</span> <span class="n">a</span><span class="p">[(</span><span class="n">k</span> <span class="o">-</span> <span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">];</span>
<span class="n">printf</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]</span> <span class="o">?</span> <span class="s">"%-4d"</span> <span class="o">:</span> <span class="s">" "</span><span class="p">,</span> <span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]);</span>
<span class="p">}</span>
<span class="n">putchar</span><span class="p">(</span><span class="sc">'\n'</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span> <span class="cm">/*Mike Laman*/</span>
</code></pre></div></div>
<p>Okay, still not very comprehensible.</p>
<p>A few points:</p>
<ul>
<li>This style of writing <code class="highlighter-rouge">main (int k, char* *l)</code> as <code class="highlighter-rouge">main(k , l) char* * l;</code> came around because of <a href="https://en.wikipedia.org/wiki/The_C_Programming_Language">this book</a>.</li>
</ul>
<p class="faded">Yes, this book and C, for that matter, is that old.</p>
<p>Anyways, we need to modernize it.</p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">int</span> <span class="n">a</span><span class="p">[</span><span class="mi">900</span><span class="p">],</span><span class="n">b</span><span class="p">,</span><span class="n">c</span><span class="p">,</span><span class="n">d</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span><span class="n">e</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span><span class="n">f</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="n">h</span><span class="p">,</span><span class="n">O</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">k</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="o">*</span><span class="n">l</span><span class="p">){</span>
<span class="n">g</span> <span class="o">=</span> <span class="n">atoi</span><span class="p">(</span> <span class="o">*</span> <span class="o">++</span><span class="n">l</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="n">k</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">k</span> <span class="o">*</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="n">g</span><span class="p">;</span> <span class="n">b</span> <span class="o">=</span> <span class="n">k</span><span class="o">++</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="n">h</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">h</span> <span class="o">*</span> <span class="n">h</span> <span class="o">&lt;=</span> <span class="n">g</span><span class="p">;</span> <span class="o">++</span><span class="n">h</span><span class="p">);</span>
<span class="o">--</span><span class="n">h</span><span class="p">;</span>
<span class="n">c</span> <span class="o">=</span> <span class="p">((</span><span class="n">h</span> <span class="o">+=</span> <span class="n">g</span> <span class="o">&gt;</span> <span class="n">h</span> <span class="o">*</span> <span class="p">(</span><span class="n">h</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">d</span> <span class="o">&lt;=</span> <span class="n">g</span><span class="p">)</span> <span class="p">{</span>
<span class="o">++</span><span class="n">O</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">f</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">f</span> <span class="o">&lt;</span> <span class="n">O</span> <span class="o">&amp;&amp;</span> <span class="n">d</span> <span class="o">&lt;=</span> <span class="n">g</span><span class="p">;</span> <span class="o">++</span><span class="n">f</span><span class="p">)</span> <span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="n">d</span><span class="o">++</span><span class="p">,</span> <span class="n">b</span> <span class="o">+=</span> <span class="n">e</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">f</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">f</span> <span class="o">&lt;</span> <span class="n">O</span> <span class="o">&amp;&amp;</span> <span class="n">d</span> <span class="o">&lt;=</span> <span class="n">g</span><span class="p">;</span> <span class="o">++</span><span class="n">f</span><span class="p">)</span> <span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]</span> <span class="o">=</span> <span class="n">d</span><span class="o">++</span><span class="p">,</span> <span class="n">c</span> <span class="o">+=</span> <span class="n">e</span><span class="p">;</span>
<span class="n">e</span> <span class="o">=</span> <span class="o">-</span><span class="n">e</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">for</span> <span class="p">(</span><span class="n">c</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">c</span> <span class="o">&lt;</span> <span class="n">h</span><span class="p">;</span> <span class="o">++</span><span class="n">c</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span><span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">b</span> <span class="o">&lt;</span> <span class="n">k</span><span class="p">;</span> <span class="o">++</span>
<span class="n">b</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">b</span> <span class="o">&lt;</span> <span class="n">k</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span> <span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]</span> <span class="o">^=</span> <span class="n">a</span><span class="p">[(</span><span class="n">k</span> <span class="o">-</span> <span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]</span> <span class="o">^=</span> <span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]</span> <span class="o">^=</span> <span class="n">a</span><span class="p">[(</span><span class="n">k</span> <span class="o">-</span> <span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">];</span>
<span class="n">printf</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]</span> <span class="o">?</span> <span class="s">"%-4d"</span> <span class="o">:</span> <span class="s">" "</span><span class="p">,</span> <span class="n">a</span><span class="p">[</span><span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="mi">5</span> <span class="o">|</span> <span class="n">c</span><span class="p">]);</span>
<span class="p">}</span>
<span class="n">putchar</span><span class="p">(</span><span class="sc">'\n'</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span> <span class="cm">/*Mike Laman*/</span>
</code></pre></div></div>
<p>I do not know what else to do.
Discuss about it in the comments! Hopefully, I’ll gain some insight (I very well understand this code, but I don’t think there is anything else that I can do to simplify it).</p>
<p>This might be one of those <em>very interesting</em> tricks you can do with code.</p>
<p>Anyways, the comment section is open for discussion.</p>
<h2 id="the-copyright">The copyright</h2>
<p>Copyright © 1984, Landon Curt Noll. All Rights Reserved. Permission for personal, educational or non-profit use is granted provided this this copyright and notice are included in its entirety and remains unaltered. All other uses must receive prior permission in writing from both Landon Curt Noll and Larry Bassel.</p></content><author><name>Nischay Hegde</name><email>[email protected]</email></author><category term="IOCCC" /><summary type="html"></summary></entry><entry><title type="html">IOCCC Problems - Anonymous.c (1984)</title><link href="http://localhost:4000/cplusplusstudent/c/2017/11/14/IOCCC-Anonymous/" rel="alternate" type="text/html" title="IOCCC Problems - Anonymous.c (1984)" /><published>2017-11-14T00:30:00+05:30</published><updated>2017-11-14T00:30:00+05:30</updated><id>http://localhost:4000/cplusplusstudent/c/2017/11/14/IOCCC-Anonymous</id><content type="html" xml:base="http://localhost:4000/cplusplusstudent/c/2017/11/14/IOCCC-Anonymous/"><ul id="markdown-toc">
<li><a href="#introduction" id="markdown-toc-introduction">Introduction</a></li>
<li><a href="#the-original-code" id="markdown-toc-the-original-code">The Original Code</a></li>
<li><a href="#finished-code" id="markdown-toc-finished-code">Finished Code</a></li>
<li><a href="#the-copyright" id="markdown-toc-the-copyright">The copyright</a></li>
</ul>
<h2 id="introduction">Introduction</h2>
<p>IOCCC is a contest where you get to see exactly the opposite of what you’re expected to do in your day to day life: write clear code.
The code submitted here is sometimes overwhelming, and I’ll try to make sense of it.</p>
<h2 id="the-original-code">The Original Code</h2>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>int i;
main()
{
for(;i["]&lt;i;++i)
{--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
</code></pre></div></div>
<p>A couple of things here:</p>
<ul>
<li>using just <code class="highlighter-rouge">main()</code> was a valid thing until standardization came along. You can still use it: it is automatically assumed to be int.</li>
<li>int i is globally declared, and henceforth is equal to zero.</li>
</ul>
<p>Also, that <code class="highlighter-rouge">read()</code> function which has three parameters looks like it could be easily simplified:
<code class="highlighter-rouge">'-' - '-'</code> is equal to zero, since we’re actually doing a subtraction of two <code class="highlighter-rouge">char</code>s, and it is implicitly assigned to <code class="highlighter-rouge">int</code>.
Similarly <code class="highlighter-rouge">'/'/'/'</code> is 1.</p>
<p>So, if we rewrite it and introduce proper formatting, we have:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>int i;
int main()
{
for(;i["]&lt;i;++i){--i;}"];
read(0,i+++"hell\o, world!\n",1));
}
read(j,i,p)
{
write(j/p+p,i---j,i/i);
}
</code></pre></div></div>
<p>Also note that <code class="highlighter-rouge">i["]&lt;i;++i){--i;}"]</code> is actually a array of characters initialized as a string.</p>
<p>Sometimes, it’s good to check what’s happening under the hood to figure out the code. I’m going to do just that.
Running <code class="highlighter-rouge">ltrace</code> tells you what library calls were being used.
Running <code class="highlighter-rouge">ltrace</code> on <code class="highlighter-rouge">anonymous.c</code> reveals this:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>write(0, "h", 1h) = 1
write(0, "e", 1e) = 1
write(0, "l", 1l) = 1
write(0, "l", 1l) = 1
write(0, "o", 1o) = 1
write(0, ",", 1,) = 1
write(0, "w", 1w) = 1
write(0, "o", 1o) = 1
write(0, "r", 1r) = 1
write(0, "l", 1l) = 1
write(0, "d", 1d) = 1
write(0, "!", 1!) = 1
write(0, "\n", 1
) = 1
write(0, "", 1) = 1
write(0, "1", 11) = 1
+++ exited (status 0) +++
</code></pre></div></div>
<p><code class="highlighter-rouge">write()</code> is a Unix system call (you can find more about it by typing <code class="highlighter-rouge">man 2 write</code> on any linux system, and what it does is similar to a <code class="highlighter-rouge">printf</code> statement, i.e., write(0,”h”,1) is the same as typing printf(“h”), since 0 means <code class="highlighter-rouge">stdout</code> and 1 is just the size of the data.</p>
<p>A person on discord helped a lot to understand this code. I thank him here..</p>
<h2 id="finished-code">Finished Code</h2>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;sys/unistd.h&gt;
int i;
int main()
{
for(;i["1234567891011"];)
{
write(0,i+++"hell\o, world!\n",1);
}
}
</code></pre></div></div>
<h2 id="the-copyright">The copyright</h2>
<p>Copyright (c) 1984, Landon Curt Noll.
All Rights Reserved. Permission for personal, educational or non-profit use is
granted provided this this copyright and notice are included in its entirety
and remains unaltered. All other uses must receive prior permission in writing
from both Landon Curt Noll and Larry Bassel.</p></content><author><name>Nischay Hegde</name><email>[email protected]</email></author><category term="IOCCC" /><summary type="html"></summary></entry><entry><title type="html">Introduction to Algorithms - Chapter 2 (Part 2)</title><link href="http://localhost:4000/cplusplusstudent/study/notes/2017/10/17/Chap2Part2/" rel="alternate" type="text/html" title="Introduction to Algorithms - Chapter 2 (Part 2)" /><published>2017-10-17T00:30:00+05:30</published><updated>2017-10-17T00:30:00+05:30</updated><id>http://localhost:4000/cplusplusstudent/study/notes/2017/10/17/Chap2Part2</id><content type="html" xml:base="http://localhost:4000/cplusplusstudent/study/notes/2017/10/17/Chap2Part2/"><ul id="markdown-toc">
<li><a href="#analysis-of-algorithms" id="markdown-toc-analysis-of-algorithms">Analysis of Algorithms</a></li>
<li><a href="#analysis-of-insertion-sort" id="markdown-toc-analysis-of-insertion-sort">Analysis of Insertion Sort</a> <ul>
<li><a href="#finding-time-taken-in-best-case" id="markdown-toc-finding-time-taken-in-best-case">Finding Time Taken In Best Case</a></li>
<li><a href="#finding-time-taken-in-worst-case" id="markdown-toc-finding-time-taken-in-worst-case">Finding Time Taken In Worst Case</a></li>
</ul>
</li>
<li><a href="#divide-and-conquer-principle" id="markdown-toc-divide-and-conquer-principle">Divide And Conquer Principle</a> <ul>
<li><a href="#merge-sort" id="markdown-toc-merge-sort">Merge Sort</a></li>
</ul>
</li>
</ul>
<h2 id="analysis-of-algorithms">Analysis of Algorithms</h2>
<p><em>Analyzing</em> an algorithm normally means predicting the <em>resources</em> that the algorithm requires.</p>
<blockquote>
<p>Occasionally, resources such as memory, communication band-
width, or computer hardware are of primary concern, but most often it is compu-
tational time that we want to measure.</p>
</blockquote>
<p>Okay, let’s do this!</p>
<p>(We assume here that the technology used for these algorithms here utilizes a generic one-processor, RAM (Random-Access Machine) model of computation)</p>
<h2 id="analysis-of-insertion-sort">Analysis of Insertion Sort</h2>
<p>We can say that the time taken by the Insertion Sort Algorithm must be proportional to the time taken, since sorting 1000 numbers is more tedious than sorting 3 numbers.</p>
<p>Also, some implementations of insertion sort can be faster than the rest.</p>
<blockquote>
<p>In general, since the time taken by an algorithm grows with the size of the input, it is traditional to describe the running time of a program as a function of the size of its input.</p>
</blockquote>
<blockquote>
<p>Therefore, we need to define “running time” and “size of input” more carefully.</p>
</blockquote>
<blockquote>
<p>The best notion for input size depends on the problem being studied. For many problems, such as sorting or computing discrete Fourier transforms, the most natural measure is the number of items in the input — for example, the array size n for sorting. For many other problems, such as multiplying two integers, the best measure of input size is the total number of bits needed to represent the input in ordinary binary notation. Sometimes, it is more appropriate to describe the size of the input with two numbers rather than one. For instance, if the input to an algorithm is a graph, the input size can be described by the numbers of vertices and edges in the graph. We shall indicate which input size measure is being used with
each problem we study.</p>
</blockquote>
<blockquote>
<p>The running time of an algorithm on a particular input is the number of primitive operations or “steps” executed. It is convenient to define the notion of step so that it is as machine-independent as possible. For the moment, let us adopt the following view. A constant amount of time is required to execute each line of our
pseudocode. One line may take a different amount of time than another line, but we shall assume that each execution of the <code class="MathJax_Preview">{i}^{th}</code><script type="math/tex">{i}^{th}</script> line takes time <code class="MathJax_Preview">c_i</code><script type="math/tex">c_i</script> , where <code class="MathJax_Preview">c_i</code><script type="math/tex">c_i</script> is a constant. This viewpoint is in keeping with the RAM model, and it also reflects how the pseudocode would be implemented on most actual computers.</p>
</blockquote>
<blockquote>
<p>In the following discussion, our expression for the running time of Insertion Sort will evolve from a messy formula that uses all the statement costs <code class="MathJax_Preview">c_i</code><script type="math/tex">c_i</script> to a
much simpler notation that is more concise and more easily manipulated. This simpler notation will also make it easy to determine whether one algorithm is more
efficient than another.</p>
</blockquote>
<blockquote>
<p>We start by presenting the Insertion Sort procedure with the time “cost” of each statement and the number of times each statement is executed. For each
<code class="MathJax_Preview">j = 2,3,...,n</code><script type="math/tex">j = 2,3,...,n</script>, where n = <em>A:length</em>, we let <code class="MathJax_Preview">t_j</code><script type="math/tex">t_j</script> denote the number of times the while loop test in line 5 is executed for that value of j . When a for or while loop exits in the usual way (i.e., due to the test in the loop header), the test is executed
one time more than the loop body. We assume that comments are not executable statements, and so they take no time.</p>
</blockquote>
<p><img src="/cplusplusstudent/assets/Chap2Part2/1.png" alt="Insertion_sort_cost" /></p>
<p>The total running time is the sum of running times for each statement executed. Therefore, the total time taken is:</p>
<pre class="MathJax_Preview"><code>T(n) = c_1n + c_2(n-1) + c_4(n - 1) + c_5\sum_{j=2}^{n} (t_j-1) + c_7\sum_{j=2}^{n}(t_j-1) + c_8(n-1)</code></pre>
<script type="math/tex; mode=display">T(n) = c_1n + c_2(n-1) + c_4(n - 1) + c_5\sum_{j=2}^{n} (t_j-1) + c_7\sum_{j=2}^{n}(t_j-1) + c_8(n-1)</script>
<p>So let’s find the best and worst cases, using which we will gain some insight as to how this algorithm works.</p>
<h3 id="finding-time-taken-in-best-case">Finding Time Taken In Best Case</h3>
<p>The best case in Insertion Sort obviously happens when the array is already sorted.</p>
<p>For all of <code class="MathJax_Preview">j = 2,3, \ldots,n</code><script type="math/tex">j = 2,3, \ldots,n</script>, we find that <code class="MathJax_Preview">A[i]\leq key $ in line 5 of the algorithm when i has an initial value of j - 1. Thus,</code><script type="math/tex">A[i]\leq key $ in line 5 of the algorithm when i has an initial value of j - 1. Thus,</script> t_j = 1 <code class="MathJax_Preview">for</code><script type="math/tex">for</script> j = 2,3,/ldots,n $$ and the best-case running time is:</p>
<pre class="MathJax_Preview"><code>T(n) = c_1n + c_2(n-1) + c_4(n-1) + c_5(n-1) + c_8(n-1)
= (c_1 + c_2 + c_4 + c_5 + c_8)n - (c_2 + c_4 + c_5 + c_8)</code></pre>
<script type="math/tex; mode=display">T(n) = c_1n + c_2(n-1) + c_4(n-1) + c_5(n-1) + c_8(n-1)
= (c_1 + c_2 + c_4 + c_5 + c_8)n - (c_2 + c_4 + c_5 + c_8)</script>
<p>Since we can express it as <code class="MathJax_Preview">an + b</code><script type="math/tex">an + b</script> for constants a and b, it is a <strong>linear function</strong> of n.</p>
<h3 id="finding-time-taken-in-worst-case">Finding Time Taken In Worst Case</h3>
<p>The worst time happens when the summation terms are at their maximum.</p>
<p>Remembering that the summation terms can be simplified to:</p>
<pre class="MathJax_Preview"><code>\sum_{j=2}^{n} j = \dfrac{n(n+1)}{2} - 1</code></pre>
<script type="math/tex; mode=display">\sum_{j=2}^{n} j = \dfrac{n(n+1)}{2} - 1</script>
<p>and</p>
<pre class="MathJax_Preview"><code>\sum_{j=2}^n (j-1) = \dfrac{n(n-1)}{2}</code></pre>
<script type="math/tex; mode=display">\sum_{j=2}^n (j-1) = \dfrac{n(n-1)}{2}</script>
<p>We get:</p>
<pre class="MathJax_Preview"><code>T(n) = c_1n + c_2(n-1) + c_4(n-1) + c_5 \left(\dfrac{n(n+1)}{2} - 1\right) + c_6 \left(\dfrac{n(n-1)}{2}\right) + c_7 \left(\dfrac{n(n-1)}{2}\right) + c_8(n-1)</code></pre>
<script type="math/tex; mode=display">T(n) = c_1n + c_2(n-1) + c_4(n-1) + c_5 \left(\dfrac{n(n+1)}{2} - 1\right) + c_6 \left(\dfrac{n(n-1)}{2}\right) + c_7 \left(\dfrac{n(n-1)}{2}\right) + c_8(n-1)</script>
<pre class="MathJax_Preview"><code>T(n) = \left(\dfrac{c_5}{2} + \dfrac{c_6}{2} + \dfrac{c_7}{2}\right)n^2 + \left(c_1 + c_2 + c_4 + \dfrac{c_5}{2} - \dfrac{c_6}{2} - \dfrac{c_7}{2} + c_8\right)n - (c_2 + c_4 + c_5 + c_8)</code></pre>
<script type="math/tex; mode=display">T(n) = \left(\dfrac{c_5}{2} + \dfrac{c_6}{2} + \dfrac{c_7}{2}\right)n^2 + \left(c_1 + c_2 + c_4 + \dfrac{c_5}{2} - \dfrac{c_6}{2} - \dfrac{c_7}{2} + c_8\right)n - (c_2 + c_4 + c_5 + c_8)</script>
<p>We can express <em>this</em> time as <code class="MathJax_Preview">an^2 + bn + c</code><script type="math/tex">an^2 + bn + c</script> for constants a, b, and c.</p>
<p>Therefore, it is a <strong>quadratic function</strong> of n.</p>
<h2 id="divide-and-conquer-principle">Divide And Conquer Principle</h2>
<p>This principle is used for problems which have subproblems that can be easily solved.</p>
<p>For example, if this sorting algorithm were to be fed only two elements at a time, it would be easy, right?</p>
<p>Therefore, if we can break down a problem to small, easily solvable chunks, solving the problem becomes easier. Just make the real problem by solving all the bits and pieces!</p>
<p>This is a complex skill, worth it’s own page (which I will give), but let’s see it in action first.</p>
<h3 id="merge-sort">Merge Sort</h3>
<p>This is the code for <a href="http://cplusplus.happycodings.com/algorithms/code17.html">mergesort</a>:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>// Merge Sort
#include &lt;iostream&gt;
using namespace std;
int a[50];
void merge(int,int,int);
void merge_sort(int low,int high)
{
if(low&lt;high)
{
int mid = low + (high-low)/2; //This avoids overflow when low, high are too large
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int h,i,j,b[50],k;
h=low;
i=low;
j=mid+1;
while((h&lt;=mid)&amp;&amp;(j&lt;=high))
{
if(a[h]&lt;=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h&gt;mid)
{
for(k=j;k&lt;=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k&lt;=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k&lt;=high;k++) a[k]=b[k];
}
int main()
{
int num,i;
cout&lt;&lt;"Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN
PRESS
ENTER]:"&lt;&lt;endl;
cin&gt;&gt;num;
cout&lt;&lt;"\n Now, Please Enter the ( "&lt;&lt; num &lt;&lt;" ) numbers (ELEMENTS) [THEN
PRESS ENTER]:"&lt;&lt;endl;
for(i=1;i&lt;=num;i++)
{
cin&gt;&gt;a[i] ;
}
merge_sort(1,num);
cout&lt;&lt;endl;
cout&lt;&lt;"So, the sorted list (using MERGE SORT) will be :"&lt;&lt;endl;
cout&lt;&lt;endl&lt;&lt;endl;
for(i=1;i&lt;=num;i++)
cout&lt;&lt;a[i]&lt;&lt;" ";
}
</code></pre></div></div>
<p>Try to understand it.
I’ll explain it in the next post.</p></content><author><name>Nischay Hegde</name><email>[email protected]</email></author><category term="Algorithms" /><summary type="html"></summary></entry><entry><title type="html">Introduction to Algorithms - Solutions - Chapter 2 (Part 1)</title><link href="http://localhost:4000/cplusplusstudent/study/notes/2017/10/16/Chap2Sol/" rel="alternate" type="text/html" title="Introduction to Algorithms - Solutions - Chapter 2 (Part 1)" /><published>2017-10-16T02:00:00+05:30</published><updated>2017-10-16T02:00:00+05:30</updated><id>http://localhost:4000/cplusplusstudent/study/notes/2017/10/16/Chap2Sol</id><content type="html" xml:base="http://localhost:4000/cplusplusstudent/study/notes/2017/10/16/Chap2Sol/"><ul id="markdown-toc">
<li><a href="#the-insertion-sort-c-program" id="markdown-toc-the-insertion-sort-c-program">The Insertion Sort C++ Program</a></li>
<li><a href="#the-bubble-sort-problem" id="markdown-toc-the-bubble-sort-problem">The Bubble Sort Problem</a> <ul>
<li><a href="#proving-the-correctness-of-bubble-sort" id="markdown-toc-proving-the-correctness-of-bubble-sort">Proving the Correctness of Bubble Sort</a></li>
</ul>
</li>
</ul>
<h2 id="the-insertion-sort-c-program">The Insertion Sort C++ Program</h2>
<p>This is the program:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;iostream&gt;
using namespace std;
int main()
{
int N; //Here, N is the size of the array
int input_arr[N]; //input_arr is the array we use to store the input
for(int i=0; i&lt;N;i++)
cin&gt;&gt;input_arr[i];
for(int j=1;j&lt;N;j++)
{
int key=input_arr[j];
int i=j-1;
while(i!&lt;0&amp;&amp;input_arr[i]&gt;key) //Here, we have to include the case when i becomes zero, becuase it does
{
input_arr[i+1]=input_arr[i];
i--;
}
input_arr[i+1]=key;
}
for(int i=0;i&lt;N;i++)
cout&lt;&lt;input_arr[i]&lt;&lt;"\n";
return 0;
}
</code></pre></div></div>
<h2 id="the-bubble-sort-problem">The Bubble Sort Problem</h2>
<p>I wrote the program for bubble sort, so that it’d be easier to understand. Here it is:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include&lt;iostream&gt;
#include&lt;algorithm&gt;
using namespace std;
int main()
{
int N; //Same as the earlier one
cin&gt;&gt;N;
int input_arr[N];
for(int i=0; i&lt;N;i++)
cin&gt;&gt;input_arr[i];
for(int j=1;j&lt;N;j++)
{
for(int i=0;i&lt;N;i++)
{
if(input_arr[i]&gt;input_arr[j])
swap(input_arr[i],input_arr[j]);
}
}
for(int i=0;i&lt;N;i++)
cout&lt;&lt;input_arr[i]&lt;&lt;"\n";
return 0;
}
</code></pre></div></div>
<h3 id="proving-the-correctness-of-bubble-sort">Proving the Correctness of Bubble Sort</h3>
<p>Initialisation: At the start, <code class="highlighter-rouge">j=1</code>, which means <code class="highlighter-rouge">input_arr[1...j+1-1]</code> contains 1 element. Therefore, it is necessarily sorted.</p>
<p>Maintenance: At each loop of j, we get a sorted array from <code class="highlighter-rouge">input_arr[1...j+1-1]</code>. Therefore, it is sorted.</p>
<p>Termination: In the final loop of j, we get a sorted array from <code class="highlighter-rouge">input_arr[1...N+1-1]</code>. Therefore, it is sorted. (Remember that <code class="highlighter-rouge">N</code> in the code implies <code class="highlighter-rouge">N+1</code> in the pseudocode, when you are dealing with array indices).</p>
<p>Hence, the given algorithm is correct.</p></content><author><name>Nischay Hegde</name><email>[email protected]</email></author><category term="Algorithms" /><summary type="html"></summary></entry><entry><title type="html">Introduction to Algorithms - Chapter 2 (Part 1)</title><link href="http://localhost:4000/cplusplusstudent/study/notes/2017/10/15/Chap2/" rel="alternate" type="text/html" title="Introduction to Algorithms - Chapter 2 (Part 1)" /><published>2017-10-15T10:30:00+05:30</published><updated>2017-10-15T10:30:00+05:30</updated><id>http://localhost:4000/cplusplusstudent/study/notes/2017/10/15/Chap2</id><content type="html" xml:base="http://localhost:4000/cplusplusstudent/study/notes/2017/10/15/Chap2/"><ul id="markdown-toc">
<li><a href="#introduction" id="markdown-toc-introduction">Introduction</a> <ul>
<li><a href="#pseudocode" id="markdown-toc-pseudocode">Pseudocode</a></li>
<li><a href="#insertion-sort" id="markdown-toc-insertion-sort">Insertion Sort</a></li>
</ul>
</li>
<li><a href="#correctness-of-insertion-sort" id="markdown-toc-correctness-of-insertion-sort">Correctness of Insertion Sort</a></li>
<li><a href="#interesting-problemsalgorithms" id="markdown-toc-interesting-problemsalgorithms">Interesting Problems/Algorithms</a></li>
<li><a href="#more-to-come" id="markdown-toc-more-to-come">More to come!</a></li>
</ul>
<h2 id="introduction">Introduction</h2>
<p>Let’s focus on the first problem introduced in the previous chapter, the sorting problem.</p>
<p>Let us examine an algorithm to solve this problem, called insertion sort. We then take the help of this algorithm to lay the framework of analysis of other algorithms.</p>
<p>But before that, we need to know what <em>pseudocode</em> is.</p>
<h3 id="pseudocode">Pseudocode</h3>
<p>All algorithms in this book are expressed in something called <em>pseudocode</em>. It is similar in many respects to C++ (Or C/Pascal if you’ve seen them, all of these language share similar syntax), the only difference being that sometimes the authors might swap actual code with English sentences to be more concise.</p>
<p>One exception (that you will probably find in the pseudocode for insertion sort) is that when you write <code class="highlighter-rouge">A[j]</code> in this language, it means “The <code class="MathJax_Preview">j^{th}</code><script type="math/tex">j^{th}</script> element of the array A”, when in C/C++ it means “The <code class="MathJax_Preview">{j-1}^{th}</code><script type="math/tex">{j-1}^{th}</script> element of the array A”.</p>
<p>Since now we have the utmost basic stuff down, let’s get to the first algorithm.</p>
<h3 id="insertion-sort">Insertion Sort</h3>
<p>Input: A sequence of n numbers <code class="MathJax_Preview">\langle a_1, a_2, a_3, \ldots, a_n \rangle</code><script type="math/tex">\langle a_1, a_2, a_3, \ldots, a_n \rangle</script>
Output: A permutation (reordering) <code class="MathJax_Preview">\langle a'_1, a'_2, a'_3, \ldots, a'_n \rangle</code><script type="math/tex">\langle a'_1, a'_2, a'_3, \ldots, a'_n \rangle</script> of the input sequence such that <code class="MathJax_Preview">\langle a'_1 \leqslant a'_2 \leqslant a'_3 \leqslant \ldots a'_n \rangle</code><script type="math/tex">\langle a'_1 \leqslant a'_2 \leqslant a'_3 \leqslant \ldots a'_n \rangle</script>.</p>
<p>Now, you have an array of n elements, and an algorithm that takes an array of n elements.</p>
<p>Before checking what the pseudocode is, let’s check the wikipedia page for what it does. <a href="https://en.wikipedia.org/wiki/Insertion_sort">Insertion Sort</a></p>
<p>So it takes an element, and puts it in its place by checking it with other elements of the array. This is done for every element in the array.</p>
<p>The way it finds the place is by comparing the selected element of the array (called a key) with all the elements in the array.</p>
<p>Let’s check the pseudocode of the Insertion Sort algorithm:</p>
<p><img src="/cplusplusstudent/assets/CLRS2/insertion.png" alt="inserton" /></p>
<p>In this pseudocode, <code class="MathJax_Preview">A[j]</code><script type="math/tex">A[j]</script> is the <code class="MathJax_Preview">{j}^{th}</code><script type="math/tex">{j}^{th}</script> element of the array <code class="MathJax_Preview">A</code><script type="math/tex">A</script>.</p>
<p>Let’s break the algorithm down:</p>
<p>The first line introduces a <strong>for</strong> loop, implying that the algorithm is gonna hold from elements 2 to the length of A.</p>
<p>The second line initialises key to <code class="highlighter-rouge">A[j]</code>, which is initialsed to <code class="highlighter-rouge">A[2]</code> (again, keep in mind that <code class="highlighter-rouge">A[2]</code> in pseudocode is equivalent to <code class="highlighter-rouge">A[1]</code> in C/C++ code).</p>
<p>You might ask this question:</p>
<blockquote>
<p>But dude, wait, doesn’t that <em>mean</em> that this algorithm doesn’t look for the first element?</p>
</blockquote>
<p>Not exactly. The next few lines should convince you of it.</p>
<p>I’ll take the rest of this algorithm as one chunk of code (you’ll understand soon)</p>
<p>So, on the first try (or rather, <em>iteration</em>) of the algorithm, <code class="highlighter-rouge">i</code> becomes equal to <code class="highlighter-rouge">j-1</code>.</p>
<p>The next three lines just talk about how it is going to find the place for the key.</p>
<p>Once the place for it is found, <code class="highlighter-rouge">key</code> is placed in the element next to the place found, i.e., <code class="highlighter-rouge">i+1</code>.</p>
<p><strong>Exercise</strong>: Find out why key is placed head of <code class="highlighter-rouge">i</code> by one element.</p>
<p>To have a more-or-less informal analysis of an algorithm, we need to know about loop invariants.</p>
<h2 id="correctness-of-insertion-sort">Correctness of Insertion Sort</h2>
<p>For a quick-and-dirty version of its meaning, a loop invariant is something that retains its value over one iteration of the program, that is used for continuing the algorithm smoothly.</p>
<p>(Spoiler alert: The loop invariant for our implementation of insertion sort was <code class="highlighter-rouge">j</code>)
The loop invariant helps to find if an algorithm is correct, or not.</p>
<p>We must show three things about a loop invariant:</p>
<blockquote>
<p><strong>Initialization:</strong> It is true prior to the first iteration of the loop.</p>
</blockquote>
<blockquote>
<p><strong>Maintenance:</strong> If it is true before an iteration of the loop, it remains true before the
next iteration.</p>
</blockquote>
<blockquote>
<p><strong>Termination:</strong> When the loop terminates, the invariant gives us a useful property
that helps show that the algorithm is correct.</p>
</blockquote>
<p>Let’s see how this holds for insertion sort:</p>
<p>Initialization: Here, <code class="highlighter-rouge">j=2</code>, so the subarray <code class="highlighter-rouge">A[1...j-1]</code>, therefore consists of A[1], and therefore, it is sorted.</p>
<p>Therefore, the loop invariant holds good at initialization.</p>
<p>Maintenance: This algorithm involves taking elements like <code class="highlighter-rouge">A[j-1]</code>,<code class="highlighter-rouge">A[j-2]</code>,<code class="highlighter-rouge">A[j-3]</code> etc. and moving them one by one till the element behind it is smaller than it. Therefore, <code class="highlighter-rouge">A[1...j]</code>, where <code class="highlighter-rouge">j</code> is the place it the element gets placed is sorted.</p>
<p>(This part normally requires a good amount of math, but I am going to skip it, since this is an informal way to show correctness).</p>
<p>Termination: The condition that makes this algorithm exit is <code class="highlighter-rouge">j=A.length</code>, which means that <code class="highlighter-rouge">A[1...A.length]</code> is sorted by the time this algorithm is over. <em>That</em> is the whole algortihm.</p>
<h2 id="interesting-problemsalgorithms">Interesting Problems/Algorithms</h2>
<ul>
<li>Prove the same for <a href="https://en.wikipedia.org/wiki/Bubble_sort">Bubblesort</a></li>
</ul>
<h2 id="more-to-come">More to come!</h2>
<p>I will be posting the code of insertion sort, as well as the exercise solutions in a separate post tomorrow. Till then, try them and keep tuned!</p></content><author><name>Nischay Hegde</name><email>[email protected]</email></author><category term="Algorithms" /><summary type="html"></summary></entry><entry><title type="html">Introduction to Algorithms - Chapter 1</title><link href="http://localhost:4000/cplusplusstudent/study/notes/2017/10/12/CLRSChap1/" rel="alternate" type="text/html" title="Introduction to Algorithms - Chapter 1" /><published>2017-10-12T05:30:00+05:30</published><updated>2017-10-12T05:30:00+05:30</updated><id>http://localhost:4000/cplusplusstudent/study/notes/2017/10/12/CLRSChap1</id><content type="html" xml:base="http://localhost:4000/cplusplusstudent/study/notes/2017/10/12/CLRSChap1/"><ul id="markdown-toc">
<li><a href="#introduction" id="markdown-toc-introduction">Introduction</a></li>
<li><a href="#so-what-are-algorithms" id="markdown-toc-so-what-are-algorithms">So, What are Algorithms?</a></li>
<li><a href="#interesting-algorithms--problems" id="markdown-toc-interesting-algorithms--problems">Interesting Algorithms / Problems</a> <ul>
<li><a href="#shortest-paths" id="markdown-toc-shortest-paths">Shortest Paths</a></li>
</ul>
</li>
</ul>
<h2 id="introduction">Introduction</h2>
<p>I will be posting my study notes that I made while reading the book <a href="https://mitpress.mit.edu/books/introduction-algorithms">Introduction to Algorithms</a>. I hope it’ll be just as good as reading the original book. There’ll be quotations from the book, but more often than not, I’ll be leaving some ideas of my own, as blatantly copying parts of a book doesn’t make good notes.</p>
<p>If there is any question you would want to ask, just comment about it in the comments section below.</p>
<h2 id="so-what-are-algorithms">So, What are Algorithms?</h2>
<p>We have seen programs. They take input, do some work on it, and then give out output. We could call the program that does this an <em>algorithm</em>, if it does the same thing for all inputs given, and gives the right output.</p>
<p>Or, in the words of the authors themselves:</p>
<blockquote>
<p>An algorithm is any well-defined computational procedure that takes
some value, or set of values, as input and produces some value, or set of values, as
output. An algorithm is thus a sequence of computational steps that transform the
input into the output.</p>
</blockquote>
<p>Let’s take a sorting algorithm as an example, as the authors themselves have:</p>
<p>The input would be a set of numbers <code class="MathJax_Preview">\langle a_1 , a_2 , a_3 , \ldots , a_n \rangle</code><script type="math/tex">\langle a_1 , a_2 , a_3 , \ldots , a_n \rangle</script> and the output is some permutation of the same, i.e., another set of the same n numbers <code class="MathJax_Preview">\langle a'_1 , a'_2 , a'_3 , \ldots a_n \rangle</code><script type="math/tex">\langle a'_1 , a'_2 , a'_3 , \ldots a_n \rangle</script></p>
<p>An algorithm, in this case would be the steps required to get the output from the input.</p>
<h2 id="interesting-algorithms--problems">Interesting Algorithms / Problems</h2>
<p>Interesting Algorithms will be a section that will occur frequently in this series, and it will list all the Algorithms listed in this chapter that I found interesting,</p>
<p>In this chapter, we have the Shortest Paths problem.</p>
<h3 id="shortest-paths">Shortest Paths</h3>
<p>This Problem, as the title says, is to find the shortest path from a place A, to another place B. I’m sure you would be able to think of lots of applications of this. In case you aren’t sure, you can think of the problem where there are multiple ways to get to a destination and you don’t really know how to get there in the least amount of distance. Since, on road, least distance usually means least time, you will be able to get there faster.</p>
<p>So, you go to <a href="https://www.google.com/maps">Google Maps</a>, or some other mapping software and find the shortest route. The way these maps find the distance is proprietary, but it is usually some form of a shortest paths algorithm, and is usually a form of Dijkstra, or the A* algorithm (both of which will be discussed later).</p></content><author><name>Nischay Hegde</name><email>[email protected]</email></author><category term="Algorithms" /><summary type="html"></summary></entry><entry><title type="html">An anti-spam service called Boxbe</title><link href="http://localhost:4000/cplusplusstudent/interesting/2017/10/11/boxbe/" rel="alternate" type="text/html" title="An anti-spam service called Boxbe" /><published>2017-10-11T00:30:00+05:30</published><updated>2017-10-11T00:30:00+05:30</updated><id>http://localhost:4000/cplusplusstudent/interesting/2017/10/11/boxbe</id><content type="html" xml:base="http://localhost:4000/cplusplusstudent/interesting/2017/10/11/boxbe/"><ul id="markdown-toc">
<li><a href="#how-it-all-started" id="markdown-toc-how-it-all-started">How it all started</a></li>
<li><a href="#what-i-did" id="markdown-toc-what-i-did">What I did</a></li>
</ul>
<h2 id="how-it-all-started">How it all started</h2>
<p>So I found an email in my Dad’s inbox. My mom wanted me to check it out. I found this:</p>
<p><img src="/cplusplusstudent/assets/boxbe/1.png" alt="boxbe" /></p>
<p>I just thought that an anti-spam service at this day and age was unnecessary, as almost all email providers have anti-spam features.</p>
<p>In a nutshell, the way this anti-spam filter works is pretty simple. You include a bunch of contacts to a “Waiting List”, and if any emails come to your inbox, your email client will notify you. If any other email comes to your inbox, it will be transferred to another folder, called “Boxbe Waiting List”, and if you want to see how much spam it has blocked, you can check that folder.</p>
<p>So it isn’t much of an anti-spam service, it just lets you select the emails you prioritize, and shows you those emails accordingly.</p>
<p>I checked the <a href="https://en.wikipedia.org/wiki/Boxbe">Wikipedia page</a>, and it seems that this service ended in 2012. That’s odd. How am I getting these emails, if the service itself ended in 2012?</p>
<p>Just out of curiosity, I looked at its Twitter Feed.</p>
<p><img src="/cplusplusstudent/assets/boxbe/2.png" alt="boxbe-twitter" /></p>
<p>Yes, the last time it tweeted was in 2009.</p>
<p>And, according to its <a href="https://en.wikipedia.org/wiki/Boxbe">Wikipedia page</a>, it is supposed to have been acquired by another company.</p>
<p>But then, I can’t say how much of that is true.</p>
<p>I have seen this service to be working (as of 10th October, 2017) and I hope this article will sort it out for anybody wanting to use this as a source (the Wikipedia page has multiple problems with it).</p>
<p>Anyways, I only made this article because some people on Twitter complained to <a href="https://twitter.com/boxbe">boxbe</a> that they couldn’t delete their Boxbe account.</p>
<h2 id="what-i-did">What I did</h2>
<p>Therefore, I made a dummy AOL Mail account and a dummy ProtonMail account in order to perform a check on whether this was true.</p>
<p><img src="/cplusplusstudent/assets/boxbe/3.png" alt="boxbe_invites" />
A part of the webapp no one likes (at least according to twitter).</p>
<p><img src="/cplusplusstudent/assets/boxbe/3_2.png" alt="boxbe_signup" />
The sign-up notification.</p>
<table>
<thead>
<tr>
<th>Before</th>
<th>After</th>
</tr>
</thead>
<tbody>
<tr>
<td><img src="/cplusplusstudent/assets/boxbe/4.png" alt="boxbe_dashboard" /></td>
<td><img src="/cplusplusstudent/assets/boxbe/5.png" alt="boxbe_after" /></td>
</tr>
</tbody>
</table>
<p>The Main dashboard before and after disabling it.</p>
<p>And to check that it is truly not working anymore, I sent a test message through ProtonMail to the AOL Mail account I had registered boxbe with.</p>
<p><img src="/cplusplusstudent/assets/boxbe/6.png" alt="proton_mail" /></p>
<p><img src="/cplusplusstudent/assets/boxbe/7.png" alt="where_it_came" /></p>
<p>As you can see, it has appeared in the Spam Folder, something that all ProtonMail emails appear in AOL Mail. So we know for sure that Boxbe was truly removed.</p>
<p>And now, I can finally go back to learning CS.</p></content><author><name>Nischay Hegde</name><email>[email protected]</email></author><category term="unrelated" /><summary type="html"></summary></entry><entry><title type="html">Operators and the end of this tutorial</title><link href="http://localhost:4000/cplusplusstudent/cpp-tutorial/2017/09/22/operators/" rel="alternate" type="text/html" title="Operators and the end of this tutorial" /><published>2017-09-22T05:30:00+05:30</published><updated>2017-09-22T05:30:00+05:30</updated><id>http://localhost:4000/cplusplusstudent/cpp-tutorial/2017/09/22/operators</id><content type="html" xml:base="http://localhost:4000/cplusplusstudent/cpp-tutorial/2017/09/22/operators/"><ul id="markdown-toc">
<li><a href="#operators" id="markdown-toc-operators">Operators</a></li>
<li><a href="#constants" id="markdown-toc-constants">Constants</a></li>
<li><a href="#library" id="markdown-toc-library">Library</a></li>
</ul>
<h2 id="operators">Operators</h2>
<p>Operators are used to do various types of operations with expressions. An <strong>expression</strong> can be thought of as being manipulations of variables.
Check out more on operators in <a href="https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Table">this</a> Wikipedia page.</p>
<h2 id="constants">Constants</h2>
<p>Constants are variables whose value when once initialised, cannot be changed afterwards.
They can be written like this: <code class="highlighter-rouge">const &lt;data_type&gt; &lt;variable_name&gt; = &lt;initialised_value&gt;;</code>
They can be used almost everywhere a normal constant is used. (But you have to be very careful about it and there is a proper syntax. I might do it in a later tutorial series.</p>
<h2 id="library">Library</h2>
<p>A library is a collection of classes and functions which can be used to make programs faster, something like the modules of Python. The main library of C++ is called the STL, or the Standard Template Library. It contains a lot of classes and functions, and almost all C++ learners will eventually learn it. It is also a work in progrss, constantly updating itself (The latest update was in 2017).</p>
<p>This is the end of “A Beginner’s Guide to C++”. I will still try to post new information every day, but it will probably be problems, becuase that’s what I have been doing now.</p></content><author><name>Nischay Hegde</name><email>[email protected]</email></author><category term="C++" /><summary type="html"></summary></entry><entry><title type="html">Sneek Peek: Vectors</title><link href="http://localhost:4000/cplusplusstudent/cpp-tutorial/2017/09/21/vectors/" rel="alternate" type="text/html" title="Sneek Peek: Vectors" /><published>2017-09-21T06:30:00+05:30</published><updated>2017-09-21T06:30:00+05:30</updated><id>http://localhost:4000/cplusplusstudent/cpp-tutorial/2017/09/21/vectors</id><content type="html" xml:base="http://localhost:4000/cplusplusstudent/cpp-tutorial/2017/09/21/vectors/"><ul id="markdown-toc">
<li><a href="#introduction" id="markdown-toc-introduction">Introduction</a></li>
<li><a href="#declaring-and-initialisation" id="markdown-toc-declaring-and-initialisation">Declaring and Initialisation</a></li>
<li><a href="#key-concept-vectors-grow-dynamically" id="markdown-toc-key-concept-vectors-grow-dynamically">Key Concept: <code class="highlighter-rouge">vector</code>s Grow Dynamically</a></li>
<li><a href="#footnotes" id="markdown-toc-footnotes">Footnotes</a></li>
</ul>
<p>For the past few days, I have been trying to finish the 11th grade portion of C++ in my tutorials and move onto some, more interesting (or rather, more advanced) topics. Here’s one: <strong>Vectors</strong>.
I thought that it’d be best to bring the concept of vectors right after arrays, when the mind is fresh about that idea.
The main thing to think about a vector is that the size of a vector is <strong>variable</strong>. Most books (or, at least the one I refer to, an old version of C++ Primer) already teach vectors <em>before</em> teaching about arrays, which I feel isn’t for the best.
Arrays are more down-to-earth than vectors, and I believe that it would be in the best interest of everyone to learn it first and then come this way.</p>
<h2 id="introduction">Introduction</h2>
<blockquote>
<p>A vector is a collection of objects of a single type, each of which has an associated integer index. As with strings, the library takes care of managing the memory associated with storing the elements. We speak of a vector as a container because it contains other objects. All of the objects in a container must have the same type. - C++ Primer, 4th Edition.</p>
</blockquote>
<p>As I have said before and as you can see from that definition, a vector is a collection of objects (much like an array), but the main difference is that the vector data type (I’m really regretting this post) is <strong>variable</strong>. Which is to mean, that you have an array, but with variable size.</p>
<h2 id="declaring-and-initialisation">Declaring and Initialisation</h2>
<table>
<thead>
<tr>
<th>Syntax</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code class="highlighter-rouge">vector&lt;T&gt; v1;</code></td>
<td>vector that holds objects of type T;</td>
</tr>
<tr>
<td> </td>
<td>Default constructor v1 is empty</td>
</tr>
<tr>
<td><code class="highlighter-rouge">vector&lt;T&gt; v2(v1);</code></td>
<td>v2 is a copy of v1</td>
</tr>
<tr>
<td><code class="highlighter-rouge">vector&lt;T&gt; v3(n, i);</code></td>
<td>v3 has n elements with value i</td>
</tr>
<tr>
<td><code class="highlighter-rouge">vector&lt;T&gt; v4(n);</code></td>
<td>v4 has n copies of a value-initialized object</td>
</tr>
</tbody>
</table>
<h2 id="key-concept-vectors-grow-dynamically">Key Concept: <code class="highlighter-rouge">vector</code>s Grow Dynamically</h2>
<p>A central property of vectors is that they are required to be implemented so that it is efficient to add elements to them at run time. Because vectors grow efficiently, it is usually best to let the vector grow by adding elements to it dynamically as the element values are known.<a href="http://www.cppstudent.in/cpp-tutorial/2017/09/21/vectors/#Footnotes">¹</a></p>
<h2 id="footnotes">Footnotes</h2>
<p>This tutorial has excerpts from a book that I personally like very much, and it is <a href="https://www.amazon.com/Primer-4th-Stanley-B-Lippman/dp/0201721481/">this</a>.</p></content><author><name>Nischay Hegde</name><email>[email protected]</email></author><category term="C++" /><summary type="html"></summary></entry></feed>