Ejemplo n.º 1
0
void test(void (*f)(int [], int), char *algorithm_name) {
    int array[] = {
        -1, -20, -15, -15, 7, 13, 14, -5, -27, 17, 21, 12, 10, -29, 14, -18,
        -7, 15, 7, -32, 26, -14, -12, 28, 19, -14, -5, 17, -29, -18, 7, 18,
        24, -32, -22, -18, 28, 5, 12, 18, -10, 22, -17, -11, 22, 18, -7, 12,
        -28, 18, 27, 2, -3, -23, -7, -31, -12, 13, 5, 2, -10, 5, 26, -16, -4,
        27, 2, -31, -14, 16, 25, 26, 4, 20, -22, 30, -10, 21, 24, -23, -3, 28,
        4, 0, -11, -6, 8, 17, 3, -31, 0, -31, -4, 8, 31, 11, 7, -17, -3, -4
    };
    int expected[] = {
        -32, -32, -31, -31, -31, -31, -29, -29, -28, -27, -23, -23, -22, -22,
        -20, -18, -18, -18, -17, -17, -16, -15, -15, -14, -14, -14, -12, -12,
        -11, -11, -10, -10, -10, -7, -7, -7, -6, -5, -5, -4, -4, -4, -3, -3,
        -3, -1, 0, 0, 2, 2, 2, 3, 4, 4, 5, 5, 5, 7, 7, 7, 7, 8, 8, 10, 11, 12,
        12, 12, 13, 13, 14, 14, 15, 16, 17, 17, 17, 18, 18, 18, 18, 19, 20, 21,
        21, 22, 22, 24, 24, 25, 26, 26, 26, 27, 27, 28, 28, 28, 30, 31
    };

    int array_size = sizeof(array)/sizeof(int);

    printf("%s\n", algorithm_name);
    printf("Input:\t");
    printarray(array, array_size);

    (*f)(array, array_size);

    printf("Output:\t");
    printarray(array, array_size);
    printf("\n");


    if(!equal(array, expected, array_size)) {
        error("The array not sorted correctly. Test failed.\n");
    }
}
void parti_integer(int value, int array[])
{
  //local variables
  int lcv;//loop control variable to control the recursive calling times
  int i = 0;//cotrol variable to check every element in the array

  //executive statements
  if(value == 1)//base case
  {
    while(array[i])//reach the end of the array, then add 1 to the array at the end
    {
      i++;
    }
    array[i] = 1;
    printarray(array);//after adding 1, print the array
  }
  else if(value == 0)//when the value = 0, where the remaining value called the partioned value are the same,such as 2 call f(2) in the partition of 4 case, print the array
  {
    printarray(array);
  }
  else//Inductive case
  {
    for(lcv = 1;lcv <= value;lcv++)
    {
      addtoarray(array, lcv);//add the value of loop control variable to the array, it will compensate what have been deleted by using deleteinarray function 
      parti_integer(value - lcv, array);//recursively call itself 
      deleteinarray(array, lcv, value);//delete the last calling form of the partition so that the addtoarray function can add new form of partition permutation into the array, such as (partition of 4)1 1 1 1, delete the last two, and add the new 2, so we get 1 1 2, more like f(2) call f(1) and f(1), and then call f(2)
    }
  }
}
Ejemplo n.º 3
0
//Main code
void main ()
{
	int numbers[10];
	int i, j;

	//generates random numbers
	for (i = 0; i < 10; i++)
	{
		numbers[i] = rand() % 100 + 1;
	}

	printf("Here are the randomly generated numbers: ");
	printarray(numbers);

	//sort
	int temp;
	for (i = 0; i < 10; i++)
	{
		for (j = i + 1; j < 10; j++)
		{
			if (numbers[i] > numbers[j])
			{
				temp = numbers[i];
				numbers[i] = numbers[j];
				numbers[j] = temp;
			}
		}
	}

	printf("\n\nHere are the sorted numbers: ");
	printarray(numbers);
}
int main()
{
	int b[10] = {1000,2,5,8,19,17,34,1,23,25};
	printarray(b);
	heapsort(b,10-1);
	printarray(b);
}
Ejemplo n.º 5
0
main()
{
        int A[SIZE], i, j, tmp;

        /* Fill array with descending numbers (worst case) */
        i=0;
        while (i<SIZE)
                A[i] = SIZE-i++;

        /* Print initial state of array */
        puts("Before:");
        printarray(A);

        /* Now, er, sort it */
        for (i=1; i<SIZE; i++)
        {
                tmp = A[i];
                j=i;
                while (j!=0 && A[j-1]>tmp)
                        A[j]=A[--j];
                A[j] = tmp;
        }

        /* Print sorted version */
        puts("After:");
        printarray(A);

}
Ejemplo n.º 6
0
int main(void){
	
	int a[] = {7, 4, 3, 0, 8, 3};
	int n = sizeof(a)/sizeof(a[0]);
	printarray(a, n);
	mergesort(a, 0, n);
	printarray(a, n);
}
Ejemplo n.º 7
0
int main()
{
	int nums[] = { 1, 1, 0 };

	printarray(nums, sizeof(nums) / sizeof(int));
	moveZeroes(nums, sizeof(nums) / sizeof(int));
	printarray(nums, sizeof(nums) / sizeof(int));
}
Ejemplo n.º 8
0
int main (int argc, const char * argv[])
{
	printarray(sources,6);
	qsort(sources, 6, sizeof(char *), compare);
    printf("Sorting String\n");
	printarray(sources,6);
    return 0;
}
Ejemplo n.º 9
0
int main()
{
    int a[5] = {};
    int b[5][10] = {{2}};
    int c[3][10][3] = {};
    printarray((int*)a, sizeof(a)/sizeof(int));
    printarray((int*)b, sizeof(b)/sizeof(int));
    printarray((int*)c, sizeof(c)/sizeof(int));
}
int main()
{
	int arr[]={32,11,25,6,77,2};
	int n=sizeof(arr)/sizeof(arr[0]);
	printarray(arr,n);
	insertionsort(arr,n);
	printarray(arr,n);
	return 0;
}
Ejemplo n.º 11
0
int main(){
  int a[LENGTH];
  // COMMENT THIS OUT TO ALWAYS GET THE SAME VALUES
  srand(time(NULL));		// set seed for rand with unix time,
				//otherwise same sequence each run
  printf("First random number is %d\n", rand() );
  fillarray (a, LENGTH);		// fill the arrray randomly
  printarray(a, LENGTH);		// show initial array
  sort (a, LENGTH);			// sort it
  printarray(a, LENGTH);		// show final array
return 0;}
Ejemplo n.º 12
0
/* Driver function to test mergesort */
int main() {
    int arr[] = {37,27,9,12,18,23};
    int size, l, r;
    size = sizeof(arr)/sizeof(int);
    /* Print unsorted array */
    printarray(arr,size);
    /* Mergesort the unsorted array */
    mergesort(arr,0,size-1);
    /* Print sorted array */
    printarray(arr,size);
    return 0;
}
Ejemplo n.º 13
0
int main (int argc, const char * argv[]) 
{   printf("List before sorting:\n");
	printarray();
	printf("Sorting by user_id (elem1)\n");
	
	//Compare function 1
	qsort((void *) &array,                    // Beginning address of array
		  array_size,                         // Number of elements in array
		  sizeof(struct ratings),             // Size of each element
		  (compfn)compare );                  // Pointer to compare function
	
	
	printarray();
	printf("Sorting source_id (elem2)\n");
	
	//Compare function 2
	//Find first address of element to order & number of rows affected
	
	int i,j=0;
	int length=0;
	int start=0;
	int end=0; 
	
	for (i=0;i<array_size;i++)
	{	if(array[i].user_id!=array[j].user_id)
			{
			
			start=j;
			end=i;
			length = end - start;	
			//printf("Length found, sorting (start %i, end %i, length %i.)\n",start,end,length);
			//	printf("Location of first element is %i.\n",&array[start].user_id);
			
			//Perform Sort
			qsort((void *) (
					&array[start].user_id),		// Beginning address of array
				    length,                             // Number of elements in array
				    sizeof(struct ratings),             // Size of each element
					(compfn)compare_2);                 // Pointer to compare function	
				
			j=i;
			}//end if loop
		end++;
		
		//printf("I: %d, J: %d.\n",i,j);	
		//getchar();	
		
	}//end while loop
	
	printarray();
    getchar();
}
Ejemplo n.º 14
0
int main(int argc, char const* argv[])
{
    int *temp;
    for(int i = 1; i <= 31; i += 5){
        temp = getRandomArray(i, 0, 25);
        printarray(temp, i);
        quicksort(temp, 0, i - 1);
        printarray(temp, i);
        delete [] temp;
    }

    return 0;
}
Ejemplo n.º 15
0
int main(int argc, char const* argv[])
{
    int *temp;
    for(int i = 1; i <= 71; i += 5){
        temp = getRandomArray(i, -20, 20);
        printarray(temp, i);
        countingsort(temp, i);
        printarray(temp, i);
        delete [] temp;
    }

    return 0;
}
Ejemplo n.º 16
0
int main()
{
	char s[SMAXLENGTH];
	char t[TMAXLENGTH];
	gettxt(s, SMAXLENGTH);
	escape(s, t);
	printf("\ns:\n");
	printarray(s, TMAXLENGTH);
	printf("\nt:\n");
	printarray(t, TMAXLENGTH);
	printf("\n\n");
	return 0;
}
Ejemplo n.º 17
0
int main(int argc, char const* argv[])
{

    for(int i = 1; i <= 41; i += 5){
        int *temp = getRandomArray(i, -25, 25);
        printarray(temp, i);
        mergeSort(temp, i, 0, i - 1);
        printarray(temp, i);
        delete [] temp;
    }


    return 0;
}
Ejemplo n.º 18
0
int scAcr20T0( SC_READER_INFO *ri, SC_CARD_INFO *ci, SC_APDU *apdu )
{
	BYTE cmd[ 5+SC_ACR20_MAX_BUFFER_SIZE+1 ];
	BYTE rsp[ SC_ACR20_MAX_BUFFER_SIZE+2 ];
	int ret, rsplen=sizeof(rsp);
#ifdef ACR20_DEBUG
	int i;
#endif

	if( ri->si==NULL ) return( SC_EXIT_BAD_PARAM );

	if( (ret=scReaderCheckAPDU( apdu, TRUE ))!=SC_EXIT_OK ) return( ret );

	/* Check if command is too long for ACR20. */
	if( (apdu->cmdlen>5) && (apdu->cmd[4]>(ri->maxc+5)) )
		return( SC_EXIT_CMD_TOO_LONG );

	/* Check if response might be too long for ACR20. */
	if( (apdu->cse==SC_APDU_CASE_2_SHORT) && (apdu->cmd[4]>ri->maxr) )
		return( SC_EXIT_RSP_TOO_LONG );
	if( (apdu->cse==SC_APDU_CASE_4_SHORT) &&
		(apdu->cmd[apdu->cmdlen-1]>ri->maxr) )
		return( SC_EXIT_RSP_TOO_LONG );

#ifdef ACR20_DEBUG
printf("[CASE:%d]",apdu->cse);
#endif
	cmd[0]=0xA0;
	if( apdu->cmdlen>0xFE ) {
		cmd[1]=0xFF;
		cmd[2]=(apdu->cmdlen>>8)&0xFF;
		cmd[3]=apdu->cmdlen&0xFF;
		memcpy( cmd+4, apdu->cmd, apdu->cmdlen );
#ifdef ACR20_DEBUG
printarray("[CMDb:",(cmd[2]<<8)+cmd[3]+4,cmd);printf("]");
#endif
		if( (apdu->cse==SC_APDU_CASE_4_SHORT) &&
			(apdu->cmdlen==(5+apdu->cmd[4])) ) {
			if( ci->protocol!=SC_PROTOCOL_T0 )
				cmd[4+apdu->cmdlen]=ri->maxr&0xFF;
			else
				cmd[4+apdu->cmdlen]=0x00;
			cmd[3]++;
			if( cmd[3]==0x00 ) cmd[2]++;
		}
#ifdef ACR20_DEBUG
printarray("[CMDa:",(cmd[2]<<8)+cmd[3]+4,cmd);printf("]");
#endif
	} else {
Ejemplo n.º 19
0
int main()
{
    int array[] = {45, 100,5, 10,60};

    printf("Before sorting: \n");
    printarray(array, SIZE);
    printf("\n");

    printf("After sorting: \n");
    //bubblesort(array, 0, SIZE);
    bubble2(array, SIZE);
    printarray(array, SIZE);
    printf("\n");

}
Ejemplo n.º 20
0
int main()
{
 int a[50], i, n;
 printf("\nEnter no. of elements: "); 
 scanf("%d", &n);
 printf("\nEnter the elements: \n");
 for (i=0; i<n; i++)
  scanf ("%d", &a[i]);
 printf("\nUnsorted elements: \n");
 printarray(a,n);
 quicksort(a,0,n-1);
 printf("\nSorted elements: \n");
 printarray(a,n);
 return 0;
}//end main
Ejemplo n.º 21
0
int main(){
	
	int arr[] = {5,3,5,4,1,2,90,100,11,1,0,-12};
	int size;

	size = sizeof(arr)/sizeof(arr[0]);

	printf("\nBefore Sorted\n");
	printarray(arr,size);
	insertionsort(arr,size);
	printf("\nAfter Sorted\n");
	printarray(arr,size);
	printf("\n");

}
Ejemplo n.º 22
0
// return new indirect array of one item of array
array slice(array a, int i){
    DEBUG(2, "slice: i=%d\n", i);
    //IFDEBUG(2, printarray(a, 0));
    const int rank = a->rank-1>0?a->rank-1:0;
    DEBUG(2, "rank=%d\n", rank);
    array z=malloc(sizeof *z + (2*rank)*sizeof(int));
    z->rank = rank;
    z->translate = a->translate? a->translate+1: 0;

    z->dims = (int *)(z+1);
    memcpy(z->dims, a->dims+1, z->rank*sizeof(int));

    z->weight = z->dims + z->rank;
    memcpy(z->weight, a->weight+1, z->rank*sizeof(int));
    if (a->translate && a->translate[0])
        z->cons = a->cons + a->translate[0][i];
    else
        z->cons = a->cons + i*a->weight[0];
    DEBUG(2, "acons=%d  zcons=%d\n", a->cons, z->cons);

    z->type = indirect;
    z->data = a->data;
    IFDEBUG(3, printarray(z, 0));
    return z;
}
Ejemplo n.º 23
0
// spec is a C array of `array`s
//     each element of spec may be
//         NULL :: select entire underlying dimension
//         rank=0 array :: select single index from dimension
//         rank=1 array :: select several indices from dimension
// spec = { { 0, 1, 2 }, NULL, { 3 }}
// new dimes:    3,  a->dims[1] 
// new weight:   1,  a->weight[1]
// new cons:     += 3 * a->weight[2]
// new translate: { {0*a->weight[0], 1*a->weight[0], 2*a->weight[0]}, NULL }
array slicec(array a, array *spec){
    int rank = 0;
    for (int i=0; i<a->rank; i++){
        DEBUG(1, "slicec: spec[%d]=%s\n", i, spec[i]? "array":"null");
        IFDEBUG(1, if (spec[i]) printarray(spec[i],0); );
        rank += spec[i] ? spec[i]->rank!=0 : 1;
    }
Ejemplo n.º 24
0
int main()
{
	FILE *f;
	int *a, i = 0;
	time_t t;

	f = fopen("data", "r");
	if (!f)
		PERROR_RET("Unable to open file data for reading");


	a = (int *) calloc(sizeof(int), MAX_NUM);

	while (!feof(f)) {
		fscanf(f, "%d", &a[i]);
		i++;
	}

	start_timestamp();
	insertsort(a, i - 1);
	t = stop_timestamp();
	printf("Sorting complete in %lu microseconds\n", t);
	printarray(a, i - 1);

	free(a);
	fclose(f);

	return 0;
}
Ejemplo n.º 25
0
int main(int argc, char** argv)  {
    if (argc == 1) {
        printf("Not Enough Arguments!\n"
                "Give a list of integers as arguments to get data about them!\n");
        return -1;
    }
    else {
        argc = argc - 1; 
        argv = argv + 1;
        int i, len, test;
        int myArray[argc];
        for(i=0; i < argc; i++) {
            myArray[i] = atoi(argv[i]);
            len++; //new array length after checking
        }
        pthread_t minthread, maxthread, avgthread; //declare threads
        pthread_attr_t attr;
        struct thread_args *args = malloc(sizeof(args));
        args->len = argc;
        args->arrPtr = myArray;
        pthread_attr_init(&attr);
        awesomePrompt();
        printarray(argc, myArray);

        pthread_create(&minthread, &attr, getMinThread, args);
        pthread_join(minthread, NULL);
        
        pthread_create(&maxthread, &attr, getMaxThread, args);
        pthread_join(maxthread, NULL);

        pthread_create(&avgthread, &attr, getAvgThread, args);
        pthread_join(avgthread, NULL);
    }
    return 0;
}
Ejemplo n.º 26
0
void bubblesort(int A[], int n) {
    int i, j;
    //for (i = n - 2; i >= 0; i--) {
    //for (i = 0; i <= n - 2; i++) {
    for (i = 0; i < n - 1; i++) {
	    //数组是0~n-1
	//i n-1-i-1
	//0 n-1-(0)-1=n-2
	//1 n-1-(1)-1=n-3
	//..............
	//n-3,n-1-(n-3)-1=1
	//n-2,n-1-(n-2)-1=0
	//
	//0~n-2,最远n-2与n-1交换
	//0~n-3
	//....
	//0~1
	//0~0,最远0与1交换
	//n-1-i,就是比较n-1之前的i个元素!
	for (j = 0; j < n - 1 - i; j++) {
	    if (A[j] > A[j + 1]) {
		printf("A[%d],A[%d]change\t", j, j + 1);
		printf("==%d,%dchange\t", A[j], A[j + 1]);
		int k;
		for (k = 0; k < n; k++) {
		    printf("%d ", A[k]);
		}
		    printf("\t");

		swap(&A[j], &A[j + 1]);
		printarray(A, n);
	    }
	}
    }
}
Ejemplo n.º 27
0
/*
  recap on C peculiarities. Pointers, passing by value/reference, passing arrays.
  classes
  */
int main(int argc, char *argv[]){
  unsigned int array1[] = {1,2,3,4,5};
  printarray(array1,sizeof(array1)/sizeof(unsigned int));
  printptr(array1+2);
  array1[2] = 12;
  printptr(array1+2);
  printarray(array1,sizeof(array1)/sizeof(unsigned int));
  Coord xxx = {
    101,  // should be x
    2320, // should be y
    -1};  // should be bla
  printf("bla = %d\n",xxx.bla);
  printf("x = %d\n",xxx.x);
  printf("y = %d\n",xxx.y);
  printCoord(xxx);
  printCoord(xxx);
  printarray2(array1,sizeof(array1)/sizeof(unsigned int));
}
Ejemplo n.º 28
0
Archivo: e.c Proyecto: gowsiii/kousi
int main(void) {
int n,a[10],i,k;
scanf("%d%d",&n,&k);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
right_rotate(a,k,n);
printarray(a,n);
	return 0;
}
Ejemplo n.º 29
0
void main(void) {
	int array[MAX];
	int x;
	printf("wuwedi x\n");
	scanf("%d\n", &x);
	fillarray(array);
	sortarray(array);
	printf("sortiraniyat masiw:");
	printarray(array);
	
		}
Ejemplo n.º 30
0
int main(int argc, char **argv) {
	gf_t *gfield_context = malloc(sizeof(gf_t));
	uint32_t *vmond_row = malloc(NUM_CHECKSUMS*sizeof(uint32_t));
	uint32_t *chksums = calloc(NUM_CHECKSUMS, sizeof(uint32_t));
	uint32_t data = 0x41414141;

	debug4("Initializing gf.\n");
	gf_init_easy(gfield_context, FIELD_SIZE);

	for (int i = 0; i < 32; i++) {
		printarray(chksums, NUM_CHECKSUMS);
		createvmond(gfield_context, vmond_row, NUM_CHECKSUMS, i+1);
		updatechecksum(gfield_context, chksums, vmond_row, NUM_CHECKSUMS, data);
		//stepvmond(gfield_context, vmond_matrix, FIELD_SIZE);
	}
	printarray(chksums, NUM_CHECKSUMS);

	//free(gfield_context);
	argc=0; argv=0;
}