示例#1
0
int make_choice(char choice, int* matrixA, int* matrixB, int* result , int size){
	int scale_factor = 0;
	switch (choice)
	{
	case 'A': case 'a':
		_add(matrixA,matrixB,result,size);
		print_square_matrix(result, size);
		break;
	case 'S': case 's':
		printf("Result of Matrix1's Sum = %d", sum(matrixA, size));
		break;
	case 'M': case 'm':
		mult(matrixA,matrixB,result,size);
		print_square_matrix(result,size);
		break;
	case 'C': case 'c':
	    printf("Enter scale factor: "); 
	    scanf("%d", &scale_factor); getc(stdin);
	    printf("\n");
		scale(matrixA, scale_factor, result, size);
		print_square_matrix(result, size);
		break;
	case 'Q': case 'q':
		square(matrixA,result,size);
		print_square_matrix(result,size);
		
		break;
	case 'B': case 'b':
		if(size<11 || size > 80){
		printf("Size is not between 11 and 80");
	}
		else{
		itu(matrixA,size);
		print_square_matrix(matrixA,size);
	}
		break;
	case 'E': case 'e':
		return 0;
	default:
		break;
	}
	printf("\n");
	return 1;
}
示例#2
0
文件: main.c 项目: potacle/exercises
void chapter1_exercises() {
	char * all_uniques = "abc defg";
	char * not_all_unique = "abcdefa";
	char * all_uniques_perm = "adebcfg";
	char * not_all_unique_perm = "debcafa";
	char * nullptr = 0;
	char * data_outofrange = "abcedéabcde";
	char * smalldata = "a";
	char * a_string_with_spaces = " hello world   this is a string ";
	char * a_string_with_repeats =
			"aaabbcddddeeffffffffffffffffffffffffghijkkkkk";
	char * bigdata =
			"abcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefgabcdefg";
	char tmp1[2048];
	char tmp2[2048];
	int matrix_5x5[5][5] = { { 0, 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 }, { 10, 11, 12,
			13, 14 }, { 15, 16, 17, 18, 19 }, { 20, 21, 22, 23, 24 } };

	int matrix[3][5] = { { 0, 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 }, { 10, 11, 12, 13,
			14 } };

	// chapter 1, exercise 1
	// solution 1 : brute force, O(n^2) time complexity
	printf("Null test: %d\n", ch1_1_hasAllUniques1(nullptr));
	printf("All uniques test: %d\n", ch1_1_hasAllUniques1(all_uniques));
	printf("Not all uniques test: %d\n", ch1_1_hasAllUniques1(not_all_unique));
	printf("Not all uniques test: %d\n", ch1_1_hasAllUniques1(not_all_unique));
	printf("small data test: %d\n", ch1_1_hasAllUniques1(smalldata));
	printf("big data test: %d\n", ch1_1_hasAllUniques1(smalldata));

	// solution 2 : use array to flag duplicates, O(n) time complexity for large strings
	printf("Null test: %d\n", ch1_1_hasAllUniques2(nullptr));
	printf("Data out of range test: %d\n",
			ch1_1_hasAllUniques2(data_outofrange));
	printf("All uniques test: %d\n", ch1_1_hasAllUniques2(all_uniques));
	printf("Not all uniques test: %d\n", ch1_1_hasAllUniques2(not_all_unique));
	printf("small data test: %d\n", ch1_1_hasAllUniques2(smalldata));
	printf("big data test: %d\n", ch1_1_hasAllUniques2(smalldata));

	// chapter 1, exercise 2
	// solution 1 : swap characters (1 with N, 2 with N-1, ... N/2 with N-N/2)
	ch1_2_reverse(nullptr);
	printf("Null test passed\n");

	strcpy(tmp1, all_uniques);
	ch1_2_reverse(tmp1);
	printf("simple reverse test: %s\n", tmp1);

	strcpy(tmp1, smalldata);
	ch1_2_reverse(tmp1);
	printf("small data test: %s\n", tmp1);

	strcpy(tmp1, bigdata);
	ch1_2_reverse(tmp1);
	printf("big data test: %s\n", tmp1);

	// chapter 1, exercise 3
	// solution 1 - sort the strings
	// solution 2 - count the characters
	printf("null test: %d\n", ch1_3_is_a_permutation2(nullptr, nullptr));

	strcpy(tmp1, all_uniques);
	strcpy(tmp2, not_all_unique);
	printf("not a permutation test: %d\n", ch1_3_is_a_permutation2(tmp1, tmp2));

	strcpy(tmp2, smalldata);
	printf("not a permutation lengths not equal test: %d\n",
			ch1_3_is_a_permutation2(tmp1, tmp2));

	strcpy(tmp1, all_uniques);
	strcpy(tmp2, all_uniques_perm);
	printf("simple permutation test: %d\n",
			ch1_3_is_a_permutation2(tmp1, tmp2));

	strcpy(tmp1, not_all_unique);
	strcpy(tmp2, not_all_unique_perm);
	printf("simple permutation test with duplicates: %d\n",
			ch1_3_is_a_permutation2(tmp1, tmp2));

	// chapter 1, exercise 4
	strcpy(tmp1, a_string_with_spaces);
	ch1_4_replace_spaces(tmp1, strlen(a_string_with_spaces));
	printf("replaced spaces: %s\n", tmp1);

	// chapter 1, exercise 5
	strcpy(tmp1, a_string_with_repeats);
	ch1_5_compress_string(tmp1);
	printf("compressed string: %s\n", tmp1);

	printf("original matrix\n");
	print_square_matrix(5, matrix_5x5);
	printf("\n");

	printf("rotated matrix\n");
	ch1_6_rotate_square_inplace(5, matrix_5x5);
	print_square_matrix(5, matrix_5x5);
	printf("\n");

	// chapter 1, exercise 7
	print_matrix(3, 5, matrix);
	ch1_7_zero_matrix(3, 5, matrix);
	print_matrix(3, 5, matrix);

	// chapter 1, exercise 8
	printf("rotation test 1: %d\n",
			ch1_8_is_a_rotation("waterbottle", "erbottlewat"));
	printf("rotation test 2: %d\n",
			ch1_8_is_a_rotation("waerbottle", "erbottlewat"));

}
示例#3
0
int main(int argc, char *argv[]) {
	MPI_Init(&argc, &argv);

	double starttime, endtime;
	starttime = MPI_Wtime();

	/* Rank of the current process. */
	int rank;

	/* Number of processes. */
	int p;

	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &p);

	/*
	 * Check if the input arguments are okay.
	 */
	if(argc != 4) {
		if (rank == 0) {
			printf("Usage: %s <matrix1.txt> <matrix2.txt> <outfile.txt>\n", argv[0]);
		}
		MPI_Finalize();
		exit(1);
	}

	int **m1 = NULL, **m2 = NULL;
	int **m3 = NULL;

	int _n[2];

	/*
	 * Allocate and read the input matrices.
	 */
	if (rank == 0) {
		m1 = read_square_matrix(argv[1], &_n[0]);
		m2 = read_square_matrix(argv[2], &_n[1]);
	}

	MPI_Bcast(_n, 2, MPI_INT, 0, MPI_COMM_WORLD);

	/*
	 * Check if the multiplication can be done, depending on the order of
	 * the matrices.
	 */
	if(_n[0] != _n[1]) {
		if(rank == 0) {
			fprintf(stderr, "Error: the matrices must have the same	order!\n");
		}
		MPI_Finalize();
		exit(1);
	}

	int n = _n[0];

	/*
	 * Check whether the number of processes divide the matrices order or
	 * not.
	 */
	if((n % p) != 0) {
		if(rank == 0) {
			fprintf(stderr, "Error: order of the matrix is not divisible by number of processes, as assumed");
		}
		MPI_Finalize();
		exit(1);
	}

	if(rank != 0) {
		m1 = alloc_square_matrix(n * n);
		m2 = alloc_square_matrix(n * n);
	}

	/* Broadcast the second matrix to every process. */
	for(int i = 0; i < n; ++i) {
		MPI_Bcast(&m2[i][0], n, MPI_INT, 0, MPI_COMM_WORLD);
	}
	
