Posts

Showing posts from January, 2011

All 1's on one side

Program to see if all the ones in a number appear on the right side of the number and all zeros appear on the left, how can I do this most efficiently? (i.e. 00000111 is true but 100010 is false)

Malloc and segmentation fault

int main(int argc, char **argv) { char *buf; buf = malloc(1<<31); fgets(buf, 1024, stdin); printf("%s\n", buf); return 1; }What is the o/p and reason for its behaviour?

Number series

Which is next number in the series
9 15 17 21 ....

Sizeof structure in c

// (typical 32 bit machine)// char 1 byte// short int 2 bytes// int 4 bytes// double 8 bytes// structure Atypedefstructstructa{charc;shortints;}structa_t;// structure Btypedefstructstructb{shortints;charc;inti;}structb_t;// structure Ctypedefstructstructc{charc;doubled;ints;}structc_t;// structure Dtypedefstructstructd{doubled;ints;charc;}structd_t;intmain(){printf("sizeof(structa_t) = %d\n",

unique paths

Image
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?


Global and local name collision

Given the following source code

staticint a_static_var =5;void foo(void){

Void pointer in c++

In c++ int main(){int i =99;

Pass by reference in c++

#include<iostream>usingnamespace std;void f