c++ program help fix the code set operation arrys

the question is :
Q-1: Set Operations:
Write a C++ program that uses one dimensional arrays to implement Set operations i.e. union, difference and intersection.
Special requirements:
Each set can contain maximum of 50 elements except the resultant set that can have 100 elements.
Elements of a set are integers from 1 to 30.
Use 0 as the end of a set.
Your program must implement the following three functions in addition to the main function. Function header of the three functions is given below:

void unionOperation(int setA[], int setB[], int resultantSet[]);
This function should receive array A and B as parameter and calculate the union of the two arrays and store the resultant array in resultantSet.


void intersectionOperation(int setA, int setB, int resultantSet[]);
This function should receive array A and B as parameter and calculate the intersection of the two arrays and store the resultant array in resultantSet.

void differnceOperation(int A, int setB, int resultantSet[])
This function should receive array A and B as parameter and calculate the difference of the two arrays and store the resultant array in resultantSet.
------------------------
------------------
the error is
rad() was not declared in this scoop

------------
the code is
---------------
#include<iostream>

using namespace std;

void unionOperation(int setA[], int setB[], int resultantSet[]);
void intersectionOperation(int setA[], int setB[], int resultantSet[]);
void differenceOperation(int A[], int setB[], int resultantSet[]);
int m; //Variable to hold first set size.
int n; //Variable to hold second set size.

int main()
{

int setA[50]; //Array for First set.
int setB[50];//Array for Second set.
int resultantSet[100];//Resultant set

int range = 30;


//Getting input for first set size.
while(1)
{
cout<<"Please enter valid number of elements for first set: ";
cin>>m;
cout<<m<<endl;
if(m>50)
cout<<"Number should be less than 50"<<endl;
else
break;
}

//Getting input for second set size
while(1)
{
cout<<"Please enter valid number of elements for second set: ";
cin>>n;
cout<<n<<endl;
if(n>50)
cout<<"Number should be less than 50"<<endl;
else
break;
}

for(int i =0;i<m-1;i++)
//loop to fill elements for first set.
{
setA[i]= rand() % range + 1; //here error
}
setA[m-1]=0;
//loop printing first set elements.
cout<<"Elements of your first set = { ";
for(int i=0;i<m;i++)
{
cout<<setA[i];
if(i<m-1)
{
cout<<" , ";
}
}
cout<<" }";
cout<<"\n\n";
//loop to fill second set elements.
for(int i=0;i<n-1;i++)
{
setB[i]= rand() % range + 1;
}
setB[n-1]=0;
//loop printing second set elements.
cout<<"Elements of your second set = { ";
for(int i=0;i<n;i++)
{
cout<<setB[i];
if(i<n-1)
{
cout<<" , ";
}
}
cout<<" }";
cout<<"\n\n";

unionOperation(setA, setB, resultantSet);
intersectionOperation(setA, setB, resultantSet);
differenceOperation(setA, setB, resultantSet);

return 0;
}

void unionOperation(int setA[], int setB[], int resultantSet[])
{
int l = 0; // index for the resultant array

copy(setA,setA+(m-1),resultantSet); // copy the setA elements to resultant set

l=m-1;
int e=l;

int tempRes[m]; // to eliminate duplicate entries in the resultant set
copy(setA,setA+(m-1),tempRes);

for(int i=0; i<n-1; i++)
{
int found=0;
for(int j=0; j<e; j++)
{
if (setB[i] == tempRes[j])
{
found = 1;
tempRes[j]=0; // if the element in set B is already present in resultant set(set a) then mark is made in result set,
//to denote one instance of that element is already paired if there are any duplicates.
break;
}
}
if (found==0)
{
resultantSet[l]=setB[i]; // if the element in setB is not found in resultant set(set a), then it is copied to result set
l++;
}
}
cout<<" Union of A and B is { ";
for(int i = 0; i < l; i++)
{
cout<<resultantSet[i]<<" ";
}
cout<<"}"<<endl;
}

void intersectionOperation(int setA[], int setB[],int resultantSet[])
{
int l=0;
int tempsetB[n];
copy(setB, setB+n, tempsetB); // copy of set b is made temporarily to keep track of matched
// values in set a and b
for(int i=0; i<m-1;i++)
{
for(int j=0; j<n-1;j++)
{
if (setA[i] == tempsetB[j]) // if both sets A and B have common element then move to resultant set
{
tempsetB[j]=0;
resultantSet[l]= setA[i];
l++;
break;
}
}
}
cout << "Intersection of A and B is { ";
for(int i=0; i<l; i++)
{
cout << resultantSet[i] << " ";
}
cout << "}" << endl;
}

void differenceOperation(int setA[], int setB[],int resultantSet[])
{
int l=0;
int tempsetB[n];
copy(setB, setB+n, tempsetB);
for(int i=0; i<m-1; i++)
{
int found=0;
for(int j=0; j<n-1; j++)
{
if (setA[i] == tempsetB[j])
{
tempsetB[j]=0;
found=1;
break;
}
}
if(found==0)
{
resultantSet[l]=setA[i]; // if an element in set A is not found in set B then copy that element to resultant set
l++;
}
}
cout << "Difference of A and B is { ";
for(int i=0; i<l; i++)
{
cout << resultantSet [i] << " ";
}
cout << "}" << endl;
}
First, it's rand(), not rad(). That makes a difference.
It seems that you have forgotten to include <stdlib.h>, that header file contains function rand().
Just some thoughts.
Since the exercise doesn't say to get the input from the keyboard it would be much easier to hard code the values also it makes testing easier.
Also it's good practice to develop and test a program in small steps, like
1. implement and test union operation
2. implement and test difference operation
3. implement and test intersection operation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

void unionOperation(int setA[], int setB[], int resultantSet[]);

int main()
{
  int setA[50] = {1, 2, 3, 4, 0};
  int setB[50] = {1, 2, 5, 6, 0};
  int tempResult[100] = {0};
  int unionResult[100] = {0}; // TO DO fill with correct values 

  unionOperation(setA, setB, tempResult);
 
  // TO DO compare tempResult with unionResult

  // TO DO do the same for difference and intersection operation

  return 0;
}

void unionOperation(int setA[], int setB[], int resultantSet[])
{
  // your code 
}


those kind of errors occur and handled after running for the first times, as well as debugging.your work should be problems-avoid derived and you should get to I used to work with a nice one called checkmarx that pretty much made it..there are few programs which help you detect those kind of vulnerabilities in your code and warns you.detect those kind of cases and consider them.
Topic archived. No new replies allowed.