Write a program that accepts two integer arrays as input. Array 1 of size 10 and
ID: 3698605 • Letter: W
Question
Write a program that accepts two integer arrays as input. Array 1 of size 10 and Array 2 of size 9. Find an integer in Array 1 that does not have its pair in Array 2 (if there are more than one such integers you only need to print out the first one you find). You should have a separate function for reading in the array elements from the user, and function for finding the lonely element. In the main() function should handle printing the result. Enter Array 1 Elements: 1 2 3 4 5 6 7 8 9 10 Enter Array 2 Elements: 10 2 4 1 3 5 7 6 9 Lonely Integer is: 8
Explanation / Answer
since no particular language is mentioned, I have written the program in C++
#include<iostream>
using namespace std;
void readArray(int array1[],int array2[])
{
cout<<"Enter elements in array1 :";
for(int i=0;i<10;i++){
cin>>array1[i];
}
cout<<"Enter elements in array2 :";
for(int i=0;i<9;i++){
cin>>array2[i];
}
}
int calcLonely(int array1[],int array2[]){
int flag=0;
for(int i=0;i<10;i++){
for(int j=0;j<9;j++){
if(array1[i]==array2[j]){
flag=1;
break;
}
}
if(flag==1)
flag=0;
else
return array1[i];
}
}
int main()
{
int array1[10],array2[9];
readArray(array1,array2);
cout<<"Lonely Integer is :"<<calcLonely(array1,array2);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.