Wednesday, May 1, 2013

Swap without using a third variable

We know how to swap 2 values using a third temp variable. This is how you do it without using a third variable.

 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
#include "stdio.h"

static void swap(int *x, int *y)
{
        *x = *x + *y;
        *y = *x - *y;
        *x = *x - *y;
        return;
}

static void swap_xor(int *x, int *y)
{
        *x = *x ^ *y;
        *y = *x ^ *y;
        *x = *x ^ *y;
        return;
}

int main(int argc, char **argv)
{

        int x = 10, y = 20;
        printf("1> %d %d\n", x, y);
        swap(&x, &y);
        printf("2> %d %d\n", x, y);
        swap_xor(&x, &y);
        printf("3> %d %d\n", x, y);

        return 0;
}

[root@Imperfecto_1 ~]# gcc -g run.c && ./a.out
1> 10 20
2> 20 10
3> 10 20

Print bits in an Integer

How to print bits in an integer? There are many ways to do this, I mean logically. I have 2 functions here, one recursive and the other one using a loop.

 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
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "limits.h"

/* This function prints the bits in a recursive mode
 * It takes out the bits from right side i.e. 0, 1, 2
 * and prints, this being recursive will print the
 * output in reverse order which will be finally
 * the correct order
 */
static void recurse_bits(int x, int *size)
{
        int bit = x & 0x1;
        if((*size)--){
                recurse_bits(x>>=1, size);
                printf("%d ", bit);
        }
}

/* This function prints the bit by taking it from the left
 * side, i.e. 31, 30 etc and printing it right away
 */
static void print_bits(unsigned int x, int *size)
{
        unsigned short bits = *size;
        unsigned int mask = 0x1 << (bits-1);
        while(bits--){
                printf("%d ", !!(x & mask));
                x<<=1;
        }
        return;
}

int main(int argc, char **argv)
{

        int size = sizeof(unsigned int) * CHAR_BIT;
        int val = 9;

        print_bits(val, &size);
        printf("\n");

        recurse_bits(val, &size);
        printf("\n");

        return 0;
}


[root@Imperfecto_1 ~]# gcc -g run.c && ./a.out
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1

Monday, April 29, 2013

The lshw command

The lshw command lists the hardware information in your system. I came across this because I was having issues with wifi connectivity in Ubuntu 12.04. It just keeps disconnecting after a while and connects back. So, wanted to check the wifi card on and look for possible solutions.

root@Imperfecto_:~# lshw
imperfecto_               
    description: Desktop Computer
    product: Parrot ()
    vendor: GOOGLE
    version: 1.0
    serial: 123456789
    width: 64 bits
    capabilities: smbios-2.7 dmi-2.7 vsyscall32
    configuration: boot=normal chassis=desktop
  *-core
       description: Motherboard
       physical id: 0

and so on...

And I was interested in my wifi card, so I gave it some filters
root@Imperfecto_:~# lshw -C network
  *-network               
       description: Wireless interface
       product: AR9462 Wireless Network Adapter
       vendor: Atheros Communications Inc.
       physical id: 0
       bus info: pci@0000:01:00.0
       logical name: wlan0
       version: 01

and so on...

Installing the gnome desktop in Ubuntu 12.04

Yeah, I also didn't like the Unity which comes by default in Ubuntu 12.04. Just install the gnome-session-fallback and you are good to go


sudo apt-get install gnome-session-fallback

#In the newer version you may want to try
sudo apt-get install gnome-session-flashback


agentBUSH@ChrUbuntu:~# sudo apt-get install gnome-session-fallback
Reading package lists... Done
... bla bla bla ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
agentBUSH@ChrUbuntu:~# 

After installation, log out and at the log-in screen you will have the gnome-classic.

Thursday, April 25, 2013

Program to print pattern

Program to print the following pattern (see below for the pattern). More patterns will be posted later.
 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
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

static void print_it(unsigned len)
{
        if(!len) return;

        unsigned i;
        char *star, *buf, fmt[16]="";

        star = malloc(sizeof(char) * len+1);
        if(!star) return;
        buf = malloc(sizeof(char) * len+1);
        if(!buf) return;

        for(i=0; i<len; i++)
                strcat(star, "*");
        snprintf(fmt, 16, "%%%ds\n", len);
        for(i=len; i>0; i--){
                strcpy(buf, star+i-1);
                printf(fmt, buf);
        }
        free(star);
        free(buf);
        return;
}

int main(int argc, char **argv)
{
        print_it(12);
        return 0;
}


[root@Imperfecto_1 ~]gcc run.c && ./a.out
           *
          **
         ***
        ****
       *****
      ******
     *******
    ********
   *********
  **********
 ***********
************


Valgrind Output
[root@Imperfecto_1 ~]valgrind ./a.out

==5472== HEAP SUMMARY:
==5472==     in use at exit: 0 bytes in 0 blocks
==5472==   total heap usage: 2 allocs, 2 frees, 26 bytes allocated
==5472== 
==5472== All heap blocks were freed -- no leaks are possible


Split functionality of awk implementation in C


 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
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

static unsigned split(const char *in, char ***t_out, const char *delim)
{

        if( !*in || !*delim ){
                return 0;
        }

        unsigned i_len = strlen(in);
        char *buf = NULL;
        char *ptr = NULL;
        unsigned t_len, i=0;

        buf = malloc(sizeof(char) * i_len);
        if(!buf) return 0;

        if(!memcpy(buf, in, i_len)) return 0;

        ptr = strtok(buf, delim);
        if(ptr) {
                t_len = strlen(ptr);
                *t_out = (char **)malloc(sizeof(char*));
                (*t_out)[0] = (char *)malloc(sizeof(char) * t_len);
                memcpy((*t_out)[0], ptr, t_len);
        }
        while(ptr){
                ptr = strtok(NULL, delim);
                if(ptr) {
                        i++;
                        t_len = strlen(ptr);
                        *t_out = (char **)realloc(*t_out, sizeof(char *) * (i+1));
                        (*t_out)[i] = (char *)malloc(sizeof(char) * t_len);
                        memcpy((*t_out)[i], ptr, t_len);
                }
        }
        free(buf);
        return i;
}

int main(int argc, char **argv)
{

        char *in = "split.implementation.of.awk";
        char **out = NULL;
        int i, len = split(in, &out, ".");

        if(out) {
                for(i=0; i<=len; i++){
                        printf("%s\n", out[i]);
                        free(out[i]);
                }
                free(out);
        }
        return 0;
}


[root@Imperfecto_1 ~] gcc -g run.c && ./a.out
split
implementation
of
awk

Valgrind output

[root@Imperfecto_1 ~]# valgrind ./a.out
==2320== malloc/free: in use at exit: 0 bytes in 0 blocks.
==2320== malloc/free: 9 allocs, 9 frees, 91 bytes allocated.
==2320== For counts of detected errors, rerun with: -v
==2320== All heap blocks were freed -- no leaks are possible.