Posts Tagged ‘ tiny c

BD-shell 1.0.0 released

I’m very pleased to announce the final release of my tiny C Unix Shell. Every requirement has been satisfied. I hope I will find the time to expand it

http://bd-things.net/projects/bd-shell/

Related posts

An example of mutual recursion using C

The first lecture of DSA was already interesting for me, I learned something about “Mutual Recursion” (when two procedures call each other). The pseudo-code example was about checking if a number is either even or odd.
Given that

  • 0 is even
  • N is even if n-1 is odd
  • N is odd if n-1 is even

And the algorithm:

even
INPUT: n – a natural number.
OUTPUT: true if n is even; false otherwise 

odd(n)
  if n = 0 then return FALSE
  return even(n-1)
even(n)
  if n = 0 then return TRUE
  else return odd(n-1)

I implemented a tiny C program which uses it:

#include <stdio.h>
int main (int argc, char const *argv[])
{
        // set an integer number here
        int number = 23945;
        // if the number is odd (1 = TRUE)
        if(odd(number)==1)
                printf("%d is odd\n",number);
        else
                printf("%d is even\n",number);
        return 0;
}

// returns 0 if the given number becomes 0, so the given number is odd
// returns even(number – 1) elsewhere
int odd(int number){
        if (number==0)
                return 0;
        else
                return even(number-1);
}

// returns 0 if the given number becomes 0, so the given number is even
// returns odd(number – 1) elsewhere
int even(int number){
        if(number==0)
                return 1;
        else
                return odd(number-1);
}
 

Let’s see if I can keep understanding things at DSA :D

Related posts

Sorting array elements with C language

UPDATE 17:19: it seems that the program I implemented today uses a kind of Bubble Sort algorithm, give it a try, it’s quite interesting!

After 3 long days studying C, I think I’ve assimilated a good knowledge base for the incoming semester.
So there is a tiny C program that sorts the elements of a given integer array, using pointers:

Download the source code (well commented)

#include <stdio.h>
void sortArray(int *firstElement, int *lastElement);
void swapArrayElements(int *firstElement, int *secondElement);
void printArray(int array[], int arraySize);

void sortArray(int *firstElement, int *lastElement){
        int *currentElement = firstElement;
        while (firstElement != lastElement){
                while(currentElement != lastElement){
                        if(*currentElement < *firstElement){
                                swapArrayElements(currentElement,firstElement);
                        }
                        currentElement++;
                }
                firstElement++;
                currentElement = firstElement;
        }
}

void swapArrayElements(int *firstElement, int *secondElement){
        int tmp;
        tmp = *firstElement;
        *firstElement = *secondElement;
        *secondElement = tmp;
}

void printArray(int array[], int arraySize){
        int counter = 0;
        while(counter<arraySize){
                printf("%d\n",array[counter]);
                counter++;
        }
}

int main (int argc, char *argv[]){
        int array[] = {2929393,1,23239,-66,15,4,3,0,112,45,3,1000,19};
        int arraySize = sizeof(array)/sizeof(array[0]);
        int *firstElement = &array[0];
        int *lastElement = firstElement + arraySize;
        printf("————————————————-\n");
        printf("Elements of the array:\n");
        printf("————————————————-\n");
        printArray(array,arraySize);
        printf("————————————————-\n");
        sortArray(firstElement,lastElement);
        printf("Elements sorted:\n");
        printf("————————————————-\n");
        printArray(array,arraySize);
        printf("————————————————-\n");
}

 

A better program should ask the user to input the array elements, and a better algorithm should not scan every array element n times, where n is the number of the elements.
But I wrote it just for fun and for learning C pointers. I will learn to do better in the Data Structures and Algorithms course in the next semester ;)

Related posts

Projects

Pomodroid
A Java/Android application that interacts with a Trac
system, retrieves developer’s tasks and lets him work following the basic rules of the Pomodoro technique.

Pomotux
A task manager implementing the Pomodoro Technique

BD-review
A dynamic website to allow people to review releases (albums, demos, EPs, singles) of (young, unsigned) music bands. Written using a small subset of JavaEE technologies, without the use of web-frameworks.

BD-incollo
A dpaste/pastebin clone written using Django

BD-shell
A tiny C shell for Unix systems

BD-theme Zen
BD-blog minimalistic Wordress theme, available for free

BD-theme
Old BD-blog Wordress theme, available for free

Unipoli
A well-written Java implementation of the popular Monopoli game by Hasbro. It is a project I wrote with other 3 University mates following a software development cycle (Scrum). Unipoli was the project for our Programming Project course. Source code included, released under GPL. We also provide Javadoc, user stories, uml diagrams, binaries.

Computer Shop Warehouse IDA
A very simple, not really useful IDA (Individual Database Application) developed for the “Introduction to Database Systems” course. The documentation is really interesting


Do you think my projects are useful? Has one of my projects helped you at the University? Do you like to learn something from my experience? Are you happy to be able to download every source code?
Then, why don’t you consider a small donation? Donations are useful to maintain my domains and the infrastructures that host my Projects. I’m just a student, I’m not interested to earn profits from my projects, that will always remain free. But I would be delighted to don’t pay for them :-)

Related posts