예제 #1
0
void Demo(){
	system("cls"); //clear screen
	printf("How many discs(1-10) do you want to solve?\n>>");
	int n;
	float m;
	scanf("%d", &n); //get the number of discs the user wants
	fillA(n);        //fill stack A with the specified number
	printf("What is you preferred playback speed:1(fast)-10(slow)\n>>");
	scanf("%f", &m); // get the preferred playback speed
	playback = m/5;  //scale it down to usable value
	towers(n, headA, headC, headB); //solve the towers of hanoi problem
	printf("\nReturning to main menu...");
	wait(2);    //wait for 2 seconds then return to the main menu
	return;
}
예제 #2
0
void transpose (Real **A, int m, int n, int size, int bb, int bre)
{
  int se[size], sd[size], re[size], rd[size];
  Real *V = createRealArray (n*m);
  Real *Vt = createRealArray (n*m);

  for (int i = 0; i < size; ++i) {
    se[i] = bb;
    sd[i] = bb*i;
    re[i] = bb;
    rd[i] = bb*i;
  }
  se[size-1] = bre;
  re[size-1] = bre;

  for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
      V[j + i*m] = A[j][i];
    }
  }

  MPI_Alltoallv(V, se, sd, MPI_DOUBLE, Vt, re, rd, MPI_DOUBLE, MPI_COMM_WORLD);
  fillA(A, Vt, re, rd, m, n, size);
}
예제 #3
0
//play mode
void play(){
	system("cls");
	printf("How many discs do you want to solve?\n>>");
	int n, val, moves=0;
	char choice;
	char name[15];
	scanf("%d", &n);  //get number of discs to solve from user
	fillA(n);         //fill stack A with the number, n
	time_t start;     //declare time variables used to count/time how long it takes to play the game till completion
	time_t end;
	float duration;
	time(&start);    //get the time at which he starts playing the game
	while(countNodes() != n){   //loops play for as long as stack C is not yet full. Game ends when Stack C is full
        int p = 1;
        while(p==1){
			system("cls");
			printStacks();     //print the stacks on the screen
			printf("\nPress A to pop from Stack A\nPress B to pop from Stack B\nPress C to pop from Stack C\n>>");
			choice = getch();
		
			switch(choice){
				case 'A':      //if he enters 'a' or 'A', pop disc from stack A
				case 'a':
					if(headA->next==tailA)break; //if stack A is empty, don't pop
					printf("popping from A");
                    val = pop(headA);            //pop node from stack A
					p--;
					break;
				case 'B':     //if he enters 'b' or 'B', pop disc from stack B
				case 'b':
					if(headB->next==tailB)break;   //if stack B is empty, don't pop
            	    printf("popping from B");
					val = pop(headB);              //pop node from stack B
					p--;
					break;
				case 'C':     //if he enters 'c' or 'C', pop disc from stack C
				case 'c':
					if(headC->next==tailC)break;  //if stack C is empty, don't pop
            	    printf("popping from C");
					val = pop(headC);             //pop node from stack C
					p--;
					break;
				default:
					printf("\b"); //if any other input is entered, delete it
					break;
			}
		}
		p=1;
		while(p==1){
			system("cls");    //clear the screen
  			printStacks();    //print the current state of the stacks
			printf("\nPress A to push to Stack A\nPress B to push to Stack B\nPress C to push to Stack C\n>>");
			choice = getch();
			switch(choice){
				case 'A':     //if he enter 'A' or 'a', push disc onto Stack A
				case 'a':
					if(val>headA->next->size)break; //prevent the user from pushing a bigger disc onto the one on stack A
					push(val, headA);               //push disc onto stack A
					p--;
					break;
				case 'B':     //if he enters 'B' or  'b', push disc onto stack B
				case 'b':
                    if(val>headB->next->size)break; //prevent the user from pushing a bigger disc onto the one on stack B
					push(val, headB);               //push disc onto stack B
					p--;
					break;
				case 'C':      //if he enter 'C' or 'c', push disc onto stack C
				case 'c':
                    if(val>headC->next->size)break; //prevent the user from pushing a bigger disc onto the one on stack C
					push(val, headC);               //push disc onto stack C
					p--;
					break;
				default:
					printf("\b"); //if any invalid input is entered, delete it
					break;
			}
		}
		moves++;
	}
	printStacks(); //print the current state of the stacks
	time(&end);    //get the time at which he completes the game
	duration = difftime(end, start);  //calculate the duration of gameplay
	printf("\nCongratulations!!\nCompleted in %d moves! (%.0f seconds)\n", moves, duration);
	printf("\nEnter you name: ");  //prompt for player's name
	scanf("%s", name);             //save the name
	FILE *fptr=NULL;               //create a file pointer
	fptr = fopen("HallofFame.txt", "a" );  //open HallofFame.txt to add details of the game
	fprintf(fptr, "%s,%d,%d,%.0f\n", name, n, moves, duration); //save player's info in the HallofFame.txt file
	fclose(fptr);                 //close the file pointer
	system("pause");
}
예제 #4
0
void RKWidget::step ( const size_t nStep )
{
    DNformat *Bstore;
    DNformat *Xstore;
    double temp;
    if(unstable) return;
    samset = false;
    if( dirty ) {
        if(aexist) {
            Destroy_CompCol_Matrix(&A);
            if ( lwork == 0 ) {
                Destroy_SuperNode_Matrix(&L);
                Destroy_CompCol_Matrix(&Up);
            } else if ( lwork > 0 ) {
                SUPERLU_FREE(work);
            }
            // these may be freed in dgssvx or Destroy_CompCol_Matrix I think
            aexist = false;
        }
        a = new double[nvar*N_];
        xa = new int[N_+1];
        asub = new int[nvar*N_];
        updateCoef(method);
        if(nblock == 1) {
            // load with coef[] ???
            fillA();
        } else if (nup+ndn == 0) {
            // solve seperate independent blocks???
        } else {
            // load block system
            blockFillA();
        }

        dCreate_CompCol_Matrix(&A, N_, N_, nnz, a, asub, xa, SLU_NC, SLU_D, SLU_GE);
        aexist = true;
        /* Initialize the statistics variables. */
        StatInit(&stat);

        dPrint_CompCol_Matrix("A matrix", &A);
        options.Fact = DOFACT;
        //options.ColPerm=NATURAL;
        options.ColPerm=COLAMD;
        options.PivotGrowth = NO;
        options.ConditionNumber = NO;
        /* ONLY PERFORM THE LU DECOMPOSITION */
        B.ncol = 0;  /* Indicate not to solve the system */
        dgssvx(&options, &A, perm_c, perm_r, etree, equed, R, C,
               &L, &Up, work, lwork, &B, &X, &rpg, &rcond, ferr, berr,
               &mem_usage, &stat, &info);
        //dPrint_CompCol_Matrix("A matrix", &A);
        printf("LU factorization: dgssvx() returns info %d\n", info);
        if ( info == 0 || info == N_+1 ) {

            if ( options.PivotGrowth ) printf("Recip. pivot growth = %e\n", rpg);
            if ( options.ConditionNumber )
                printf("Recip. condition number = %e\n", rcond);

            printf("L\\U_ MB %.3f\ttotal MB needed %.3f\n",
                   mem_usage.for_lu/1e6, mem_usage.total_needed/1e6);
            fflush(stdout);
            options.Fact = FACTORED; /* Indicate the factored form of A is supplied. */
            B.ncol = 1;
            dirty = false;
        } else if ( info > 0 && lwork == -1 ) {
            printf("** Estimated memory: %d bytes\n", info - n);
        }
        if ( options.PrintStat ) StatPrint(&stat);
        StatFree(&stat);
    }
    for ( size_t n = 0; n < nStep; n++ ) {
        for( int ns = 0; ns < nStage; ns++ ) {
            ///set B matrix
            Bstore= (DNformat *)B.Store;
            rhsb=(double*)Bstore->nzval;
            if (nup+ndn == 0) {
                // solve seperate independent blocks???
                std::cout << "Discontinuous Galerkin Not Implimented\n";
                return;
            } else {
                fillB(ns);
            }

            ///solve factored system
            StatInit(&stat);
            options.Fact = FACTORED;
            dgssvx(&options, &A, perm_c, perm_r, etree, equed, R, C,
                   &L, &Up, work, lwork, &B, &X, &rpg, &rcond, ferr, berr,
                   &mem_usage, &stat, &info);

            //if( n == 1 ) printf("Triangular solve: dgssvx() returns info %d\n", info);

            ///update U_
            if ( info == 0 || info == N_+1 ) {

                /* This is how you could access the solution matrix. */
                Xstore = (DNformat *)X.Store;
                rhsx = (double*)(Xstore->nzval);
                for ( size_t i = 0; i <  N_ ; i++ ) {
                    if(rhsx[i]> 1e16) unstable = true;
                    b_k[i+N_*ns]=rhsx[i];
                }
            } else {
                std::cout << "ERROR: Matrix Solution Failed   info = " << info << std::endl;
            }
            StatFree(&stat);
        }
        for(int j=0 ; j<nStage; j++) {
            temp=b_b[j];
            if( temp != 0 ) {
                for(size_t i = 0; i <  N_ ; i++ ) {
                    U_[i] += temp*b_k[i+j*N_];
                }
            }
        }
        cStep++;
        totCFL += CFL;
    }
}