6.5.5 Searching an element in an array

6.5.5 এটা এৰেত এটা উপাদান সন্ধান কৰা

Suppose, we are given a series of numbers and we store these numbers in an array. We are now given another number. We need to write a C program to check whether this number is present in the series of numbers or not. If the number is present, we need to report its index in the array.

ধৰি লওক, আমাক সংখ্যাৰ এটা শৃংখলা দিয়া হৈছে আৰু আমি এই সংখ্যাবোৰ এটা এৰেত সংৰক্ষণ কৰোঁ। আমাক এতিয়া আন এটা নম্বৰ দিয়া হৈছে। সংখ্যাৰ শৃংখলাত এই সংখ্যাটো আছে নে নাই পৰীক্ষা কৰিবলৈ আমি এটা চি প্ৰ’গ্ৰাম লিখিব লাগিব। যদি সংখ্যাটো থাকে, আমি ইয়াৰ সূচীটো এৰেত প্ৰতিবেদন কৰিব লাগিব।

We can apply the same strategy as before to store the numbers in the array. Now let us develope a logic for searching. The following flowchart shows the searching logic.

আমি এৰেত নম্বৰবোৰ সংৰক্ষণ কৰিবলৈ আগৰ দৰে একে ৰণনীতি প্ৰয়োগ কৰিব পাৰোঁ। এতিয়া আমি সন্ধানৰ বাবে এটা যুক্তি বিকশিত কৰোঁ আহক। নিম্নলিখিত ফ্লোচাৰ্টখনে সন্ধানযুক্তি প্ৰদৰ্শন কৰে।

The corresponding code segment is given below.

সংশ্লিষ্ট কোড খণ্ডটো তলত দিয়া হৈছে।

We have used a while loop in the code segment. You may use any other loop as well. The first two lines in the code segment are responsible for getting the number for searching from the keyboard. This number is stored in a variable “item”.

আমি কোড শাখাত অলপ সময়ৰ লুপ ব্যৱহাৰ কৰিছো। আপুনি আন যিকোনো লুপব্যৱহাৰ কৰিব পাৰে। কোড শাখাৰ প্ৰথম দুটা শাৰী কীবৰ্ডৰ পৰা সন্ধান কৰাৰ বাবে নম্বৰ প্ৰাপ্ত কৰাৰ বাবে দায়বদ্ধ। এই নম্বৰটো এটা চলক “আইটেম”ত সংৰক্ষণ কৰা হয়।

We initialize the index to zero and the loop runs as long as it is less than totalEle that indicates the total number of elements present in the array.

আমি সূচকটো শূন্যলৈ আৰম্ভ কৰোঁ আৰু লুপটো তেতিয়ালৈকে চলে যেতিয়ালৈকে ই টোটেলইলতকৈ কম হয় যি এৰেত থকা উপাদানৰ মুঠ সংখ্যা সূচায়।

As soon as the item and the array element matches (line number 6 in the code segment), we announce that the item is present and we terminate the program by returning an integer (return 0).

আইটেম আৰু এৰে উপাদান মিলাৰ লগে লগে (কোড শাখাত লাইন নম্বৰ 6), আমি ঘোষণা কৰোঁ যে আইটেমটো আছে আৰু আমি এটা ইণ্টেগাৰ ঘূৰাই প্ৰ’গ্ৰামটো সমাপ্ত কৰোঁ (ৰিটাৰ্ণ 0)।

Otherwise, we go to the next element in the array by incrementing the index. When all the elements in the array are visited, we declare that the item is not present. The complete program is shown in Example 6.7.

অন্যথা, আমি সূচীবৃদ্ধি কৰি এৰেৰ পৰৱৰ্তী উপাদানলৈ যাওঁ। যেতিয়া এৰেৰ সকলো উপাদান দৰ্শন কৰা হয়, আমি ঘোষণা কৰোঁ যে আইটেমটো উপস্থিত নাই। সম্পূৰ্ণ প্ৰ’গ্ৰামটো উদাহৰণ 6.7-ত দেখুওৱা হৈছে।

Have you observed anything important in the above activity?

আপুনি ওপৰোক্ত কাৰ্যকলাপত গুৰুত্বপূৰ্ণ কিবা লক্ষ্য কৰিছে নেকি?

When we search for the number 5 in the series of numbers, we see that the number 5 is present at indices 1 and 7. But the program terminates displaying that it is present at index 1.

যেতিয়া আমি সংখ্যাৰ শৃংখলাত 5 নম্বৰটো বিচাৰোঁ, আমি দেখিছোঁ যে 5 নম্বৰটো সূচক 1 আৰু 7-ত আছে। কিন্তু প্ৰ’গ্ৰামটোৱে সূচী 1-ত থকাটো প্ৰদৰ্শন কৰাটো সমাপ্ত কৰে।

How can we handle this situation? Whenever a number is present more than once, we want to display all the indices.