#ifdef DEBUG
	print_square_matrix(m2, n);
#endif

	int rows_for_each = n / p;
	MPI_Request *ireq = (MPI_Request*) malloc(n * sizeof(MPI_Request));

	/* N/A: MPI_Scatter(m1, rows_for_each * n, MPI_INT, m1cc, rows_for_each * n, MPI_INT, 0, MPI_COMM_WORLD);*/
	/*
	 * Send some rows of the first matrix to each process.
	 */
	if (rank == 0) {
		for(int i = 0; i < p; ++i) {
			for(int j = 0; j < rows_for_each; ++j) {
				MPI_Isend(&m1[i * rows_for_each + j][0], n, MPI_INT, i, DUMMY_TAG, MPI_COMM_WORLD, &ireq[i * rows_for_each + j]);
			}
		}
	}

	 /*
	  * Receive some rows of the first matrix from the master process.
	  */
	for(int j = 0; j < rows_for_each; ++j) {
		MPI_Recv(&m1[rank * rows_for_each + j][0], n, MPI_INT, 0, DUMMY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
	}

#ifdef DEBUG
	for(int i = 0; i < n; ++i) {
		printf("Process #%d, row %d: ", rank, i);
		for (int j = 0; j < n; ++j) {
			printf("%d ", m1[i][j]);
		}
		puts("");
	}
#endif

	m3 = alloc_square_matrix(n);

	/*
	 * Here the (partial) multiplication happens in each process.
	 */
	for(int i = 0; i < rows_for_each; ++i) {
		int posicaoi = rank * rows_for_each + i;
		for(int j = 0; j < n; ++j) {
			int cell = 0;
			for(int k = 0; k < n; ++k) {
				cell += m1[posicaoi][k] * m2[k][j];
			}
			m3[posicaoi][j] = cell;
		}
	}

#ifdef DEBUG
	for(int i = 0; i < n; ++i) {
		printf("Process #%d, row %d: ", rank, i);
		for (int j = 0; j < n; ++j) {
			printf("%d ", m3[i][j]);
		}
		puts("");
	}
#endif

	/*
	 * Each process send the information back to the master process.
	 */
	for(int j = 0; j < rows_for_each; ++j) {
		MPI_Isend(&m3[rank * rows_for_each + j][0], n, MPI_INT, 0, DUMMY_TAG, MPI_COMM_WORLD, &ireq[rank * rows_for_each + j]);
	}

	/*
	 * The master process receives information from the other processes
	 * (including itself).
	 */
	if (rank == 0) {
		for(int i = 0; i < p; ++i) {
			for(int j = 0; j < rows_for_each; ++j) {
				MPI_Recv(&m3[i * rows_for_each + j][0], n, MPI_INT, i, DUMMY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
			}
		}
	}

	endtime = MPI_Wtime();

	if (rank == 0) {
#ifdef DEBUG
		print_square_matrix(m3, n);
#endif
		write_square_matrix(m3, n, argv[3]);
	}

	if (rank == 0) {
		printf("Process #%d: total time: %lf seconds\n", rank, endtime - starttime);
	}

	if(ireq)
		free(ireq);

	if(m1)
		unalloc_square_matrix(m1, n);

	if(m2)
		unalloc_square_matrix(m2, n);

	if(m3)
		unalloc_square_matrix(m3, n);

	MPI_Finalize();

	return 0;
}