Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <iomanip>
- using namespace std;
- int linearSearch(int A[], int N, int X)
- {
- for (int i = 0; i < N; i++)
- {
- if (A[i] == X) {
- return i;
- }
- }
- return -1;
- }
- int main()
- {
- const int N = 20; // حجم المصفوفة
- int A[N] = { 17, 3, 45, 8, 29, 12, 56, 4, 23, 9, 31, 5, 48, 27, 1, 15, 36, 19, 7, 50 };
- int x = 31;
- // طباعة محتوي المصفوفة
- for (int i = 0; i < N; i++)
- cout << A[i] << " ";
- cout << "\n\n";
- // متغير لحفض نتيحة تنفيذ الدالة
- int result = linearSearch(A, N, x);
- // التأكد من قيمة المتغير هل تساوي سالب واحد
- // في حال نجاح العملية فالمتغير يحمل قيمة الفهرس
- if (result != -1)
- cout << "the value " << x << " is found in " << result << endl;
- else
- cout << "the value " << x << " not found in " << result << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment