এই টিউটোরিয়ালটি c++ কোড ব্যবহার করে একটি তিন-তির্যক অ্যারের উপরের সারির নিচের সারিতে অদলবদল করার জন্য ডিজাইন করা হয়েছে। তদুপরি, যদি একটি তিন-তির্যক অ্যারে একটি ইনপুট হয়, তাহলে কাঙ্খিত ফলাফলগুলি অবশ্যই এরকম কিছু হতে হবে;

এই জন্য, অ্যালগরিদমে অ্যাকশনের কোর্সটি নিম্নরূপ সংক্ষিপ্ত করা হয়েছে;
অ্যালগরিদম
Step-1: Input a diagonal array Step-2: Pass it to Swap() method Step-3: Traverse the outer loop till 3 Step-4: increment j= i+ 1 in the inner loop till 3 Step-5: put the array value in a temp variable Step-6: interchange the value arr[i][j]= arr[j][i] Step-7: put the temp data to arr[j][i] Step-8: Print using for loop
সুতরাং, c++ কোডটি পূর্বোক্ত অ্যালগরিদমের ব্যঞ্জনা অনুসারে তৈরি করা হয়েছে;
উদাহরণ
#include <iostream>
#define n 3
using namespace std;
// Function to swap the diagonal
void swap(int arr[n][n]){
// Loop for swap the elements of matrix.
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
// Loop for print the matrix elements.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << arr[i][j] << " ";
cout << endl;
}
}
// Driver function to run the program
int main(){
int arr[n][n] = {
{ 1, 2, 3},
{ 4, 5, 6},
{ 7, 8, 9},
};
cout<<"Input::"<<endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << arr[i][j]<< " ";
cout << endl;
}
// Function call
cout<<"Output(Swaped)::"<<endl;
swap(arr);
return 0;
} আউটপুট
নিচের আউটপুটে যেমন দেখা যাচ্ছে, 3D অ্যারের নিচের অংশের সাথে অদলবদল করা উপরের সারি;
Input:: 1 2 3 4 5 6 7 8 9 Output(Swaped):: 1 4 7 2 5 8 3 6 9