Permutation and Combination…again.

These here are updated versions to my previous attempts at listing permutations and combinations, and these are a lot shorter than those and easier to understand. Even though it is in C++, converting to pure C is not a huge endevour either.

Combinations

#include <iostream>
#include <string>
#include <cstdio>
#define MAX 100
using namespace std;
int count;
int r;
void comb(char *arr, int i, int j, const char *line){
    if (j<r) {
        for (int x=i;x<count;x++){
            char newline[MAX];
            sprintf(newline, "%d %s",x+1, line );
            comb(arr, x+1, j+1, newline);        
        }
    } else {
        cout<<line<<endl;
    }
}

int main(){
    cin>>count>>r;
    char bits[count];

    for (int i=0;i<count;i++) bits[i] = 0;
    comb(bits,0,0,"");
    return 0;
}

Permutations

#include <iostream>
#include <string>
#include <cstdio>
#define MAX 100
using namespace std;
int count;

void perm(char *arr, int i, const char *line){

    if (i<count) {
        for (int x=0;x<count;x++){
            if (arr[x] != 1) {
                char newline[MAX];
                sprintf(newline, "%d %s",x+1, line );

                arr[x] = 1;
                perm(arr, i+1, newline);
                arr[x] = 0;
            }
        }
    } else {
        cout<<line<<endl;
    }
}

int main(){
    cin>>count;
    char bits[count];

    for (int i=0;i<count;i++) bits[i] = 0;
    perm(bits,0,"");
    return 0;
}

Send SMS using Android ADB

After a great deal of searching through Android documentation, I’ve made a simple shell script that’ll send SMS!
To send, run this script with argument 1 as the phone number(in CCIIXXXXXXX format C is for country code and I is for company code) and argument 2 as the message. Use “” if you have spaces in it.

#! /bin/bash
# Send text through adb by Madura A.
# https://market.android.com/details?id=org.jraf.android.nolock
# Please use the above app(or similar) to keep it from locking while
# using this script

# FIX: Android 4.1.1 suggested by samox86
PATH=$PATH:/system/xbin:/system/bin

ADB=/opt/android-sdk-linux/platform-tools/adb
$ADB shell am start -a android.intent.action.SENDTO -d sms:$1 --es sms_body "$2" --ez exit_on_sent true
sleep 1
$ADB shell input keyevent 22
sleep 1
$ADB shell input keyevent 66

Change $ADB according to where you’ve installed ADB. Make sure you’ve configured ADB(obviously!).

Spectrum

This is a sound spectrum using ALSA & OpenGL, it is not a *proper* spectrum because it does not have any FFT support it just shows data only ordered by time. Download the code and ‘make’ to compile. Following packages are needed,

freeglut3, libasound-dev

Dancing cubes

This is true ALSA multi-threaded capturing! This my first, ALSA and POSIX thread application. Happy that it works as intended(well I hope the cubes jumped much higher but hey..they jump!). Drawing is done on one thread and the capturing is done on another. Make sure you set the recording source for ALSA to your system out. It was easy for me because ALSA is managed(to an extent) by PulseAudio(don’t worry, the application does not include anything PulseAudio). pavucontrol makes it easier to set the source to the system’s output’s monitor.

Here’s how to compile this,

g++ -I. -o dance ogl.cc alsa.cc -DdDOUBLE -lGL -lGLU -lglut -lode -lasound

Please have the ALSA development package installed.
ALSA code was ripped out from Linux Journal’s web site, thank you!