Beispiel #1
0
void algorithm(int *arr, int size, int value, int version, FILE *file) {

    clock_t T1, T2;
    double time;
    int *outputArr;
    
    outputArr = createArr(size);
    initArr(outputArr, size);
    
    T1 = clock();//start timer
    
    //Call the appropriate algorithm on the array
    switch (version) {
        case 1 :
            outputArr = algorithm_1(arr, outputArr, size, value);
            break;
        case 2 :
            outputArr = algorithm_2(arr, outputArr, size, value);
            break;
        case 3 :
            outputArr = algorithm_3(arr, outputArr, size, value);
            break;
    }
    
    T2 = clock();//end timer
    time = (double)(T2 - T1) / CLOCKS_PER_SEC;//get total time
    
    value = sumArray(outputArr, size);
    print_results(outputArr, size, value, file);
    
    fprintf(file, "Time to execute: %fs\n", time);
    
    free(outputArr);
}
Beispiel #2
0
int main(int argc, char *argv[]) {
	/*
	printf("Argument count: %d\n", argc);
	for (int i = 0; i < argc; i++) {
		printf("Argument vector values:%s at %p memory\n", argv[i], argv[i]);
		for (char *j=argv[i]; *j!='\0'; j++) {
			printf("Another way to print argument vector values: "
                               "%c at %p memory\n", *j, j);
		}
	}
	*/

	int *arrp[10] = {1,2,3,4,5,6,7,8,9,10};

	printf("Array pointer start is: %p\n",arrp);
	printf("Array pointer start is: %p\n",&arrp);
	printf("Array pointer start is: %p\n",arrp[0]);
	printf("Array pointer start is: %p\n",&arrp[0]);
	printf("Array pointer start is: %p\n",arrp[5]);
	printf("Array pointer start is: %p\n",&arrp[5]);

	initArr(arrp);

	return 0;
}
Beispiel #3
0
int main(int argc, char *argv[])
{
    hdf_sa_t arr[LEN];
    initArr(arr, LEN);

    // create data type corresponding to compound struct
    hid_t cid = H5Tcreate(H5T_COMPOUND, sizeof(hdf_sa_t));
    H5Tinsert(cid, "a", HOFFSET(hdf_sa_t, a), H5T_NATIVE_INT);
    H5Tinsert(cid, "b", HOFFSET(hdf_sa_t, b), H5T_NATIVE_FLOAT);
    H5Tinsert(cid, "c", HOFFSET(hdf_sa_t, c), H5T_NATIVE_DOUBLE);

    // write data to file
    hid_t fid = H5Fcreate("compound.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    // create data space
    hsize_t dim[1] = {LEN};
    hid_t space = H5Screate_simple(1, dim, NULL);
    hid_t dataset = H5Dcreate(fid, "compound", cid, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    // write data 
    H5Dwrite(dataset, cid, H5S_ALL, H5S_ALL, H5P_DEFAULT, arr);

    H5Sclose(space);
    H5Dclose(dataset);
    H5Fclose(fid);

    return 0;
}
Beispiel #4
0
int *algorithm_1(int *arr, int *outputArr, int size, int value) {
    
    int i, k, *tempArr1, *tempArr2, *tempArr3;
    k = value;
    
    if (k == 1) {
        outputArr[0]++;
        return outputArr;
    }
    else if ((i = isCoin(arr, size, k))) {
        outputArr[i]++;
        return outputArr;
    }
    else {
        //loop through every integer from 1 to "value"
        for (i = 1; i < k; i++) {
            
            //create and initilize three temprorary arrays of size "size" to all 0's
            tempArr1 = createArr(size);
            tempArr2 = createArr(size);
            tempArr3 = createArr(size);
            initArr(tempArr1, size);
            initArr(tempArr2, size);
            initArr(tempArr3, size);
            
            //recursively call algorithm_1 on array from 1 -> i and i + 1 -> "value"
            tempArr1 = algorithm_1(arr, tempArr1, size, i);
            tempArr2 = algorithm_1(arr, tempArr2, size, k - i);
            
            //add the sum of all elements at each index of tempArr1 and temArr2 together, assign to tempArr3
            addArr(tempArr1, tempArr2, tempArr3, size);
            
            //if this new sum of elements array (tempArr3) is less than the running min (outputArr) or the current output array is not a solution, then replace outputArr with tempArr3
            if ((sumArray(tempArr3, size) < sumArray(outputArr, size)) || !coinCheck(outputArr, arr, size, k)) {
                copyArr(tempArr3, outputArr, size);
            }
            
            free(tempArr1);
            free(tempArr2);
            free(tempArr3);
        }
    }
    
    return outputArr;
    
}
Beispiel #5
0
int main(void) {
    int low, high;
    memset(arr, 0, sizeof(arr));
    initArr();   
    scanf("%d %d\n", &low, &high);
    while (low && high) {
        printf("%d\n", arr[high] - arr[low - 1]); 
        scanf("%d %d\n", &low, &high);
    }
    return 0;
}
Beispiel #6
0
int main(){
	int letters[26] = {0};
	int Arr[26];	//保存字母的顺序
	char string[200];
	gets(string);
	lower(string);
	count(string, letters);
	initArr(Arr);
	sort(letters, Arr);
	print(letters, Arr);
	return 0;
}
Beispiel #7
0
/*
 下面在存储的时候就已经排序了,没有调用上面的冒泡排序,这是之前说过的桶排序思想
 */
int main(int argc, char const *argv[])
{
    int arr1[N];
    int arr2[N];
    initArr(arr1, N);
    initArr(arr2, N);
    int value;
    int len = 0;
    int n;
    scanf("%d", &n); //输入n,表示下面一个数组要输入多少元素
    for (int i = 0; i < n; ++i) //输入第一个数组元素
    {
        scanf("%d", &value);
        arr1[value] = 1 ; //出现位置的值等于一,这样可以去掉重复的元素
    }
    
    for (int i = 0; i < n; ++i)//输入第二	个数组元素
    {
        scanf("%d", &value);
        arr2[value] = 1 ;//出现位置的值等于一,这样可以去掉重复的元素
    }
    
    for (int i = 0; i < N; i++) { // 将两个数字的值加起来, 因为要求的是非共有的元素。
        arr1[i] = arr1[i] + arr2[i]; //当出现位置大于1时,说明两个数组中共有
    }

    for (int i = 0; i < N; ++i) //从1开始,因为第0为是大小
    {
        if(arr1[i] == 1) // 如果只出现一次,说明不是重复的元素
        {
            arr2[len] = i;
            len ++ ; //记录长度
        }
    }
     //排序, 这里没有调用冒泡排序,因为输入时已经使用了桶排序,不过你可以看看冒泡排序
    //    bubbleSort(arr2, len);
    printArr(arr2, len);
    return 0;
    
}
Beispiel #8
0
int main(void)
{   
  
    extern int flag;
    int i, j;
    int arr[HIGHT][WIDTH] = {0};
    int ch;

    initArr(arr);

    initscr();/* Start curses mode */
    cbreak();/* Line buffering disabled, Pass on * everty thing to me */
    keypad(stdscr, TRUE);/* I need that nift                        y F1 */
    noecho();
    start_color();/* Start the color functionality */

    printWin(arr);

    /* Initialize the window parameters */
    mvprintw(0,0,"%s","Press F1 to exit");
    while((ch = getch()) != KEY_F(1))
    {
        switch(ch)
        {
            case KEY_LEFT:
            case 'h':
            leftArrow(arr);
            printWin(arr);
            break;
            case KEY_RIGHT:
            case 'l':
            rightArrow(arr);
            printWin(arr);
            break;
            case KEY_UP:
            case 'k':
            upArrow(arr);
            printWin(arr);
            break;
            case KEY_DOWN:
            case 'j':
            downArrow(arr);
            printWin(arr);
            break;
        }
    }
    endwin();
    return 0;
}   
Beispiel #9
0
int	main()
{
	int	n;
	clock_t start,stop;

	initArr(arr,N);
	
	start=clock();
	SortArr	(arr);
	stop=clock();
	
	/*if((n=errors(arr,N)))
		printf("Se encontraron %d errores\n",n);
	else
		*/printf("%d elementos ordenados en %1.2f segundos\n",N,((float)stop-(float)start)/1000.0);
		
	Sleep(3000);
}
Beispiel #10
0
int main() {

    int arr[100][50] = {0};
    int res[105] = {0};
    int i, j, tmp;
    initArr(arr);

    for (i=0; i<100; i++) {
        for (j=0; j<50; j++) {
            printf("arr[%d][%d] is %d\n", i, j, arr[i][j]);
        }
    }

    for (j=49; j>=0; j--) {
        for (i=0; i<100; i++) {
            res[j+55] += arr[i][j];
        }
    }

    tmp = 0;
    for (i=104; i>0; i--) {
        printf("res[%d] is %d, ", i, res[i]);
        tmp = res[i];
        res[i] %= 10;
        printf("after process res[%d] is %d, ", i, res[i]);
        printf("before process res[%d] is %d, ", i-1, res[i-1]);
        if (tmp > 10) {
            res[i-1] += tmp/10;
            printf("after process res[%d] is %d\n", i-1, res[i-1]);
        }
    }

    for (i=0; i<105; i++) {
        printf("%d", res[i]);
    }

    return 0;
}
Beispiel #11
0
int main() {
  int i, j;
  struct timespec tStart;
  struct timespec tEnd;

  initArr(X);
  //printArr(X);
  
  double delta = 0.0;
  int numIters = 0;

  clock_gettime(CLOCK_REALTIME, &tStart);

  do {
    numIters += 1;

    //
    // TODO: implement the stencil computation here
    //

   #pragma omp parallel for default(none) private(i,j) shared(X,Y)
   for (i=1; i <= N; ++i) {
      for (j=1; j <= N; ++j) {
         double center = X[i][j] * 0.25;
         double adjacent = (X[i-1][j] + X[i+1][j] + X[i][j-1] + X[i][j+1]) * 0.125;
         double diagonals = (X[i-1][j-1] + X[i+1][j-1] + X[i-1][j+1] + X[i+1][j+1]) * 0.0625;

         Y[i][j] = 
            (center + adjacent + diagonals);
      }
   }

    // TODO: implement the computation of delta and get ready for the
    // next iteration here...
    //
    // 1) check for termination here by computing delta -- the largest
    //    absolute difference between corresponding elements of X and Y
    //    (i.e., the biggest change due to the application of the stencil 
    //    for this iteration of the while loop)
    //
    // 2) copy Y back to X to set up for the next execution
    //

   //Compute Delta
   delta = 0.0;
   #pragma omp parallel for default(none) private(j) shared(X,Y) reduction(max:delta)
   for (i=1; i <= N; ++i) {
      for (j=1; j <= N; ++j) {
         double temp = fabs(Y[i][j] - X[i][j]);
         if (delta < temp) delta = temp;
      }
   }

   #pragma omp parallel for default(none) private(j) shared(X,Y)
   // copy Y back to X to setup for next iteration
   for (i=1; i <=N; ++i) {
      for (j=1; j <=N; ++j) {
         X[i][j] = Y[i][j];
      }
   }

  } while (delta > epsilon);

  clock_gettime(CLOCK_REALTIME, &tEnd);
  
  //printArr(X);

  printf("Took %d iterations to converge\n", numIters);
  struct timespec tsDiff = diff(tStart, tEnd);
  //printf("Elapsed Time: %d.%09d Sec.\n", tsDiff.tv_sec, tsDiff.tv_nsec);
  printf("%d.%09d\n",tsDiff.tv_sec, tsDiff.tv_nsec);

}