Ignore and Pause in C

I hate using non-standard code, and I also feel uneasy about telling someone to learn non-standard code. The reason is because this reduces the portability of code. Someone who uses non-standard code, have to learn new things every time they change their tools (i.e., OSs, compilers, IDEs, etc.).Unfortaunally some College teachers resolve to non-standard code to simplify thier lives. For some C programming instructors, use the library which is a library only available in Windows for a limited set of old compilers. This header file is used because it contains the getch function which basically causes the terminal screen to pause until the user hits the key.

The  standard alternative to getch function is getchar which can be used by including file. The problem with getchar is that it does not work if you use it after a previous call of scanf.  The reason is that a scanf does not remove the the end of lines in the input buffer which are  a result of a user hitting the key after each request of input. For getchar to work, you need to simply ignore what ever in the input buffer after each call to the scanf function before you call getchar. While C++ comes with a nice ignore method function, C unfortunately does not. However, it is not hard to code one by your self.

Here is my implementation of a an ignore function which I use after a scanf, and this helps me call getchar to do a proper and standard pause.

// FileName: area.c
// Compute the area of a cicle
// Shows the use of ignore and pause functions.

#include <stdio.h>
#include <math.h>

void ignore(int n, char delim)
// Pre : n is initialized to number of characters to ignore
//       delim is initialized to the some delimiter character
// Post: The function has extracts characters from the input sequence and
//       discards them, until either n characters have been extracted,
//       or one compares equal to delim.
{
    int count = 0;
    char c;
    while ((c = getchar()) != delim
        && !feof(stdin)
        && count != n)
        count++;
}

void pause()
// Pre : The input stream cin is empty.
// Post: The program has displayed a one-line message
//       beginning in column 1 and then paused.
//       The user has pressed <ENTER>.
{
    char returnChar;

    printf("Press <ENTER> to continue ... ");
    returnChar = getchar();
}

int main()
{
    const double PI = 3.14159265359;
    double r, area;

    printf("Enter radius: ");
    scanf("%lf", &r); ignore(80, '\n');

    area = PI * pow(r,2.0);

    printf("\nArea of a circle of radius %lf is %lf\n", r, PI);

    pause();

    return 0;
}

I hope you find this useful.

Advertisements

Creating A Local Maven repo from 3Rd party JARs

Sometimes you might need to package 3rd party JAR files in a way that allows you to incorporate them into a maven project easily.  To do so, you need to put the JARs in a local repository to so that the can be picked up by Apache maven. You can  achieve this by using the maven-install-plugin by running the following command:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

To illustrate this with an example, let us assume that we want to add the  Algorithms Book library to your local repository. You need download first the algs4.jar to your machine (i.e. ~/Downloads). Once you have done so, you can run the following command to add this file to your local repository.

mvn install:install-file -Dfile=~/Downloads/algs4.jar -DgroupId=edu.princeton.cs \
-DartifactId=algs4 -Dversion=1.0 -Dpackaging=jar

Once you added the jar file to the local repository. You can use the following XML in your project’s pom.xml.

<dependency>
	<groupId>edu.princeton.cs</groupId>
	<artifactId>algs4</artifactId>
	<version>1.0</version>
</dependency>

Happy Coding.

To 9/11 victims: You country is as Responsible as Saudi Arabia

The US Senate and House of Representatives recently approved a bill allowing the victims of 9/11 to sue Saudi Arabia for being backing the terrorists who were responsible for 9/11.

At the surface this might seems a noble cause, however, they forget that their own country is as responsible as Saudi Arabia. They seem to forget that their successive governments have long supported directorships in the Middle East and Islamic World. Dictatorship regeimes in countries like Egypt, Saudi Arabia, Jordan, and Pakistan have long enjoyed the support of the USA which has condoned the oppressions of such regimes. Such oppressions resulted in the rebellion of certain individuals like Osama Bin Laden who ended up establishing an Al Qaeda The victims of 9/11 also seems to forget that their own country has funded Al Qaeda and helped in establishing it during the Afghanistan war against the Soviet Union.

It is a bit ironic that the victims of country whose government is responsible the biggest atrocities in the 21st centenary. The USA government repeatedly lied to make the case for invading Iraq in 2003 which has results in the destruction of Iraq. Not only this, but they let the country to be taken extremist groups like Al Qaeda and later on Daesh.

Somehow the Americans think that they have the moral high ground when it comes to the war on terror. Yet, their government is stained with the blood of innocent victims who died because of the wars that of the USA in the Middle East.

If you really want to seek justice, you should then start at your own backyard and sue your own government.

Opening Up a port on RedHat

By default RedHat comes with strict configuration for protecting you from hackers. If you want to open up certain ports for network traffic you will need to use iptables command.  Here is how you might open tcp port 3030 for network traffic


$ sudo iptables -I INPUT -p tcp -m tcp --dport 3030 -j ACCEPT
$ sudo service iptables save # saves rules to /etc/sysconfig/iptables

Alternatively you can use  firewall tui tool


$ sudo system-config-firewall-tui

Replacing URIs with labels in RDF object fields using sparql

Here is how it is done with an example from Linked Movie Database

prefix movie: <http://data.linkedmdb.org/resource/movie/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?s ?p ?o
WHERE {
    {
       <http://data.linkedmdb.org/resource/director/102> ?p ?o1 .
       optional {?o1 rdfs:label ?o.}
       BIND (<http://data.linkedmdb.org/resource/director/102> as ?s)
       FILTER(bound(?o))
    }
    UNION
    {
       <http://data.linkedmdb.org/resource/director/102> ?p ?o .
       optional {?o rdfs:label ?label.}
       BIND (<http://data.linkedmdb.org/resource/director/102> as ?s)
       FILTER(!bound(?label))
    }
}
order by ?p
limit 25

Females in DBPedia

Here is a simple SPARQL query to list all females in DBPedia

SELECT ?property ?hasValue ?isValueOf
WHERE {
  { <http://dbpedia.org/ontology/Person> ?property ?hasValue }
  UNION
  {
       ?isValueOf ?property <http://dbpedia.org/ontology/Person> .
       ?isValueOf <http://dbpedia.org/property/gender> ?gender .
       FILTER regex(?gender, "female", "i")
  }
}

Algorithms, Part I — Social Network Connectivity (With Union-Find)

Nice blog post about “Social network connectivity” problem that appears in Coursera Algorithms I (https://class.coursera.org/algs4partI-009)

Steel Wings

Q: Given a social network containing N members and a log file containing M timestamps at which times pairs of members formed friendships, design an algorithm to determine the earliest time at which all members are connected (i.e., every member is a friend of a friend of a friend … of a friend). Assume that the log file is sorted by timestamp and that friendship is an equivalence relation. The running time of your algorithm should be MlogN or better and use extra space proportional to N.

(Java Code on github at the bottom of the post. )

My thoughts and solutions:

When solving dynamic connectivity problems (like whether there is a path between two nodes but we don’t care what the exact path is), we can try using the Union-Find algorithm. And this is a typical application of union-find in social network.

This question asks if we can determine the earliest time at which all…

View original post 391 more words