Beispiel #1
0
int main(int argc, char *argv[])
{
    if (argc == 0) perror("Please input a sorted string for searching!\n");
    
    char *string = argv[1];
    char findChar = *argv[2];

    FILE *output;
    char *fn;
    int sl = strlen(string);
    if (!asprintf(&fn, "%s_%d.txt", argv[0], sl)) perror("Unable to create file name.\n");
    output = fopen(fn, "a+");

    char smallest;
    struct timespec start, end;
    double cpu_time;
    
    assert(smallest_character("cfjpv", 'a') == 'c');
    assert(smallest_character("cfjpv", 'e') == 'f');
    assert(smallest_character("cfjpv", 'z') == 'c');

    clock_gettime(CLOCK_REALTIME, &start);
    smallest = smallest_character(string, findChar);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time = diff_in_milisecond(&start, &end);
    printf("String=%s\n", string);
    printf("Input: %c, Smallest: %c\n", findChar, smallest);
    printf("Time exec: %lfms\n", cpu_time);
    
    fprintf(output, "%lf\n", cpu_time);
    fclose(output);

    return 0;
}
Beispiel #2
0
int main()
{
    FILE *file = fopen("q2_iterative.txt", "a");
    double cpu_time1;
    struct timespec start, end;
    srand(time(NULL));
    int random_len = (rand() % 10) + 1;
    char i;
    static const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
    char str[random_len];
    struct sched_param param;
    int maxpri;
    maxpri = sched_get_priority_max(SCHED_FIFO);//max=99, min=1
    if(maxpri == -1) {
        perror("sched_get_priority_max() failed");
        exit(1);
    }
    param.sched_priority = maxpri;
    if (sched_setscheduler(getpid(), SCHED_FIFO, &param) == -1) {
        perror("sched_setscheduler() failed");
        exit(1);
    }
    memset(str, 0, sizeof(str));
    for(i=0; i<random_len; i++) str[i] = alpha[rand() % (strlen(alpha) - 1)];
    for(i=97; i<123; i++) { //test from a to z
        clock_gettime(CLOCK_REALTIME, &start);
        assert(smallest_character(str,i));
        clock_gettime(CLOCK_REALTIME, &end);
        printf("Output[%c]: %c\n",i, smallest_character(str,i));
        cpu_time1 = diff_in_second(start, end);
        fprintf(file, "%f\n", cpu_time1);
    }
    return 0;
}
Beispiel #3
0
int main()
{
    char str[] = {'c', 'f', 'j', 'p', 'v'};
    char c[3] = {'a', 'c', 'Z'};
    char result[3];
    struct timespec start, end;

    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);

    int i;
    for (i = 0; i < 10000; ++i) {
        result[0] = smallest_character(str, c[0]);
        result[1] = smallest_character(str, c[1]);
        result[2] = smallest_character(str, c[2]);
    }

    clock_gettime(CLOCK_REALTIME, &end);
    double cpu_time = diff_in_second(start, end);

    printf("result[0]: %c\n", result[0]);
    printf("result[1]: %c\n", result[1]);
    printf("result[2]: %c\n", result[2]);
    printf("execution time: %lf sec\n", cpu_time);

    return 0;
}
Beispiel #4
0
int main()
{
    char input[]={'c','f','j','p','v'},c='m',output;
    
    assert((smallest_character(input,c)) && "function error!\n");
    output = smallest_character(input, c);
    printf("result : %c\n",output);
    return 0;
}
Beispiel #5
0
int main()
    {
        char test[] = "cfjpv";
        char test2[] = "cck";
        printf("%c\n",smallest_character(test,'a'));
        printf("%c\n",smallest_character(test,'c'));
        printf("%c\n",smallest_character(test,'z'));
        printf("%c\n",smallest_character(test2,'f'));
        return 0;
    }
