⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aub1.cpp

📁 This program is used to find the union, intersection and exclusive OR for given two sorted arrays.
💻 CPP
字号:
#include <iostream.h>
#include <stdlib.h>


void main()
{
     int nums1[] = { 1,2,3,4,6,8,9 };
     int nums2[] = { 1,2,4,8,10 };
     int nums3[15];
     int nums4[15];
     int nums5[15];
     int size1 = 7;
     int size2 = 5;
     int k,t=0;
	 int pos1 = 0, pos2 = 0, pos3 = 0, pos4=0, pos5=0;
	 

	 t=t+5;
	 //inserting the elements of array 1
	 cout << "Number of Elements of Array 1= ";
	 cin >> size1;
	 cout << "Elements:" <<endl;
     for (k = 0; k < size1; k++)
     {
		 cout << "A[" << k << "] = ";
		 cin >> nums1[k];
     }
	 
	 //inserting the elements of array 2
	 cout << "Number of Elements of Array 2= ";
	 cin >> size2;
	 cout << "Elements:" <<endl;
     for (k = 0; k < size2; k++)
     {
		 cout << "A[" << k << "] = ";
		 cin >> nums2[k];
     }
     
	 //printing elements of array 1
	 cout << endl << "ARRAY 1: ";
     for (k = 0; k < size1; k++)
           cout << nums1[k] << " ";

	 //printing elements of array 2
     cout << endl << endl;
     cout << "ARRAY 2: ";
     for (k = 0; k < size2; k++)
           cout << nums2[k] << " ";

	 //merge sort
	 while (pos1 < size1 && pos2 < size2)
	 {
		 //checking the union n intersection
		 t=t+3;
		 if(nums1[pos1]==nums2[pos2])
		 {
			t=t+3;
			nums3[pos3++]=nums1[pos1]; 
			nums4[pos4++]=nums1[pos1];
			pos1++;
			pos2++;
			t=t+12;
		 }
		 else
		 {
			t=t+3;
			//checking the union n XOR
			if (nums1[pos1] < nums2[pos2])
			{
				t=t+3;
				nums3[pos3++] = nums1[pos1];
				nums5[pos5++] = nums1[pos1];	
				pos1++;
				t=t+10;

			}
			else
			{
				t=t+3;
				nums3[pos3++] = nums2[pos2];
				nums5[pos5++] = nums1[pos2];
				pos2++;
				t=t+10;
			}
		 }

	}
    t=t+3;
	//checking if there is the rest of elements to be inserted in union n XOR array
	if (pos1 < size1)
	{
		t=t+1;
		while (pos1 < size1)
		{
			t=t+1;
			nums3[pos3++] = nums1[pos1];
			nums5[pos5++] = nums1[pos1];
			pos1++;
			t=t+10;
		}
		t=t+1;
	}
    else
	{
        t=t+1;
		while (pos2 < size2)
		{
			t=t+1;
			nums3[pos3++] = nums2[pos2];
			nums5[pos5++] = nums2[pos2];
			pos2++;
			t=t+10;
		}
		t=t+1;
	}

	//printing the result
     cout << endl << endl;
     cout << "UNION ARRAY: ";
     for (k = 0; k < pos3; k++)
           cout << nums3[k] << " ";

     cout << endl << endl;


     cout << "INTERSECTION ARRAY 3: ";
     for (k = 0; k < pos4; k++)
           cout << nums4[k] << " ";

     cout << endl << endl;


     cout << "XOR ARRAY 3: ";
     for (k = 0; k < pos5; k++)
           cout << nums5[k] << " ";

     cout << endl << endl;
	 cout << "Time: " << t <<endl;

     system("PAUSE");

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -