

Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
#include <stdio.h>
#define ROW 5
#define COL 3
int a[ROW][COL];
void init() {
for (int i = 0; i < ROW; i++)
for (int j = 0; j < COL; j++)
a[i][j] = 0; // ban đầu tắt hết
}
void printMatrix() {
printf("\nTrang thai den:\n");
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++)
printf("%d ", a[i][j]);
printf("\n");
}
}
void toggleRow(int r) {
for (int j = 0; j < COL; j++)
a[r][j] = !a[r][j];
}
void toggleCol(int c) {
for (int i = 0; i < ROW; i++)
a[i][c] = !a[i][c];
}
void togglePos(int r, int c) {
a[r][c] = !a[r][c];
}
int power() {
int sum = 0;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (a[i][j] == 1) {
if ((i % 2 == 0) && (j % 2 == 0))
sum += 10; // lẻ (index 0 là hàng 1)
else if ((i % 2 == 1) && (j % 2 == 1))
sum += 20; // chẵn
else
sum += 15;
}
}
}
return sum;
}
int main() {
int choice, r, c;
init();
do {
printf("\n===== MENU =====\n");
printf("1. Bat/tat theo hang\n");
printf("2. Bat/tat theo cot\n");
printf("3. Bat/tat theo vi tri\n");
printf("4. Tinh cong suat\n");
printf("5. Hien thi ma tran\n");
printf("0. Thoat\n");
printf("Chon: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Nhap hang (0-4): ");
scanf("%d", &r);
if (r >= 0 && r < ROW)
toggleRow(r);
break;
case 2:
printf("Nhap cot (0-2): ");
scanf("%d", &c);
if (c >= 0 && c < COL)
toggleCol(c);
break;
case 3:
printf("Nhap hang va cot: ");
scanf("%d %d", &r, &c);
if (r >= 0 && r < ROW && c >= 0 && c < COL)
togglePos(r, c);
break;
case 4:
printf("Tong cong suat: %d W\n", power());
break;
case 5:
printMatrix();
break;
}
} while (choice != 0);
return 0;
}
`\color{#7B2FF7}Z\color{#8E44F7}e\color{#A55CF6}l\color{#BB74F5}y\color{#D08CF4}t\color{#E6A4F3}h`
Hãy giúp mọi người biết câu trả lời này thế nào?
#include <stdio.h>
#define ROW 5
#define COL 3
int a[ROW][COL];
void init() {
for (int i = 0; i < ROW; i++)
for (int j = 0; j < COL; j++)
a[i][j] = 0; // ban đầu tắt hết
}
void printMatrix() {
printf("\nTrang thai den:\n");
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++)
printf("%d ", a[i][j]);
printf("\n");
}
}
void toggleRow(int r) {
for (int j = 0; j < COL; j++)
a[r][j] = !a[r][j];
}
void toggleCol(int c) {
for (int i = 0; i < ROW; i++)
a[i][c] = !a[i][c];
}
void togglePos(int r, int c) {
a[r][c] = !a[r][c];
}
int power() {
int sum = 0;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (a[i][j] == 1) {
if ((i % 2 == 0) && (j % 2 == 0))
sum += 10; // lẻ (index 0 là hàng 1)
else if ((i % 2 == 1) && (j % 2 == 1))
sum += 20; // chẵn
else
sum += 15;
}
}
}
return sum;
}
int main() {
int choice, r, c;
init();
do {
printf("\n===== MENU =====\n");
printf("1. Bat/tat theo hang\n");
printf("2. Bat/tat theo cot\n");
printf("3. Bat/tat theo vi tri\n");
printf("4. Tinh cong suat\n");
printf("5. Hien thi ma tran\n");
printf("0. Thoat\n");
printf("Chon: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Nhap hang (0-4): ");
scanf("%d", &r);
if (r >= 0 && r < ROW)
toggleRow(r);
break;
case 2:
printf("Nhap cot (0-2): ");
scanf("%d", &c);
if (c >= 0 && c < COL)
toggleCol(c);
break;
case 3:
printf("Nhap hang va cot: ");
scanf("%d %d", &r, &c);
if (r >= 0 && r < ROW && c >= 0 && c < COL)
togglePos(r, c);
break;
case 4:
printf("Tong cong suat: %d W\n", power());
break;
case 5:
printMatrix();
break;
}
} while (choice != 0);
return 0;
}
Hãy giúp mọi người biết câu trả lời này thế nào?
Bảng tin