কম্পিউটার টিউটোরিয়াল

C++ প্রোগ্রাম: দেওয়া সংখ্যা বা ক্যারেক্টার সেট থেকে র‍্যান্ডম পার্টিশন জেনারেট করা


এটি একটি C++ প্রোগ্রাম যা দেওয়া সংখ্যা বা ক্যারেক্টার সেট থেকে র‍্যান্ডম পার্টিশন জেনারেট করে।

অ্যালগরিদম

শুরু
  পূর্ণসংখ্যা বা ক্যারেক্টার ইনপুট নিন।
  চয়েস 1-এর জন্য:
    n পূর্ণসংখ্যা অ্যারে ইনপুট নিন।
    l = 0 অ্যাসাইন করুন অ্যারে ট্রাভার্স করার জন্য।
    rand() ব্যবহার করে n-এর র‍্যান্ডম ইন্টিজার পার্টিশন জেনারেট করুন।
    প্রতিটি পার্টিশন i-এর জন্য, ইনডেক্স ভ্যালু l থেকে পরবর্তী i পূর্ণসংখ্যা প্রিন্ট করুন।
  চয়েস 2-এর জন্য:
    ch[]-এ একটি স্ট্রিং ইনপুট নিন।
    স্ট্রিংয়ের দৈর্ঘ্য ক্যালকুলেট করুন এবং l = 0 অ্যাসাইন করুন, এটি স্ট্রিং ট্রাভার্স করবে।
    স্ট্রিং দৈর্ঘ্যের র‍্যান্ডম ইন্টিজার পার্টিশন জেনারেট করুন।
    প্রতিটি পার্টিশন i-এর জন্য, ইনডেক্স ভ্যালু l থেকে পরবর্তী i ক্যারেক্টার প্রিন্ট করুন।
শেষ

উদাহরণ

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

int main() {
    int n, i, j, l, c;
    cout << "Enter 1 for integer and 2 for character array to generate array: ";
    cin >> c;
    // চয়েস 1-এর জন্য:
    if (c == 1) {
        cout << "\nEnter the number of element in the integer array: ";
        cin >> n;
        int a[n];
        cout << "\nEnter the elements of the array: \n";
        for (i = 0; i < n; i++) {
            cout << "Enter " << i+1 << " element: ";
            cin >> a[i];
        }
        cout << "\nThe random partition of the given array is: \n";
        l = 0;
        while (n > 0) {
            cout << "\t[";
            i = rand() % n + 1;
            n = n - i;
            for (j = 0; j < i; j++) {
                cout << a[l] << " ";
                l++;
            }
            cout << "]";
        }
    }
    // চয়েস 2-এর জন্য:
    else {
        char ch[100];
        cout << "Enter the characters: ";
        cin >> ch;
        n = strlen(ch);
        cout << "\nThe random partition of the given string is: \n";
        l = 0; // l= 0 অ্যাসাইন করুন অ্যারে ট্রাভার্স করার জন্য।
        while (n > 0) {
            cout << "\t[ ";
            i = rand() % n + 1;
            n = n - i;
            for (j = 0; j < i; j++) {
                cout << ch[l] << " ";
                l++;
            }
            cout << "]";
        }
    }
    return 0;
}

আউটপুট

Enter 1 for integer and 2 for character array to generate array: 1 Enter the number of element in the integer array: 10 Enter the elements of the array: Enter 1 element: 10 Enter 2 element: 9 Enter 3 element: 8 Enter 4 element: 7 Enter 5 element: 6 Enter 6 element: 5 Enter 7 element: 4 Enter 8 element: 3 Enter 9 element: 2 Enter 10 element: 1 The random partition of the given array is: [10 9 ] [8 7 6 5 ] [4 3 2 ] [1 ]

আউটপুট

Enter 1 for integer and 2 for character array to generate array: 2 Enter the characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ The random partition of the given string is: [ A B C D E F G H I J K L M N O P ] [ Q R S T U V W X ] [ Y ] [ Z ]

  1. C++ প্রোগ্রাম: মাল্টিপ্লাই-উইথ-ক্যারি পদ্ধতিতে র‍্যান্ডম সংখ্যা জেনারেট

  2. C++ প্রোগ্রাম: প্রোবাবিলিটি ডেনসিটি ফাংশন ব্যবহার করে র‍্যান্ডম সংখ্যা জেনারেট

  3. C++ প্রোগ্রাম: মিডল স্কোয়ার পদ্ধতিতে র‍্যান্ডম সংখ্যা জেনারেট

  4. C++ প্রোগ্রাম: র‍্যান্ডম নম্বর জেনারেট