Sunday, July 31, 2016

C++: what is the difference between passing a reference and passing a pointer to a function?

Both are same in the kind of functionality they provide, they allow you to modify a variable of calling function.

Pointers store variable location and need special operators to access the content. References, on the other hand, also point to the same location but with a symbolic variable ('alias' of the original symbolic variable).

When we call a function with the original variable, a new copy of the variable is created and passed to the function. The references are introduced exactly to bypass the copy stage and pass the symbolic variable directly to the function.


The reason for the existence of references is that they are slightly more user friendly than the pointers. We can have normal expressions (without '*') and yet have pointer functionality.

References are even used when we do not want pointer functionality, i.e., we do not want to to change the variable in the called function. This is done by declaring the reference variable 'const'. The advantage is to avoid copy of the large objects.  

Thursday, July 28, 2016

While delivering presentation: how do I look at Notes?

Answer: Look at your notes in a deliberate manner and let the students know that you are consulting them.

After you finish a slide, look at your notes in a deliberate manner, don't glance surreptitiously at the notes as if you're trying to make it seem that you're not looking at or consulting notes.

It may seem long to you, but student's do not mind it. In fact, they get a breather to grasp the things explained at the previous slide.

Friday, July 15, 2016

Algorithm Analysis: How to calculate coefficients of asymptotic notation?


An algorithm with running time $f(n)$ is said to be $\theta(g(n))$ if the following holds:

$c_1g(n) \leq f(n) \leq c_2g(n)$ for all $n>n_0$ and some value of $c_1$ and $c_2$.

For example, $f(n)=\frac{n^2}{2}-3n$ is $\theta(n^n)$.

To prove this, we need to find at least one set of values for $n_0, c_1$ and $c_2$ that satisfies above inequality. Below is a simple procedure to do that:
  1. To calculate $c_1$, obtain a value of n for which $f(n)$ is positive and solve left side inequality with that value of $n$.
    • $n_0=7$ and $c_1\leq1/14$
  2.  To calculate $c_2$, just put $n=1$ and solve the right side inequality.
    • $n_0=1$ and $c_2=1/2$
The basic idea is to make sure that $c_1$ and $c_2$ are positive.




How to add LaTex (math symbols) on blogger.com?

To write equations on blogger.com using LaTex, you need to add a java script to the template. Follow the steps below to add the script.
  1. Click on the Blog (not Post) you want to add equations to.
  2. Find "Template" on right side panel and click it.
  3. Click on "Edit HTML".
  4. Search for <head> and paste the following text just below <head>:
  5. Save it and add latex math code in-between two dollar signs ($\$$latex$\$$) as follows:
    • Einstein's most famous equation is $\$E=mc^2\$$
  6. It will appear as: 
    • Einstein's most famous equation is $E=mc^2$

Tuesday, August 18, 2015

How to configure XeLatex for US Letter (8.5x11 inches) pdf size?

Most conferences and journals only accept US Letter size formatted PDF. However, most latex compilers produce A4 size PDF by default. You need to explicitly tell the compiler to generate US Letter size.

XeLatex is one of those compilers that allow you to use both PDF and EPS figures. To configure XeLatex for US Letter size, follow this:

  • In whatever editor you are using, find the place where you provide arguments for XeLatex compiler. For texmaker, it is at "Preferences" or "Configure texmaker", depending on your version.
  • Add -papersize=letter to the arguments. The modified arguments should look something like this:
    • xelatex -synctex=1 -papersize=letter   -interaction=nonstopmode %.tex
  • Save and compile your code.

Tuesday, May 26, 2015

Latex Tables: How to Center Text Both Horizontally and Vertically

The normal approach is to use ">{\centering\arraybackslash}m{3cm}" to specify cell format at the top:
\begin{table}
\centering\begin{tabular}{|>{\centering\arraybackslash}m{3cm}|>{\centering\arraybackslash}m{5cm}|} \hline\textbf{Topic} & \textbf{Paragraph} \\\hline \hlineTopic 1 & This is a paragraph. This is a paragraph. This is a paragraph.\\\hline\end{tabular} \end{table}
The text is both vertically and horizontally centered, but, you probably want the Paragraph column to be left aligned. If you use "p" to specify the cell format for the second column, the text will no longer be vertically centered:

\begin{table} \centering\begin{tabular}{|>{\centering\arraybackslash}m{3cm}|p{5cm}|} \hline\textbf{Topic} & \textbf{Paragraph} \\\hline \hlineTopic 1 & This is a paragraph. This is a paragraph.\\\hline\end{tabular} \end{table}
To have a paragraph style text and vertical alignment both at the same time, use  ">{\centering\arraybackslash}m{3cm}" to specify cell format at the top and use \multicolumn{1}{m}{Text} in the table entry:
\begin{table} \centering\begin{tabular}{|>{\centering\arraybackslash}m{3cm}|m{5cm}|} \hline\textbf{Topic} & \textbf{Paragraph} \\\hline \hlineTopic 1 & This is a paragraph. This is a paragraph.\\\hline\end{tabular} \end{table}
Enjoy!
 






Sunday, April 12, 2015

Ubuntu Matlab: Error using VideoFileReader/setup Could not open the specified file.

In order to read video file using vision.VideoFileReader object, you need to initialize it:

video_source = vision.VideoFileReader('test.avi')

Now, to access a (next) frame of the video, you need to run the following command:

frame  = step(videoSource);

If you are doing it for the first time after fresh Matlab installation, you will probably run into the following error:

Error using VideoFileReader/setup 
Could not open the specified file.

This happens because Matlab is unable to find the correct codec to decode the video file. To solve this problem, follow these steps:


  • Install ubuntu-restricted-extras
    • sudo apt-get install ubuntu-restricted-extras
  • Install Gstreamer
    • sudo add-apt-repository ppa:gstreamer-developers/ppa
    • sudo apt-get update
    • sudo apt-get install gstreamer0.10-tools gstreamer-tools gstreamer0.10-plugins-base gstreamer0.10-plugins-good gstreamer0.10-plugins-bad
  • Install gstreamer0.10-ffmpeg 
    • sudo add-apt-repository ppa:mc3man/trusty-media
    • sudo apt-get update 
    • sudo apt-get install gstreamer0.10-ffmpeg
Restart the Matlab and it should work just fine!

Enjoy!