- Kiểm tra ma phương (Magic Square) trong C
[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]
/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/
#include<stdio.h>
#include<conio.h>
int main() {
int size = 3;
int matrix[3][3]; // = {{4,9,2},{3,5,7},{8,1,6}};
int row, column = 0;
int sum, sum1, sum2;
int flag = 0;
printf("\nNhap ma tran: ");
for (row = 0; row < size; row++) {
for (column = 0; column < size; column++)
scanf("%d", &matrix[row][column]);
}
printf("In ma tran vua nhap: \n");
for (row = 0; row < size; row++) {
printf("\n");
for (column = 0; column < size; column++) {
printf("\t%d", matrix[row][column]);
}
}
//Kiem tra tong phan tu tren cac duong cheo
sum = 0;
for (row = 0; row < size; row++) {
for (column = 0; column < size; column++) {
if (row == column)
sum = sum + matrix[row][column];
}
}
//Kiem tra tong phan tu tren cac hang
for (row = 0; row < size; row++) {
sum1 = 0;
for (column = 0; column < size; column++) {
sum1 = sum1 + matrix[row][column];
}
if (sum == sum1)
flag = 1;
else {
flag = 0;
break;
}
}
//Kiem tra tong phan tu tren cac cot
for (row = 0; row < size; row++) {
sum2 = 0;
for (column = 0; column < size; column++) {
sum2 = sum2 + matrix[column][row];
}
if (sum == sum2)
flag = 1;
else {
flag = 0;
break;
}
}
if (flag == 1)
printf("\nDay so nay la Magic square!");
else
printf("\nDay so nay khong phai la Magic square!");
return 0;
}
[/bg_collapse]
- Trừ hai ma trận trong C
[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]
/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/
#include<stdio.h>
#include <cstdlib>
int main() {
int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
int row1, col1, row2, col2;
printf("\nNhap so hang cua Mat1: ");
scanf("%d", &row1);
printf("\nNhap so cot cua Mat1: ");
scanf("%d", &col1);
printf("\nNhap so hang cua Mat2: ");
scanf("%d", &row2);
printf("\nNhap so cot cua Mat2: ");
scanf("%d", &col2);
/* Truoc khi nhap cac phan tu, kiem tra xem so hang va so cot
cua hai ma tran co bang nhau khong */
if (row1 != row2 || col1 != col2) {
printf("\nHai ma tran khong cung kich co!!!");
exit(0);
}
//Nhap cac phan tu cua ma tran 1
printf("\nNhap ma tran 1\n");
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("Nhap phan tu a[%d][%d]: ", i, j);
scanf("%d", &mat1[i][j]);
}
}
//Nhap cac phan tu cua ma tran 2
printf("\nNhap ma tran 2\n");
for (i = 0; i < row2; i++)
for (j = 0; j < col2; j++) {
printf("Nhap phan tu b[%d][%d]: ", i, j);
scanf("%d", &mat2[i][j]);
}
//Tru hai ma tran
for (i = 0; i < row1; i++)
for (j = 0; j < col1; j++) {
mat3[i][j] = mat1[i][j] - mat2[i][j];
}
//Hien thi ma tran ket qua
printf("\nKet qua cua phep tru hai ma tran la: \n");
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("%d\t", mat3[i][j]);
}
printf("\n");
}
return (0);
}
[/bg_collapse]
- Cộng hai ma trận trong C
[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]
/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/
#include<stdio.h>
#include <cstdlib>
int main() {
int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
int row1, col1, row2, col2;
printf("\nNhap so hang cua Mat1: ");
scanf("%d", &row1);
printf("\nNhap so cot cua Mat1: ");
scanf("%d", &col1);
printf("\nNhap so hang cua Mat2: ");
scanf("%d", &row2);
printf("\nNhap so cot cua Mat2: ");
scanf("%d", &col2);
/* Truoc khi nhap cac phan tu, kiem tra xem so hang va so cot
cua hai ma tran co bang nhau khong */
if (row1 != row2 || col1 != col2) {
printf("\nHai ma tran khong cung kich co!!!");
exit(0);
}
//Nhap cac phan tu cua ma tran 1
printf("\nNhap ma tran 1\n");
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("Nhap phan tu a[%d][%d] : ", i, j);
scanf("%d", &mat1[i][j]);
}
}
//Nhap cac phan tu cua ma tran 2
printf("\nNhap ma tran 2\n");
for (i = 0; i < row2; i++)
for (j = 0; j < col2; j++) {
printf("Nhap phan tu b[%d][%d] : ", i, j);
scanf("%d", &mat2[i][j]);
}
//Cong hai ma tran
for (i = 0; i < row1; i++)
for (j = 0; j < col1; j++) {
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
//Hien thi ma tran ket qua
printf("\nKet qua cua phep cong hai ma tran la: \n");
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("%d\t", mat3[i][j]);
}
printf("\n");
}
return (0);
}
[/bg_collapse]
- Tính đường chéo của ma trận trong C
[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]
/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/
#include<stdio.h>
int main() {
int i, j, mat[10][10], row, col;
int sum = 0;
printf("\nNhap so hang cua ma tran: ");
scanf("%d", &row);
printf("\nNhap so cot cua ma tran: ");
scanf("%d", &col);
//Nhap cac phan tu cua ma tran m x n
printf("\nNhap ma tran: \n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("\nNhap phan tu a[%d][%d] : ", i, j);
scanf("%d", &mat[i][j]);
}
}
//Cong tat ca phan tu tren duong cheo chinh
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (i == j)
sum = sum + mat[i][j];
}
}
//Hien thi ket qua
printf("\nTong duong cheo cua ma tran la: %d", sum);
return (0);
}
[/bg_collapse]
- Chương trình C để tính tổng tất cả các phần tử của ma trận
[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]
/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/
#include<stdio.h>
int main() {
int i, j, mat[10][10], row, col;
int sum = 0;
printf("\nNhap so hang cua ma tran: ");
scanf("%d", &row);
printf("\nNhap so cot cua ma tran: ");
scanf("%d", &col);
//Nhap cac phan tu cua ma tran
printf("\nNhap ma tran: \n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("\nNhap phan tu mat[%d][%d] : ", i, j);
scanf("%d", &mat[i][j]);
}
}
//Cong tat ca cac phan tu cua ma tran
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
sum = sum + mat[i][j];
}
}
//Hien thi ket qua
printf("\nTong tat ca cac phan tu cua ma tran la: %d", sum);
return (0);
}
[/bg_collapse]
- Tìm ma trận chuyển vị trong C
[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]
/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/
#include<stdio.h>
#include<conio.h>
int main() {
int arr[10][10], size, i, j, temp;
printf("\nNhap kich co cua ma tran: ");
scanf("%d", &size);
printf("\nNhap ma tran:\n");
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
scanf("%d", &arr[i][j]);
}
}
printf("\nMa tran vua nhap la: \n");
for (i = 0; i < size; i++) {
printf("\n");
for (j = 0; j < size; j++) {
printf("%d\t", arr[i][j]);
}
}
/* Tim ma tran chuyen vi */
for (i = 1; i < size; i++) {
for (j = 0; j < i; j++) {
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
printf("\nMa tran chuyen vi cua ma tran da cho la:\n");
for (i = 0; i < size; i++) {
printf("\n");
for (j = 0; j < size; j++) {
printf("%d\t", arr[i][j]);
}
}
return (0);
}
[/bg_collapse]
- Chương trình C để tìm ma trận nghịch đảo
[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]
/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/
#include<stdio.h>
//viet mot ham de tim cac phan tu cua ma tran nghich dao
// a 3 i i
void reduction(float a[][6], int size, int pivot, int col) {
int i, j;
float factor;
factor = a[pivot][col];
for (i = 0; i < 2 * size; i++) {
a[pivot][i] /= factor;
}
for (i = 0; i < size; i++) {
if (i != pivot) {
factor = a[i][col];
for (j = 0; j < 2 * size; j++) {
a[i][j] = a[i][j] - a[pivot][j] * factor;
}
}
}
}
int main() {
float matrix[3][6];
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 6; j++) {
if (j == i + 3) {
matrix[i][j] = 1;
} else {
matrix[i][j] = 0;
}
}
}
printf("\nNhap ma tran co kich co 3 X 3:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%f", &matrix[i][j]);
}
}
for (i = 0; i < 3; i++) {
reduction(matrix, 3, i, i);
}
printf("\nMa tran nghich dao cua ma tran da cho la:\n");
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%8.3f", matrix[i][j + 3]);
}
}
return(0);
}
[/bg_collapse]
- Nhân hai ma trận trong C
[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]
/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;
printf("\nNhap ma tran dau tien:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\nNhap ma tran thu hai:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}
printf("Ma tran dau tien: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", a[i][j]);
}
printf("\n");
}
printf("Ma tran thu hai: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", b[i][j]);
}
printf("\n");
}
//phep nhan ma tran
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
sum = 0;
for (k = 0; k <= 2; k++) {
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
printf("\nTich cua hai ma tran la: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", c[i][j]);
}
printf("\n");
}
return (0);
}
[/bg_collapse]