Problem 5: Storing the digits of a number in an array

সমস্যা 5: এটা সংখ্যাৰ অংকবোৰ এটা এৰেত সংৰক্ষণ কৰা

In the previous problem, we have extracted the digits of a number and displayed them. Now, we need to store them in an array.

আগৰ সমস্যাটোত, আমি এটা সংখ্যাৰ অংকবোৰ উলিয়াইছো আৰু সেইবোৰ প্ৰদৰ্শন কৰিছো। এতিয়া, আমি সেইবোৰ এটা এৰেত সংৰক্ষণ কৰিব লাগিব।

If we observe the above program, we see that in each iteration of the while loop, we extracted one digit. Now, this digit is to be stored as an array element. Following code segment can do this job.

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

Here, digit is the name of the array where we want to store the extrcted digits of the number.

ইয়াত, ডিজিট হৈছে এৰেৰ নাম য’ত আমি সংখ্যাটোৰ বাহ্যিক অংকবোৰ সংৰক্ষণ কৰিব বিচাৰো।

In the next iteration, the digit we extract has to be stored the next array element. This mans, the array index is to incremented in each iteration of the loop. Following is the complete code segment.

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

The complete C program is shown using Example 7.6.The extracted digits of a number is stored in a global array digits [10]. The function find – digit() returns the last index of the array elements. This tells the number of elements stored in the array. We used this information to display the array elements when we came back from the duction call.

সম্পূৰ্ণ চি প্ৰ’গ্ৰামটো উদাহৰণ 7.6 ব্যৱহাৰ কৰি দেখুওৱা হৈছে। এটা সংখ্যাৰ আহৰণ কৰা অংকবোৰ গোলকীয় এৰে অংকত সঞ্চিত কৰা হয় [10]। ফাংচন ফাইণ্ড – ডিজিট() এৰে উপাদানবোৰৰ অন্তিম সূচী টো ঘূৰাই দিয়ে। ই এৰেত সঞ্চিত উপাদানৰ সংখ্যা কয়। যেতিয়া আমি ডাক্টিয়ান কলৰ পৰা উভতি আহিছিলো তেতিয়া আমি এৰে উপাদানবোৰ প্ৰদৰ্শন কৰিবলৈ এই তথ্যব্যৱহাৰ কৰিছিলো।

Example 7.6: A C program for storing the digits in an array using function.

We see that the elements are display in reversed order. That is, if the input number is 47837, it displays 7 3 8 7 4. To get it displayed in correct in correct order, we can apply the logic for displaying array elements in reverse order. This was already discussed in Section 6.5.6 of the previous chapter.

আমি দেখিছোঁ যে উপাদানবোৰ বিপৰীত ক্ৰমত প্ৰদৰ্শিত হয়। অৰ্থাৎ, যদি ইনপুট নম্বৰ 47837 হয়, ই 7 3 8 7 4 প্ৰদৰ্শন কৰে। ইয়াক সঠিক ক্ৰমত সঠিকভাৱে প্ৰদৰ্শন কৰিবলৈ, আমি বিপৰীত ক্ৰমত এৰে উপাদানবোৰ প্ৰদৰ্শন কৰাৰ যুক্তি প্ৰয়োগ কৰিব পাৰোঁ। এইটো আগৰ অধ্যায়ৰ ধাৰা ৬.৫.৬ ত ইতিমধ্যে আলোচনা কৰা হৈছিল।

Let us write another function display () to display the digits of the numbers that got stored in the array. This function starts displaying the elements from the lest Index to 0 with a decrement in the index value at each iteration.

এৰেত সংৰক্ষণ কৰা সংখ্যাবোৰৰ অংকবোৰ প্ৰদৰ্শন কৰিবলৈ আমি আন এটা ফাংচন ডিচপ্লে () লিখোঁ আহক। এই ফাংচনটোৱে প্ৰতিটো পুনৰাবৃত্তিত সূচী মূল্যত হ্ৰাসৰ সৈতে পাছে ইণ্ডেক্সৰ পৰা 0 লৈ উপাদানবোৰ প্ৰদৰ্শন কৰা আৰম্ভ কৰে।

We present the complete program in Example 7.7. you can see that main () function of the program looks simple and clear. it performs three tasks- (i) reads a number from the keyboard (ii) calls a function to get the digits, and (iii) calls another function to display the digits.

আমি সম্পূৰ্ণ কাৰ্যসূচীটো উদাহৰণ 7.7-ত উপস্থাপন কৰোঁ। আপুনি দেখিব পাৰে যে প্ৰ’গ্ৰামটোৰ মুখ্য () ফাংচনটো সৰল আৰু স্পষ্ট দেখাযায়। ই তিনিটা কাৰ্য্য সম্পাদন কৰে- (আই) অংকবোৰ প্ৰাপ্ত কৰিবলৈ কীবোৰ্ডৰ (আইআই) পৰা এটা নম্বৰ পঢ়ে, আৰু (তৃতীয়) অংকবোৰ প্ৰদৰ্শন কৰিবলৈ আন এটা ফাংচন কল কৰে।