Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

BÀI TẬP BUỔI 6

Câu 1: Đọc và in các phần tử mảng trong C#


int[] mang = new int[5];
Console.WriteLine("Nhap 5 phan tu");
for (int i = 0; i < mang.Length; i++)
{
Console.Write("Nhap phan tu {0}: " , i);
mang[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Cac phan tu vua nhap la:");
for (int i = 0; i < mang.Length; i++)
Console.Write("{0} ", i);
}
}
}

Câu 2: Cách in mảng theo chiều đảo ngược trong C#

int n;
int[] mang= new int[100];
Console.Write("Nhap so phan tu trong mang: ");
n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Console.Write("Phan tu {0}: ", i);
mang[i]= Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Day cac phan tu vua nhap la:");
for (int i = 0; i < n; i++)
{
Console.Write("{0} ", i);
}
Console.WriteLine();
Console.WriteLine("In ra chieu nguoc lai");
for (int i = n -1; i >= 0; i--)
{
Console.Write("{0} ", mang[i]);
}
}
}
}

Câu 6: In ra các phần tử duy nhất của mảng trong C#

int i, j, n, dem = 1;
int[] mang = new int[100];
Console.Write("Nhap so phan tu trong mang: ");
n = int.Parse(Console.ReadLine());
for (i = 0; i < n; i++)
{
Console.Write("Phan tu {0}: ", i);
mang[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nCac phan tu duy nhat duoc tim thay trong mang la: \n");
for (i = 0; i <=n; i++)
{

for (j = 0; j <i; j++)


{
if (mang[i] == mang[j])
dem++;
else break;
}
if (dem==1)
{
Console.Write("{0} ", mang[i]);
}
}
}
}

Câu 7: Chương trình C# để trộn(ghép) hai mảng

int i, j, n1, n2;


int[] m1 = new int[100];
int[] m2 = new int[100];
int[] m3 = new int[100];
Console.Write("Nhap so phan tu trong mang A: ");
n1 = int.Parse(Console.ReadLine());
for (i = 0; i < n1; i++)
{
Console.Write("Phan tu {0}: ", i);
m1[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Mang A moi nhap la: ");
for (i = 0; i < n1; i++)
{
Console.Write("{0} ", m1[i]);
}
Console.WriteLine();
Console.Write("Nhap so phan tu mang B: ");
n2 = int.Parse(Console.ReadLine());
for (j = 0; j < n2; j++)
{
Console.Write("Phan tu {0} :", j);
m2[j] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Mang B vua nhap la: ");
for (j = 0; j < n2; j++)
{
Console.Write("{0} ", m2[j]);
}
Console.WriteLine();
Console.WriteLine("Tron hai mang A va B thanh mang C");
for (i = 0; i < n1; i++)
{
m3[i] = m1[i];
}
for (j = 0; j < n2; j++)
{
m3[i] = m2[j];
i++;
}
int n3 = n1 + n2;
for (i = 0; i < n3; i++)
{
Console.Write("{0} ", m3[i]);
}

}
}

Câu 8:Chương trình C# để đếm số lần xuất hiện của từng phần tử trong mảng

Cách 1:

Console.Write("Nhap so phan tu can luu giu trong mang: ");


int n = int.Parse(Console.ReadLine());
int[] m = new int[n];
Random r = new Random();
int[] b = new int[100];

Console.Write("Nhap {0} phan tu vao trong mang:\n", n);


for (int i = 0; i < n; i++)
{
m[i] = r.Next(-9, 9);
Console.WriteLine("Phan tu {0}: {1} ",i, m[i]);
b[i] = -1;
}
for (int i = 0; i < n; i++)
{
int bien_dem = 1;
for (int j = i + 1; j < n; j++)
{
if (m[i] == m[j])
{
bien_dem++;
b[j] = 0;
}
}

if (b[i] != 0)
{
b[i] = bien_dem;
}
}
Console.Write("\nTan suat xuat hien cua tung phan tu trong mang la: \n");
for (int i = 0; i < n; i++)
{
if (b[i] != 0)
{
Console.Write("Phan tu {0} xuat hien {1} lan\n", m[i], b[i]);
}
}

}
}
}

Cách 2:

int N;
Console.Write("Nhap so phan tu cua mang N: ");
N = Int32.Parse(Console.ReadLine());

int[] t = new int[N];


Console.WriteLine("Nhap du lieu cho mang t[N]: ",N);
for (int i = 0; i < N; i++)
{
Console.Write("Nhap phan tu {0}: ", i);
t[i] = Int32.Parse(Console.ReadLine());
}
Console.WriteLine();

Console.WriteLine("Mang vua nhap la: ");


for (int i = 0; i < N; i++)
{
Console.Write("{0} ", t[i]);
}
Console.WriteLine();

int[] count = new int[N];


for (int i = 0; i < N; i++)
{
count[i] = 1;
}

for (int i = 0; i < N - 1; i++)


{
if (count[i] == 0) continue;
for (int j = i + 1; j < N; j++)
{
if (count[j] == 0) continue;
if (t[i] == t[j])
{
count[i]++;
count[j]--;
}
}

Console.WriteLine("Phan tu {0} xuat hien {1} lan", t[i], count[i]);


}
}
}
}
Câu 10: Chia mảng thành mảng chẵn, mảng lẻ trong C#