আমি এই পৰিস্থিতিটো কেনেকৈ মোকাবিলা কৰিব পাৰোঁ? যেতিয়াই এটা সংখ্যা একাধিকবাৰ থাকে, আমি সকলো সূচক প্ৰদৰ্শন কৰিব বিচাৰো।

If we observe the above program carefully, we see that this was happening because we terminated the program by returning 0 whenever we got a match (line number 9 in the code segment).

যদি আমি ওপৰোক্ত প্ৰ’গ্ৰামটো সাৱধানে পৰ্যবেক্ষণ কৰোঁ, আমি দেখিছোঁ যে এইটো ঘটি আছে কিয়নো আমি যেতিয়াই এটা মেচ পাওঁ (কোড শাখাত শাৰী নম্বৰ 9) 0 ঘূৰাই দি প্ৰ’গ্ৰামটো সমাপ্ত কৰিছো।

Naturally, we can not end the program here, even if we get a match! We need to it is a match and we need to go ahead to look for the next match if there is any. report that.

স্বাভাৱিকতে, আমি ইয়াত প্ৰ’গ্ৰামটো সমাপ্ত কৰিব নোৱাৰো, আনকি আমি এখন মেচ পালেও! আমি এইখন এখন খেল হ’ব লাগিব আৰু যদি আছে তেন্তে আমি পৰৱৰ্তী খেলখন বিচাৰিবলৈ আগবাঢ়ি যাব লাগিব। সেইটো প্ৰতিবেদন কৰক।

Thus the code segment becomes as follows.

এনেদৰে কোড খণ্ডটো নিম্নলিখিত ধৰণৰ হৈ পৰে।

What have you observed now?

আপুনি এতিয়া কি লক্ষ্য কৰিছে?

The program rightly displays all the appearances of a given number. So we solved the above issue.

প্ৰ’গ্ৰামটোৱে এটা প্ৰদত্ত নম্বৰৰ সকলো বোৰ প্ৰদৰ্শন সঠিকভাৱে প্ৰদৰ্শন কৰে। গতিকে আমি ওপৰোক্ত সমস্যাটো সমাধান কৰিলোঁ।

But the program also displays the line “Given number is NOT present”. This is wrong – we do not want this line to be displayed.

কিন্তু প্ৰ’গ্ৰামটোৱে “প্ৰদত্ত নম্বৰ টো উপস্থিত নহয়” শাৰীটোও প্ৰদৰ্শন কৰে। এইটো ভুল – আমি এই শাৰীটো প্ৰদৰ্শিত হোৱাটো নিবিচাৰো।

We can solve this with the help of another variable – flag.

যুক্তিটো সৰল! এৰে উপাদানবোৰ দৰ্শন কৰোঁতে, যদি আমি এটা মিল পাওঁ আমি ফ্লেগ ভেৰিয়েবল ছেট কৰি ইয়াক মন কৰিম। আমি এৰেৰ সকলো উপাদান দৰ্শন কৰা সমাপ্ত কৰাৰ পিছত, যদি পতাকাখন এতিয়াও ৰিছেট অৱস্থাত আছে, আমি সুৰক্ষিতভাৱে ক’ব পাৰোঁ যে আমি কোনো মিল পোৱা নাই আৰু আমি ঘোষণা কৰিব পাৰোঁ যে আইটেমটো উপস্থিত নাই।আমি ইয়াক আন এটা চলক – পতাকাৰ সহায়ত সমাধান কৰিব পাৰোঁ।

The logic is simple! While visiting the array elements, if we find a match we take a note of it by setting a flag variable. After we finish visiting all the elements in the array, if the flag is still in reset condition, we can safely say that we have not found any match and we can declare that the item is not present.

যুক্তিটো সৰল! এৰে উপাদানবোৰ দৰ্শন কৰোঁতে, যদি আমি এটা মিল পাওঁ আমি ফ্লেগ ভেৰিয়েবল ছেট কৰি ইয়াক মন কৰিম। আমি এৰেৰ সকলো উপাদান দৰ্শন কৰা সমাপ্ত কৰাৰ পিছত, যদি পতাকাখন এতিয়াও ৰিছেট অৱস্থাত আছে, আমি সুৰক্ষিতভাৱে ক’ব পাৰোঁ যে আমি কোনো মিল পোৱা নাই আৰু আমি ঘোষণা কৰিব পাৰোঁ যে আইটেমটো উপস্থিত নাই।

The code segment is presented below.

কোড খণ্ডটো তলত উপস্থাপন কৰা হৈছে।

The complete C program is presented using Example 6.8. This program reports all the appearances of a given number in the series of numbers.

সম্পূৰ্ণ চি প্ৰ’গ্ৰামটো উদাহৰণ 6.8 ব্যৱহাৰ কৰি উপস্থাপন কৰা হৈছে। এই প্ৰ’গ্ৰামটোৱে সংখ্যাৰ শৃংখলাত এটা প্ৰদত্ত সংখ্যাৰ সকলো উপস্থিতি প্ৰতিবেদন কৰে।