tags:

views:

131

answers:

3

Hey Guys

My aim is to write an app which generates an char - array (each should be random-filled with strings of the length 4) and sorts this array. The time this process takes should be measured. I coded the following:

#include <string.h>
#include <jni.h>
#include <time.h>
#include <math.h>

clock_t start, finish;
static int ARRAY_LENGTH = 200;
static int WORD_LENGTH = 4;
char values[200];

void sortStringArray(void){
    int i, j;
    for(i = 0; i < ARRAY_LENGTH; i++){
        for(j = 0; j < ARRAY_LENGTH-1; j++){
          if(strcmp(values[j], values[j+1]) > 0) {
            char holder = values[j+1];
            values[j+1] = values[j];
            values[j] = holder;
          } 
        }
    }   
}
char generateRandomChar(char aC[]){
   int length = strlen(aC);
   char randStr[WORD_LENGTH];
   int m;
   for(m = 0; m <WORD_LENGTH; m++){
     int randNr = rand()%length;
     randStr[m] = aC[randNr];
  }
  return randStr;
}

void fillStringArray(void)
{
  char allowedChars[] = "abcdefghijklmnopqrstuvwxyz";
  int k;
  for(k = 0; k < ARRAY_LENGTH; k++){
     char randStr = generateRandomChar(allowedChars);
     values[k] = randStr;
  }
}
double
Java_com_example_hellojni_HelloJni_processStringSort( JNIEnv* env, jobject thiz)
{
    start = clock();
    fillStringArray();
    sortStringArray();
    finish = clock();
    return ((double)(finish - start));
}

Since I am pretty new to coding C, i am not that fimilar with the concept of pointers, and therefore i recieve some mistakes.

alt text

It would be helpful if sb could explain me where it would be useful to use a pointer in this code. Some help with the errors would be very appreciated. Thanks! Ripei

+3  A: 

Without re-writing your code from scratch, it is difficult to to know where to start. I'm afraid it is all wrong. In order to get a good understanding of pointer and character string use in C, you must read a good, authoritative book on the language, Luckily, C has one of the best such books in the world - The C Programming Language. If you haven't already got a copy, get one, and if you have, re-read the chapters on pointers and strings.

anon
As a corollary, it shows the coder didn't go step by step trying out if what was written worked. He should, especially when learning a new language.
Vinko Vrsalovic
@Vinko Very good point. It's quite shocking how many learning programmers write huge wodges of code and are then surprised when it won't compile or work.
anon
+2  A: 

Well for one thing you seem to think that char means string.... sometimes? char means a character, a number between 0 and 255. As the warnings on line 15 say, values[j] and values[j+1] are not strings (char *), they are characters (char). You probably want to make values an array of strings, ie an array of arrays of characters.


The 2nd set of warnings you're getting are related to line 31, where you're returning an array of characters (a pointer) from a function that states that it returns a character. The compiler silently casts the pointer to a character (since a pointer is a number) and returns that. You'll end up with a random number, which is probably not what you want.

To fix this you'll have to make the function return a char *, but there's a catch. randStr is gone as soon as you get out of the function, thus making it impossible to return. You could use strdup to duplicate the string and, after you're done using it in your main function, you call free to get rid of it.

While we're on this function, the parameter to it should be a char *, not a char[]. They have different meanings.


The last message (the only error reported as such apparently) is because you didn't define rand(). Adding a #include <stdlib.h> at the beginning of the program should fix it.

Blindy
For most C implementations, `unsigned char` holds values from 0 to 255, but an implementation is free to specify any upper limit for `unsigned char` type as long as it is at *least* 8-bit.
dreamlax
How sure are you? As far as I know, the standard requires `char` to be exactly 1 byte long.
Blindy
A: 

Thank you very much Blindy for your hints. I tried to implement your hints. Now the program doesn't throw errors but the problem is that i can't check if the operation is done correctly with the programm-environment i've to work with. Do you think the code is correct as it is shown below? Also the time it takes is quite less: 11ms. Do I calc this right?

Neil Butterworth,... well your're probably right, but I had to start somehow... and I tried my best to do so. Vinko Vrsalovic,... well you're not right ;) I did it step by step but I thought that its better to show you the whole program and all errors at one time.

#include <string.h>
#include <jni.h>
#include <time.h>
#include <stdlib.h>

long start, finish;
static int ARRAY_LENGTH = 500;
static int WORD_LENGTH = 4;
static int LOOPS = 10;
char *values[1000];

static long getTime(void){
    struct timeval  now;
    gettimeofday(&now, NULL);
    return (long)(now.tv_sec*1000 + now.tv_usec/1000);
}

void sortStringArray(void){
    int i, j;
    for(i = 0; i < ARRAY_LENGTH; i++){
        for(j = 0; j < ARRAY_LENGTH-1; j++){
          if(strcmp(values[j], values[j+1]) > 0) {
            char *holder = values[j+1];
            values[j+1] = values[j];
            values[j] = holder;
          } 
        }
    }   
}
char* generateRandomChar(char *aC){
   int length = strlen(aC);
   char randStr[WORD_LENGTH];
   int m;
   for(m = 0; m <WORD_LENGTH; m++){
     int randNr = rand()%length;
     randStr[m] = aC[randNr];
  }
  return strdup(randStr);
}


void fillStringArray(void)
{
  char *allowedChars = "abcdefghijklmnopqrstuvwxyz";
  int k;
  for(k = 0; k < ARRAY_LENGTH; k++){
     char *randStr = generateRandomChar(allowedChars);
     values[k] = randStr;
  }
}

jlong
Java_com_example_hellojni_HelloJni_processStringSort( JNIEnv* env, jobject thiz)
{
    start = getTime();
    int i;
    for(i = 0; i < LOOPS; i++){
      fillStringArray();
      sortStringArray();
    }
    finish = getTime();
    return (finish - start);
}
Ripei
You do it step by step and TEST it until it is doing what it should do... having a complete and incorrect program shows you didn't do that. You just shouldn't move on to the next function or piece of code until the other one is tested and shown correct.
Vinko Vrsalovic
You need to add a loop over "values" to free the memory you have allocated with the strdup, for instance as a separate function to be called after sortStringArray. Otherwise you create memory leaks.
Slauma
Alternatively to using strdup you could replace "char randStr[WORD_LENGTH];" by "char* randStr=(char*)malloc(WORD_LENGTH+1);" and add randStr[WORD_LENGTH]=0 before the return. It saves to copy the string.
Slauma
I can't really tell what you're trying to do so I can't help fix the semantics of your program. I just told you what the syntactic problems were. About the timing though, the operations you're doing (iterating over an array of max 500 characters) are instantaneous. There's no point in timing them; what you're seeing (11ms) is how fine grained `clock()` is on your platform. Trust me, you can do hundreds of thousands of iterations of your code and you wouldn't notice it.
Blindy
Oh and Slauma is right, `randStr[WORD_LENGTH]` has to be 0, if you tried printing what you wrote your program would most likely crash (and that's the best case scenario).
Blindy
Surprising that the code as it is now doesn't crash and you were able to measure the time at all. Not only for printing, even for strdup and strcmp in sortStringArray the strings must be null-terminated but they aren't. Must be rather good luck but I wouldn't trust the result of sorting.
Slauma