Beispiel #6
0
char smallest_character(char str[], char c)
{
    if (str[0]!='\0') {
        if (str[0]>c)
            return str[0];
        else {
            return ((smallest_character(str+1,c)<c)?str[0]:smallest_character(str+1,c));
        }
        return str[0];
    }
}
Beispiel #7
0
int main()
{
    char a[20] = {'c','f','j','p','v'};
    char b[20] = {'c','c','k'};
    char ans=smallest_character(a,'a');
    printf("%c\n", ans);
    ans=smallest_character(a,'c');
    printf("%c\n", ans);
    ans=smallest_character(a,'z');
    printf("%c\n", ans);
    ans=smallest_character(b,'f');
    printf("%c\n", ans);
    return 0;
}
Beispiel #8
0
int main()
{
    char out;
    struct timespec start, end;
    /* test case */
    /* input */
    char input[] = {'c', 'f', 'g', 'h', 'i', 'j','k', 'l','m','n','o', 'p', 'v'};
    out = smallest_character(input,'z',input[0]);
    assert( out == 'c' );

    clock_gettime(CLOCK_REALTIME, &start);
    out = smallest_character(input, 'z',input[0]);
    clock_gettime(CLOCK_REALTIME, &end);

    printf("answer is %c, execution time is %lf nsec\n",out ,diff(start,end));
    return 0;
}
Beispiel #9
0
char smallest_character(char str[], char c, char first)
{
    if(*str=='\0')	return first;
    if(*str>c) {
        return *str;
    } else {
        return smallest_character(str+1,c, first);
    }
}
Beispiel #10
0
//find smallest character larger than key recursively
char smallest_character(char str[], char c, int l)
{
	assert(strlen(str)>0);
        if(c < str[l-1])
            return smallest_character(str,c,l-1);
        else if(l == strlen(str) || l == 0)
		return str[0];
	else
		return str[l];
}
Beispiel #11
0
char smallest_character(char *str, char c ,int index)
{
    if(index < strlen(str)) {
        if(str[index] <= c) {
            return smallest_character(str,c,++index);
        } else
            return str[index];
    }
    return str[0];
}
Beispiel #12
0
int main( int argc, char *argv[] )
{
	/* There must be 3 arguments. */
	assert( argc == 3 && "Input format: <program> <sorted_str> <search character>" );
	
#ifdef MEASURE_TIME
	struct timespec start, end;

	clock_gettime( CLOCK_REALTIME, &start );
	smallest_character( argv[1], argv[2][0] );
	clock_gettime( CLOCK_REALTIME, &end );
#endif

	printf( "%c\n", smallest_character( argv[1], argv[2][0] ) );
#ifdef MEASURE_TIME
	printf( "Time for smallest_character(): %.9lf sec\n", diff_in_second( start, end ) );
#endif

	return 0;
}
Beispiel #13
0
int main()
{

    char sortedArray[] = {'c','f','j','p','v'};
    char ref = 'a';
    int index = 0;

    printf("Output : %c\n", smallest_character(sortedArray, ref, index));

    return 0;
}
Beispiel #14
0
char smallest_character(char str[], char c, int i)
{

    if (i == strlen(str))
        return str[0];
    else if (str[i] > c)
        return str[i];
    else
        return smallest_character(str, c, ++i);

}
Beispiel #15
0
int main()
{
    char str[]={'c','f','j','p','v'};
    char c[]={'a','c','z','f'};
    int left=0,right=(sizeof(str)/sizeof(str[0]))-1;
    for(int i=0;i<(sizeof(c)/sizeof(c[0]));i++)
    {
         printf("output is %c\n",smallest_character(str,c[i],left,right));
     }

    return 0;
}
Beispiel #16
0
int main(void)
{
	char input_arr[] = {'c','f','j','p','v','y'};
    	char key[] = {'a','c','j','s','z'};
	int inputlen = sizeof(input_arr)/sizeof(input_arr[0]);
	int keylen = sizeof(key)/sizeof(key[0]);
	int i=0;
	for(i=0;i<keylen;i++){
		printInput(input_arr, key[i]);
		printf("Output: %c\n\n",smallest_character(input_arr, key[i],inputlen));
	}
	return 0;
}
Beispiel #17
0
int main(int argc, char *argv[])
{
char tempInput[100];
char searchChar;
char temp;
char output;
int count = 0;
int end = 0;

FILE *input;
input = fopen (argv[1], "r");
if (input == NULL) {
	printf("FILE NOT FOUND!!");
	exit(1);
}

while (fscanf (input, "%c", &temp) != EOF) {
	if (temp == ']') {
		end = 1;
	}
	else if (temp != '[' && temp != '\'' && temp != ',' && temp != ' ' && temp != '\n') {
		switch(end) {
			case 0:
				tempInput[count] = temp;
				count++;
				break;
			case 1:
				searchChar = temp;
				end = 0;
				break;
			default:
				break;
		}
	}
}

char inputString[count];
int eco;
for (eco = 0;eco < count;eco++) {
	inputString[eco] = tempInput[eco];
}

output = smallest_character(inputString, searchChar);
printf("The smallest character is '%c'.\n", output);


fclose(input);
return 0;
}
Beispiel #18
0
int main()
{
    char input[100010];
    char str[100010];
    while(fgets(input,sizeof(input),stdin)!=0) {
        int len=strlen(input),i,j=0;
        for(i=2; i<len; i+=5) {
            if(input[i]=='\'') {
                str[j] = '\0';
                printf("'%c'\n", smallest_character(str,j,input[i+1]));
            } else
                str[j++]=input[i];
        }
    }
    return 0;
}
Beispiel #19
0
int main()
{
   struct timespec start, end;
    double cpu_time;
    char str[][30]={"   ","!!#$)+/","023589",":<=@","CDEFJMNPSV","[]^_","cdefjmnpsv","  !!235===DDDEcccggssv"};
    char c;
    for(int j=0;j<7;++j){
      printf("The sorted character array is %s \n",str[j]);
      printf("Enter the search character : ");
      scanf("%c",&c);
      //c = getche();
      clock_gettime(CLOCK_REALTIME, &start);
      assert(printf("%c\n",smallest_character(str[j], c)));
      clock_gettime(CLOCK_REALTIME, &end);
      cpu_time = diff_in_second(start, end);
      printf("execution time of iterative : %lf sec\n\n", cpu_time);
    }
}
Beispiel #20
0
int main(int argc, char *argv[])
{
	char str[9];
	char rtnCh;
	char cmpInput;
	clock_t start, end;
	double timespend;

	strcpy(str, argv[1]);
	cmpInput = *argv[2];
	start = clock();
	rtnCh = smallest_character(cmpInput, str);
	end = clock();
	timespend = timeCal(start, end);
	printf("ANS : %c\ntime spend : %lf\n", cmpInput, timespend);
	printf("the smallest characteris %c\n", rtnCh);

	return 0;
}
Beispiel #21
0
int main()
{
    char str[10];
    char c;
    struct timespec start, end;
    double cpu_time1;

    printf("Enter the SORTED string : ");
    gets(str);
    printf("Enter the search character : ");
    scanf("%c",&c);
    clock_gettime(CLOCK_REALTIME, &start);
    fflush(stdin);
    printf("%c\n",smallest_character(str, c));
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);
    printf("execution time of recursive : %lf sec\n", cpu_time1);
    return 0;
}
Beispiel #22
0
int main(int argc, char *argv[])
{
    char str[MAX_LEN];
    char given;
    FILE *fp;
    assert(fp=fopen(argv[1], "r"));
    while (fscanf(fp,"%s %c", str, &given)==2) {

        /* Print input message
        assert(printf("Input: ['%c'", str[0]));
        for (char *c=str+1; *c; c++)
            printf(", '%c'", *c);
        printf("], '%c'\n", given);
        */

        //printf("Output: '%c'\n", smallest_character(str, given));
        printf("%c\n", smallest_character(str, given));
    }

    return 0;
}
Beispiel #23
0
int main(){
    char smallest;
    smallest = smallest_character("cck", 'f');
    printf("Smallest character : %c\n", smallest);
    return 0;
}