5.2 PROBLEM SOLVING USING NESTED LOOP

5.2 নেষ্টেড লুপ ব্যৱহাৰ কৰি সমাধান কৰাত সমস্যা

Recall the programs of Example 4.12 and Example 4.13 of the previous chapter. We used one loop for displaying one line. As we were to display 5 and 9 lines, we used loop 5 and 9 number of times in the programs. But the same task can be done with a lesser number of loops.

পূৰ্বৱৰ্তী অধ্যায়ৰ উদাহৰণ 4.12 আৰু উদাহৰণ 4.13-ৰ কাৰ্যসূচীবোৰ মনত পেলাওক। আমি এটা শাৰী প্ৰদৰ্শন কৰিবলৈ এটা লুপ ব্যৱহাৰ কৰিছিলো। যিহেতু আমি 5 আৰু 9 শাৰী প্ৰদৰ্শন কৰিব লাগিছিল, আমি প্ৰ’গ্ৰামবোৰত লুপ 5 আৰু 9 বাৰ ব্যৱহাৰ কৰিছিলো। কিন্তু একেটা কাম কম সংখ্যক লুপৰ সৈতে কৰিব পাৰি।

Let us take Example 4.12. We need to display the following pattern.

আহক আমি উদাহৰণ 4.12 লওঁ। আমি নিম্নলিখিত আৰ্হিটো প্ৰদৰ্শন কৰিব লাগিব।

XXXXX

XXXX

XXX

XX

X

Let us have a close look at the pattern and write the tasks to be done in each line as follows.

We have seen that in every line, the task is the same – to display X. The difference is in the number of times X is to be displayed. If we want to write a general formula, we may say that in the kth line, we need to display X (6-k) times.

আমি দেখিছোঁ যে প্ৰতিটো শাৰীতে, কামটো একে – এক্স প্ৰদৰ্শন কৰিবলৈ। পাৰ্থক্যটো এক্স প্ৰদৰ্শন কৰিব লগা সময়ৰ সংখ্যাত আছে। যদি আমি এটা সাধাৰণ সূত্ৰ লিখিব বিচাৰো, আমি ক’ব পাৰোঁ যে কেথ শাৰীত, আমি এক্স (6-কে) বাৰ প্ৰদৰ্শন কৰিব লাগিব।

In line number 1, k = 1, thus, 6-k is 6 – 1 = 5.

In line number 2, k = 2, thus, 6-k is 6-2 = 4.

In line number 3, k = 3, thus, 6-k is 6 – 3 = 3.

In line number 4, k = 4, thus, 6-k is 6 – 4 = 2.

In line number 5, k = 5, thus, 6-k is 6 – 5 = 1.

So, we are to write a loop in the following way.

গতিকে, আমি নিম্নলিখিত ধৰণে এটা লুপ লিখিব লাগিব।

Now, if we use for loop, the above segment expands to the following
এতিয়া, যদি আমি লুপৰ বাবে ব্যৱহাৰ কৰোঁ, ওপৰোক্ত খণ্ডটো নিম্নলিখিতবোৰলৈ সম্প্ৰসাৰিত হয়।

Next, we need to write the code for the display part. We can again use another for loop there. We need to use a loop so that it runs (6-k) times and in each iteration, it needs to display the character X. Thus get the following code segment.

ইয়াৰ পিছত, আমি প্ৰদৰ্শন অংশৰ বাবে কোডটো লিখিব লাগিব। আমি তাত লুপৰ বাবে পুনৰ আন এটা ব্যৱহাৰ কৰিব পাৰোঁ। আমি এটা লুপ ব্যৱহাৰ কৰিব লাগিব যাতে ই (6-কে) বাৰ চলে আৰু প্ৰতিটো পুনৰাবৃত্তিত, ই এক্স বৰ্ণটো প্ৰদৰ্শন কৰিব লাগিব। এনেদৰে নিম্নলিখিত কোড খণ্ডটো প্ৰাপ্ত কৰক।

The complete program is shown using Example 5.1 The for loop in line number 5 is the outer loop and the loop for in line number 7 is the inner loop. We see a drastic reduction in the program length as compared to Example 4.12.

সম্পূৰ্ণ প্ৰ’গ্ৰামটো উদাহৰণ 5.1 ব্যৱহাৰ কৰি দেখুওৱা হৈছে শাৰী নম্বৰ 5-ত লুপৰ বাবে বাহিৰৰ লুপ আৰু শাৰী নম্বৰ 7-ৰ বাবে লুপটো হৈছে ভিতৰৰ লুপ। উদাহৰণ 4.12-ৰ তুলনাত আমি কাৰ্যসূচীৰ দৈৰ্ঘ্য যথেষ্ট হ্ৰাস হোৱা দেখিছোঁ।