Console.Write("Nhap so phan trong mang: ");


int n = int.Parse(Console.ReadLine());
int[] m = new int[n];
int[] m1 = new int[100];
int[] m2 = new int[100];
Random r = new Random();
int i, j=0, k = 0;
Console.WriteLine("Nhap {0} phan tu vao trong mang: ", n);
for (i = 0; i < n; i++)
{
m[i] = r.Next(0,20);
Console.WriteLine("Phan tu {0}: {1}", i, m[i]);
}
for (i = 0; i < n; i++)
{
if (m[i] % 2 == 0)
{
m1[j] = m[i];
j++;
}
else
{
m2[k] = m[i];
k++;
}
}

Console.Write("\nCac phan tu chan la: \n");


for (i = 0; i < j; i++)
{
Console.Write("{0} ", m1[i]);
}

Console.Write("\nCac phan tu le la:\n");


for (i = 0; i < k; i++)
{
Console.Write("{0} ", m2[i]);
}
}
}
}

Câu 12: Sắp xếp mảng theo thứ tự giảm dần trong C#

int[] m = new int[100];


int i, j, bien;
Console.Write("Nhap so phan tu cua mang: ");
int n = int.Parse(Console.ReadLine());
for (i = 0; i < n; i++)
{
Console.Write("Phan tu {0}: ", i);
m[i] = Convert.ToInt32(Console.ReadLine());
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (m[i] < m[j])
{
bien = m[i];
m[i] = m[j];
m[j] = bien;
}
}
}

Console.WriteLine("In cac phan tu mang theo thu tu giam dan: ");


for (i = 0; i < n; i++)
{
Console.Write("{0} ", m[i]);
}

}
}
}
Câu 18: Đọc và in mảng hai chiều trong C#

int[,] mang = new int[2, 4];


for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write("mang[{0},{1}]: ", i, j);
mang[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine();
Console.WriteLine("Mang vua nhap la: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write("{0} ", mang[i, j]);
}
Console.WriteLine();
}
}
}
}
Câu 20: Trừ ma trận trong C#

int[,] A = new int[2, 4];


int[,] B = new int[2, 4];
int[,] C = new int[2, 4];
Console.WriteLine("Cac phan tu cua mang A: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write("Phan tu A[{0},{1}]: ", i, j);
A[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine();
Console.WriteLine("Cac phan tu cua mang B: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write("Phan tu B[{0},{1}]: ", i, j);
B[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine();
Console.WriteLine("Mang A: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write("{0} ", A[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Mang B: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write("{0} ", B[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
C[i, j] = A[i, j] - B[i, j];
}
}
Console.WriteLine("Ma tran hieu cua hai ma tran:");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write("{0} ", C[i, j]);
}
Console.WriteLine();
}
}
}
}
Câu 22: Tìm ma trận chuyển vị trong C#

Console.Write("Nhap so hang: ");


int r = int.Parse(Console.ReadLine());
Console.Write("Nhap so cot: ");
int c =int.Parse(Console.ReadLine());
int[,] A = new int[r, c];
int[,] B = new int[r, c];
Console.WriteLine("Cac phan tu cua mang A: ");
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
Console.Write("Phan tu A[{0},{1}]: ", i, j);
A[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine();
Console.WriteLine("Mang A vua nhap : ");
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
Console.Write("{0} ", A[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
B[j, i] = A[i, j];
}
}
Console.WriteLine();
Console.WriteLine("Ma tran chuyen vi cua A: ");
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
Console.Write("{0} ", B[i, j]);
}
Console.WriteLine();
}
}
}
}

Câu 23: Chương trình để tính tổng các phần tử trên đường chéo chính của ma trận

Console.Write("Nhap so hang: ");


int r = int.Parse(Console.ReadLine());
Console.Write("Nhap so cot: ");
int c =int.Parse(Console.ReadLine());
int[,] A = new int[r, c];
int sum = 0;
Console.WriteLine("Cac phan tu cua mang A: ");
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
Console.Write("Phan tu A[{0},{1}]: ", i, j);
A[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine();
Console.WriteLine("Mang A vua nhap : ");
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
Console.Write("{0} ", A[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
if (i == j)
sum += A[i, j];
}
}
Console.WriteLine();
Console.WriteLine("Tong cac phan tu tren duong cheo chinh: {0}", sum);
}
}
}

Câu 26: In ma trận tam giác trên trong C#

Console.Write("Nhap so hang: ");


int r = int.Parse(Console.ReadLine());
Console.Write("Nhap so cot: ");
int c =int.Parse(Console.ReadLine());
int[,] A = new int[r, c];
int sum = 0;
Console.WriteLine("Cac phan tu cua mang A: ");
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
Console.Write("Phan tu A[{0},{1}]: ", i, j);
A[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine();
Console.WriteLine("Mang A vua nhap : ");
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
Console.Write("{0} ", A[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("In ra tam giac ma tran tren: ");
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
if (i <= j)
Console.Write("{0} ", A[i, j]);
else Console.Write("0 ");
}
Console.WriteLine();
}
}
}
}

You might also like