Implement queue using stack

The puzzle is to implement basic queue operations with using only basic stack operations. That is, a stack object is given, we need construct a wrapper for the queue functions, insert, remove, which will only use the stack object as its storage, and naturally will have to use the stack operations. I have already posted the opposite task in the post Implement stack using a queue

This can be done using two stack objects. We call these the first stack and the second stack. Although either the insert or the remove complexity will no more be O(1).

I have discussed the process gradually. I added the last solution when it clicked in my mind while reviewing this post.

Continue reading

Posted in Coding Discussions, Computer Science | Tagged , , , | 4 Comments

Get list of installed packages and their details in R

When we need packages in R we install them. May be some time you wanted to have a look what packages were installed in your system and also what there version numbers are. Although rarely you would like to do this, here is a small tip on how to do it. Continue reading

Posted in GNU+Linux+FOSS, Tutorials | Tagged , , | Leave a comment

Plot histogram in terminal

We had assignments to print “*”s in different formation in undergraduate class, which I never liked as they were pointless. Now I got a somewhat justifiable application, plot histogram in terminal. In the last post Generating random numbers from Normal distribution in C I posted the C code to generate random numbers from the Normal distribution using the Polar method. In this post I am posting a simple code to plot the histogram of generated random numbers from this or any other distribution. Let me first post the code and then explain what is going on. Continue reading

Posted in Coding Discussions, Computer Science | Tagged , , , , , | 1 Comment

Generating random numbers from Normal distribution in C

I needed to write a random number generator in C which will generate random numbers from Normal Distribution (Gaussian Distribution). Without this component I couldn’t proceed to finish writing a C code for Heuristic Kalman Algorithm by Lyonnet and Toscano for some experiments. I selected the Marsaglia and Bray method also known as the Polar method to generate Normal random variables. Here is how it is done. Continue reading

Posted in Coding Discussions, Computer Science | Tagged , , , , , | 2 Comments

C LANGUAGE UERS! Y U NO SEE STANDARDS!

C LANGUAGE USERS! Y U NO SEE STANDARDS!

C LANGUAGE USERS!
Y U NO SEE STANDARDS!

I had to make this. There are people who give a damn to the standards and follow the stone age Turbo C 3.1 compiler, run programs compiled by it. In the cases for compiler dependent and undefined behaviours, they try to guess in some way why the output was like that and get into a conclusion. Continue reading

Posted in C Language, Computer Science, Humor | Tagged , , , | Leave a comment

C Q&A #5: A question about assignment operation and evaluation

Here is another C code snippet question. Consider the following piece of code, and spot if there is a problem in it or not.

#include <stdio.h>

int main (void)
{
  int x = 5;
  +x += 1;
  printf ("%d\n", +x);
  return 0;
}

Continue reading

Posted in C Language, Computer Science | Tagged , , , | Leave a comment

Save work environment in Octave

You won’t like to loose the variables and the command history generated after hours or days of work in octave. It is a good idea to save the environment in intervals to create backup points in addition to save from crashes. Here’s a quick tutorial on how to save your working environment in octave. Continue reading

Posted in Tutorials | Tagged , , , | Leave a comment

phoxis.org 2012 in review

Here goes some stats of this blog for this year from wordpress.com. Not bad, but not satisfactory, the end year posting frequency was terrible.

The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

19,000 people fit into the new Barclays Center to see Jay-Z perform. This blog was viewed about 86,000 times in 2012. If it were a concert at the Barclays Center, it would take about 5 sold-out performances for that many people to see it.

Click here to see the complete report.

Posted in Writings | Tagged | 2 Comments

Dynamically allocating 2d array with adjacent rows in memory

In a previous post “Allocating multidimentional array at runtime in C” I have explained a technique to allocate multidimensional arrays on runtime. While playing around with OpenMPI, I came to know that while sending/receiving a buffer, it requires the elements of a 2d matrix (or any dimension) to be in adjacent. Basically it does not care what you send, or receive, what it cares is the number of elements to be send should be adjacent to one another. In the previous post, the process will allocate the 2d or n-d matrix, but the rows of the matrix may not be adjacent to each other, as each row was allocated separately with malloc and then inserted into another array of pointers, each of which location points to the base addresses of these memory block. Read the post for details.

In C language the 2d array/matrix are stored in a row-major order, that is the elements of the matrix are stored adjacent to each other in the memory row wise. The first row comes first then just after the first row the second row starts, and so on. In the previous method the rows of the matrix may be scattered throughout the memory, as they are allocated with seperate malloc calls, but each of these returned addresses to the memory blocks (used as rows) are assigned to another array of pointers, which holds the rows together, and allows the mat[i][j] syntax to work.

For the applications in which, we might need to allocate the matrix dynamically at runtime, also have the rows of the matrix requires to be adjacent in the memory, and also make the mat[i][j] syntax work can be fulfilled by the following approach.
Continue reading

Posted in C Language, Coding Discussions, Computer Science | Tagged , , , | 4 Comments

Journey of a simple recursive code

Some one asked the following question in StackOverflow

Implement a function with prototype char *repeat(char *s, int n) so that it creates and returns a string which consists of n repetitions of the input string s. For example: if the input is “Hello” and 3, the output is “HelloHelloHello”. Use only recursive constructs.

I immediately started compiling the answer as it was a pretty straight forward question. But it was not only me who was posting the answer. So after posting, very quickly other answers started appearing. It was a general everyday answer, until a person (who also answered the question) pointed out a bug in my code. Bugs disturb the mind. So I dug into the code and fixed it. The things started to get challenging when others started to get more compact code, which didn’t let me sit idle.
Continue reading

Posted in C Language, Coding Discussions, Computer Science | Tagged , , , | Leave a comment