Example 5.1: AC program to display a pattern.

Now, let us make the program flexible. The last program displayed 5 lines and the number of characters in each line decreased by one – 5 charactering line 1, 4 in line 2 and so on. If we want the number of lines to be dynamic, We may give an option to enter from the keyboard, Then the program should display the patterns accordingly.

এতিয়া, আমি প্ৰ’গ্ৰামটো নমনীয় কৰোঁ আহক। অন্তিম প্ৰ’গ্ৰামটোৱে 5 টা শাৰী প্ৰদৰ্শন কৰিছিল আৰু প্ৰতিটো শাৰীত বৰ্ণৰ সংখ্যা এটা – 5 বৰ্ণৰেখা 1, 4 শাৰী 2 আদিত হ্ৰাস পাইছিল। যদি আমি লাইনৰ সংখ্যা গতিশীল হোৱাটো বিচাৰো, আমি কীবোৰ্ডৰ পৰা প্ৰবিষ্ট কৰাৰ বিকল্প এটা দিব পাৰোঁ, তেনেহ’লে প্ৰ’গ্ৰামটোৱে সেই অনুসৰি আৰ্হিবোৰ প্ৰদৰ্শন কৰিব লাগে।

If the user enters9, the program should display 9 characters (X) in the first line. Then 8 characters inline 2, 7 in line 3 and so on. The pattern is shown below.

যদি ব্যৱহাৰকাৰীয়ে 9 ত প্ৰৱেশ কৰে, প্ৰ’গ্ৰামটোৱে প্ৰথম শাৰীত 9 টা বৰ্ণ (এক্স) প্ৰদৰ্শন কৰিব লাগে। তাৰ পিছত 8 টা বৰ্ণ ইনলাইন 2, 7 শাৰী 3 ইত্যাদি। আৰ্হিটো তলত দেখুওৱা হৈছে।

X X X X X X X X X

X X X X X X X X

X X X X X X X

X X X X X X

X X X X X

X X X X

XXX

XX

X

We observe that if we want N lines to be displayed, the number of characters in the first line is N. In the next line, it is N-1 then N-2 and so on.

আমি লক্ষ্য কৰোঁ যে যদি আমি এন শাৰীপ্ৰদৰ্শন কৰাটো বিচাৰো, প্ৰথম শাৰীত বৰ্ণৰ সংখ্যা হৈছে এন। পৰৱৰ্তী শাৰীত, ই হৈছে এন-1 তাৰ পিছত এন-2 ইত্যাদি।

In the last program, We started with 5 characters. In this case, we need to start with N chararcters. The remining logic is the same in both cases. When we needed to start with 5, we used (6-k) in the program. That means, in this case, we need to use (N+1-k).

অন্তিম প্ৰ’গ্ৰামত, আমি 5 টা বৰ্ণৰ সৈতে আৰম্ভ কৰিছিলো। এই ক্ষেত্ৰত, আমি এন চাৰাৰ্কটাৰৰ সৈতে আৰম্ভ কৰিব লাগিব। দুয়োটা ক্ষেত্ৰতে পুনৰ্খননৰ যুক্তি একে। যেতিয়া আমি 5 ৰে আৰম্ভ কৰাৰ প্ৰয়োজন হৈছিল, আমি প্ৰ’গ্ৰামটোত (6-কে) ব্যৱহাৰ কৰিছিলো। ইয়াৰ অৰ্থ হৈছে, এই ক্ষেত্ৰত, আমি (এন+1-কে) ব্যৱহাৰ কৰিব লাগিব।

The complete program is shown using Example 5.2. We initially give an option to the user for entering the number of lines to be displayed. Then we run the for loops (as in Example 5.1) using N as the boundary variable. Here, we write N in place of 5 in the outer loop and (N+1-k) in place of (6-k) in the inner loop.

সম্পূৰ্ণ প্ৰ’গ্ৰামটো উদাহৰণ 5.2 ব্যৱহাৰ কৰি দেখুওৱা হৈছে। আমি প্ৰথমতে প্ৰদৰ্শন কৰিব লগা শাৰীৰ সংখ্যা প্ৰবিষ্ট কৰাৰ বাবে ব্যৱহাৰকাৰীক এটা বিকল্প দিওঁ। তাৰ পিছত আমি এনক বাউণ্ডেৰী ভেৰিয়েবল হিচাপে ব্যৱহাৰ কৰি লুপৰ বাবে (উদাহৰণ 5.1-ৰ দৰে) চলাওঁ। ইয়াত, আমি বাহিৰৰ লুপত 5 ৰ ঠাইত এন আৰু ভিতৰৰ লুপত (6-কে)ৰ ঠাইত (এন+1-কে) লিখিম।