Programming

You are a beginner in C programming language (maybe 1st year student in a computer science department) and you want to meet a deadline for a programming assignment. The program specification is simple (because it is still the beginning) but there is something annoying happenning with your source code.

The course instructor has forced you to compile your C programs with all the appropriate flags enabled, e.g.

>> gcc program_name.c -ansi -pedantic -Wall -o program_name

Your program includes mathematical functions and as a result you have included math.h library and you have added -lm flag during compilation time:

>> gcc program_name.c -ansi -pedantic -Wall -lm -o program_name

Your final program should work (perfectly!) and after compilation NO warnings should appear. Otherwise, you lose points on your final mark! :-)

You have followed all the instructions, your program is working(!) BUT you still get an annoying warning, like:

implicit declaration of function abs

You try again and again, you 'google' this warning but there is no RESULT. Usually, the compiler is absolutely right and points to the right direction (in simple programs of course). So, there is an implicit declation of the 'abs' function (which returns the absolute value of a number). Something is missing. What? The following line (in most of the cases):

#include <stdlib.h>

You should include stdlib.h library as well. Don't forget that! That's it ... it should have helped you! -:)

 

In this article, we propose a simple way for sorting a matrix depending on the values of one of its columns. We use a minimal example for that but the method can be applied to matrices of any size.

Let's create our matrix first (it will be an unsorted 3x2 matrix). You may skip Steps 1 to 3 as you will have a matrix already!

  • Step 1: Create  the 1st column of the matrix

Matlab - How to sort a matrix depending on a column

  • Step 2: Create the 2nd column of the matrix

Matlab - How to sort a matrix depending on a column

  • Step 3: Compose the two columns to form the matrix

Matlab - How to sort a matrix depending on a column

  • Step 4: Sort the target column (in our example it is the 2nd column) of the matrix and receive a sorter vector

Matlab - How to sort a matrix depending on a column

  • Step 5: Compose the SORTED matrix

Matlab - How to sort a matrix depending on a column

 

I hope this helps somebody!!!

How to clear Apache Cayenne's cache

Submitted by bill on Fri, 01/08/2008 - 01:28

In this post we refer to some simple ways - tricks in order to clear Apache Cayenne's cache. Clearing the cache is essential especially when huge datasets are extracted continuously from the database (and passed into objects).

A nice and easy solution for this is to create a new DataContext before executing queries that will fetch huge amounts of data. You just have to create a new object each time (before the query):

DataContext context = new DataContext.createDataContext();

and then use this for your query:

List queryRows = context.performQuery(selectQuery);

Another way, which is not always very effective but it can be used together with the previous one is to directly clear the data rows from the object store cache.

context.getObjectStore().getDataRowCache().clear();

In heavy duty applications you should always adjust the memory for the java heap using the runtime arguments -Xms128m (mininum memory size set to 128Mb) and -Xmx512m (maximum memory size set to 512 Mb). The values (128 and 512) are just an example (you should test your code and find out the right numbers for these values).

 

This is another way to sort a HashMap. This way is more useful as it sorts the HashMap and keeps the duplicate values as well.

public LinkedHashMap sortHashMapByValuesD(HashMap passedMap) {
    List mapKeys = new ArrayList(passedMap.keySet());
    List mapValues = new ArrayList(passedMap.values());
    Collections.sort(mapValues);
    Collections.sort(mapKeys);
        
    LinkedHashMap sortedMap = 
        new LinkedHashMap();
    
    Iterator valueIt = mapValues.iterator();
    while (valueIt.hasNext()) {
        Object val = valueIt.next();
        Iterator keyIt = mapKeys.iterator();
        
        while (keyIt.hasNext()) {
            Object key = keyIt.next();
            String comp1 = passedMap.get(key).toString();
            String comp2 = val.toString();
            
            if (comp1.equals(comp2)){
                passedMap.remove(key);
                mapKeys.remove(key);
                sortedMap.put((String)key, (Double)val);
                break;
            }

        }

    }
    return sortedMap;
}

Syndicate content