In: Computer Science
Given two unsorted arrays of integers.
a) Write a pseudocode algorithm which will output only the integers
not common to both arrays. When writing pseudocode, consider that
no implementation for data structures or algorithms exist.
b) Implement your algorithm in Modern C++
please upvote ,comment for any query . Thanks.
Note : pseudocode written fro C++ and also program compiled and tested for C++ in CodeBlock IDE. check attached image for output .
program just wrote for your reference please consider only pseudocode.
Pseudocode :
function main(){
set i,j,matchFlag to zero
while i less than length of array1
for from m=0 to length of
array1
if element of
array1 index i equals to element of array2 index m
set matchFlag zero
break
else
matchFlag to one
endif
endfor
if matchFlag equals to one
print "uncommon
elements is " i index element of array1
set matchFlag to
zero
endif
for from x=0 to length of
array2
if element of
array2 index j equals to element of array1 index x
set matchFlag zero
break
else
matchFlag to one
endif
endfor
if matchFlag equals to one
print "uncommon
elements is " j index element of array2
set matchFlag to
zero
endWhile
}end
Program in C++ :
#include<iostream>
using namespace std;
int main()
{
int array1[10]={2,49,7,39,304,403,10,9,5,9}; //unsorted array
1
int array2[10]={2,49,12,39,304,403,10,9,5,11}; //unsorted array
2
int matchFlag=0;
int matchFlag2=0;
int i=0,j=0;
while(i<10) //while loop from 0 to length of array
{
for(int x=0;x<10;x++) //for array1 match to array 2
{
if(array1[i]==array2[x])
{
matchFlag=0;
break;
}
else
{
matchFlag=1;
}
}
if(matchFlag==1)
{
cout<<"Number is "<<array1[i]<<endl; //print
uncommon from array1
matchFlag=0;
}
for(int m=0;m<10;m++) //for array2 match with array 1
{
if(array2[j]==array1[m])
{
matchFlag=0;
break;
}
else
{
matchFlag=1;
}
}
if(matchFlag==1)
{
cout<<"Number is "<<array2[j]<<endl; //print
uncommon from array2
matchFlag=0;
}
i++;
j++;
}
}
Output :