38
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
Console.WriteLine("Chương trình tìm kiếm theo thuật toán tìm kiếm tuyến tính");
8
int n, tim;
9
int [] a;
10
11
Console.WriteLine("Nhập vào số phần tử mảng:");
12
n = int.Parse(Console.ReadLine());
13
//Khởi tạo mảng
14
a = new int[n];
15
//Nhập dữ liệu cho từng phần tử mảng, chỉ số mảng bắt đầu từ 0
16
for(int i=0; i<n; i++)
17
{
18
Console.Write("a[{0}]:",i);
19
a[i] = int.Parse(Console.ReadLine());
20
}
21
Console.WriteLine("Nhập vào giá trị tìm kiếm:");
22
tim = int.Parse(Console.ReadLine());
23
//Dùng vòng lặp duyệt qua các phần tử
24
bool found = false;
25
for(int i=0; i<n; i++)
26
{
27
if(tim == a[i])
28
{
29
found = true;
30
Console.WriteLine("Tìm giá trị {0} ở vị trí {1} trong mảng",tim,(i+1));
31
break;
32
}
33
}
34
35
if(found == false)
36
Console.WriteLine("Không tìm thấy giá trị tìm kiếm trong mảng");
37
}
38
}
Cached Result