Q-1: Set Operations: Write a ( C++ Or Java, Java is better ) program that uses o
ID: 3731749 • Letter: Q
Question
Q-1: Set Operations:
Write a (C++ Or Java, Java is better) program that uses one dimensional arrays to implement Set operations i.e. complement and intersection.
Special requirements:
Each set can contain maximum of 15 elements except the resultant set that can have 30 elements.
Elements of a set are integers from 1 to 30.
Your program must implement the following two functions in addition to the main function. Function header of the two functions is given below:
void complementOfSet(int universalSet[], int setA[], int resultantSet[]); This function should receive array universalSet and setA as parameter and calculate the complement of the array setA and store the resultant array in resultantSet.
void intersectionOperation(int setA[], int setB[], int resultantSet[]); This function should receive array setA and setB as parameter and calculate the intersection of the two arrays and store the resultant array in resultantSet.
Your main function should read the elements of array A (setA) , array B (setB) and universal array (universalSet) from the user and call the appropriate function to perform a specific Set operation and display the elements of the resultant set/array.
Sample Input /Output:
Enter elements of Set A: 2 4 6 7 9
Enter elements of set B: 1 3 5 7 9 10 18
Enter elements of Universal set: 1 2 3 4 5 6 7 9 10 11 12 15
Complement of A: 1, 3, 5, 10, 11, 12, 15
Intersection of A and B is: 7, 9
Q-2:
Write a (C++ Or Java, Java is better) program to check whether an integer number is a term of Fibonacci series.
In mathematics, the Fibonacci series are the numbers in the following integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144…………….
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. Your program should read an integer number from the user and check whether the number is a term of the Fibonacci series. Assume that maximum integer number entered by user is 10,00,000.
Sample Input / Output:
Enter an Integer number: 121393
121393 is a term of Fibonacci Series.
Sample Input / Output:
Enter an Integer number: 157817
157817 is not a term of Fibonacci Series.
Explanation / Answer
1. a set can have maximum of 15 elements and each element can have value between 1 and 30. So here we represent a set by an array of length 30 of boolean type where if the set contains i then the ith position is set to true.
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int setA[]=new int[30];
int setB[]=new int[30];
int universalSet[]=new int[30];
int resultSet[]=new int[30];
System.out.println("Enter number of elements in set A:");
int nA=sc.nextInt();
System.out.println("Enter "+nA+" space separated elements for set A: ");
for(int i=0;i<nA;i++){
int inp=sc.nextInt();
setA[inp-1]=1;
}
System.out.println("Enter number of elements in set B:");
int nB=sc.nextInt();
System.out.println("Enter "+nB+" space separated elements for set B: ");
for(int i=0;i<nB;i++){
int inp=sc.nextInt();
setB[inp-1]=1;
}
System.out.println("Enter number of elements in universal set:");
int nU=sc.nextInt();
System.out.println("Enter "+nU+" space separated elements for Universal set : ");
for(int i=0;i<nU;i++){
int inp=sc.nextInt();
universalSet[inp-1]=1;
}
System.out.println("Set A:");
printSet(setA);
System.out.println("Set B:");
printSet(setB);
System.out.println("Universal Set :");
printSet(universalSet);
complementOfSet(universalSet,setA,resultSet);
System.out.println("complement of set A:");
printSet(resultSet);
resultSet=new int[30];
intersectionOperation(setA,setB,resultSet);
System.out.println("Intersection of set A and B:");
printSet(resultSet);
}
private static void printSet(int set[]){
for(int i=0;i<set.length;i++){
if(set[i]==1){
System.out.print(i+1+" ");
}
}
System.out.println();
}
private static void complementOfSet(int universalSet[], int setA[], int resultantSet[] ){
for(int i=0;i<universalSet.length;i++){
if(universalSet[i]==1 && setA[i]==0){//i present in universalSet but not in A
resultantSet[i]=1;
}
}
}
private static void intersectionOperation(int setA[], int setB[], int resultantSet[]){
for(int i=0;i<setA.length;i++){
if(setA[i]==1 && setB[i]==1){//if i present in both set A and B
resultantSet[i]=1;
}
}
}
}
2. A number is n Fibonacci if and only if one or both of (5*n2 + 4) or (5*n2 – 4) is a perfect square
using this property, the code is:
class GFG
{
// A utility method that returns true if x is perfect square
static boolean isPerfectSquare(int x)
{
int s = (int) Math.sqrt(x);
return (s*s == x);
}
static boolean isFibonacci(int n)
{
return isPerfectSquare(5*n*n + 4) ||
isPerfectSquare(5*n*n - 4);
}
// Driver method
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++)
System.out.println(isFibonacci(i) ? i + " is a term of Fibonacci Series." :
i + " is a not term of Fibonacci Series.");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.