int main() { int n; // size of square matrix int **A=NULL,**B=NULL,**C=NULL; int **TA=NULL; // TA = traspose(A) printf("Please input the size of matrix:"); scanf("%d",&n); _flushall(); // Call the function - AllocIntMatrix to get the memory for matrix A and B // --- write your codes here --- A=AllocIntMatrix(n); B=AllocIntMatrix(n); int i,j; // Ask the user to enter the matrix printf("A=\n"); // --- write your codes here to get matrix A--- for(i=0; i<n; i++) for(j=0; j<n; j++) scanf(" %d", &A[i][j]); printf("B=\n"); // --- write your codes here to get matrix B--- for(i=0; i<n; i++) for(j=0; j<n; j++) scanf(" %d", &B[i][j]); // call the two functions for transpose(A) and C = transpose(A)*B TA=matrix_transpose(&A[0], n); // share the ownership of the heap memory (i.e., A) with matrix_transpose() by call by reference (pointer) // obtain the ownership of the heap memory (i.e., TA) requested in matrix_transpose() //multiplication C=matrix_multiplication(&TA[0], &B[0], n);// share the ownership of the heap memory (i.e., A, B) with matrix_multiplication() by call by reference (pointer) // obtain the ownership of the heap memory (i.e., C) requested in matrix_multiplication() system("cls"); printf("A=\n"); // --- write your codes here to print out A --- int CNT=0; for(i=0; i<n; i++){ for(j=0; j<n; j++){ printf("%d ", A[i][j]); CNT++; if(CNT == n){ printf("\n"); CNT=0; } } } printf("\n"); printf("TA=\n"); // --- write your codes here to print out TA --- for(i=0; i<n; i++){ for(j=0; j<n; j++){ printf("%d ", TA[i][j]); CNT++; if(CNT == n){ printf("\n"); CNT=0; } } } printf("\n"); printf("B=\n"); // --- write your codes here to print out B --- for(i=0; i<n; i++){ for(j=0; j<n; j++){ printf("%d ", B[i][j]); CNT++; if(CNT == n){ printf("\n"); CNT=0; } } } printf("\n"); printf("C=\n"); // --- write your codes here to print out C --- for(i=0; i<n; i++){ for(j=0; j<n; j++){ printf("%d ", C[i][j]); CNT++; if(CNT == n){ printf("\n"); CNT=0; } } } // release heap memory explicitly preventing memory leakage FreeIntMatrix(A ,n); // release A FreeIntMatrix(B ,n); // relase B FreeIntMatrix(C ,n); // relase C FreeIntMatrix(TA ,n); // release TA A = B = C = TA = NULL; system("PAUSE"); return 0; }
void CostCalculator::OptimizeCost(char ** haplotypes, int count, int markers) { LongCounter uniqueHaplotypes; int bits = sizeof(long long) * 8 - 1; int limit = count / 2; long long * individualHaplotypes = new long long [count]; int ** uniqueCounts = AllocateIntMatrix(markers, bits); // First we construct a matrix with the number of unique haplotypes // along different portions of the current solution for (int i = 0; i < markers; i++) { uniqueHaplotypes.Clear(); // Retrieve one marker haplotypes for (int j = 0; j < count; j++) { individualHaplotypes[j] = haplotypes[j][i]; uniqueHaplotypes.IncrementCount(individualHaplotypes[j]); } // Count the number of unique haplotypes uniqueCounts[i][0] = uniqueHaplotypes.Entries(); for (int j = 1; j < bits; j++) { if (uniqueHaplotypes.Entries() > limit || i + j >= markers) { uniqueCounts[i][j] = count; continue; } uniqueHaplotypes.Clear(); for (int k = 0; k < count; k++) { individualHaplotypes[k] = individualHaplotypes[k] * 2 + haplotypes[k][i+j]; uniqueHaplotypes.IncrementCount(individualHaplotypes[k]); } uniqueCounts[i][j] = uniqueHaplotypes.Entries(); } } // Finally, we use dynamic programming to find the best cost path if (cost != NULL) delete [] cost; if (path != NULL) delete [] path; cost = new double [markers]; path = new int [markers]; cost[0] = BasicCost(count); path[0] = 0; for (int i = 1; i < markers; i++) { cost[i] = cost[i - 1] + BasicCost(count); path[i] = i; for (int j = 1; j < bits; j++) if (i - j >= 0) { double alternate_cost = cost[i - j] + ReducedCost(uniqueCounts[i-j][j], count) * j + TranslationCost(count); if (alternate_cost < cost[i]) { cost[i] = alternate_cost; path[i] = i - j; } } } #ifdef _DEBUG // Estimate savings double naiveCost = markers * BasicCost(count); printf("Reduced state space optimization would result in a speedup of about %.1f-fold\n", naiveCost / cost[markers-1]); int position = markers - 1; printf(" Optimal Path: "); while (position != 0) if (path[position] == position) printf(" Step(%d) ", position--); else { printf(" Condense(%d -> %d)", position, path[position]); position = path[position]; } printf("\n"); #endif // We are all done, free memory FreeIntMatrix(uniqueCounts, markers); delete [] individualHaplotypes; }