Use the techniques for generating random numbers and use for loops to process th
ID: 3581645 • Letter: U
Question
Use the techniques for generating random numbers and use for loops to process the indicated arrays. Develop a program that does the following:
1. Create and display an array, x, of fifty integers, i, selected at random from the range 0 <= i < 20.
2. Create and display an array, y, of fifty integers, j, selected at random from the range 0 <= j < 20.
3. Compute and display an array, a, that contains the integers which appear in x and not y. 4. Compute and display an array, b, that contains the integers which appear in y and not x. The arrays a and b should not contain any duplicates.
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
void RemoveDup(int *a, int& n)
{int i,j,k;
for(i=0;i<n;++i)
for(j=i+1;j<n;)
{
if(a[i]==a[j])
{
for(k=j;k<n-1;++k)
a[k]=a[k+1];
--n;
}
else
++j;
}
}
int main(int argc, char const *argv[])
{
int x[50],y[50];
srand(time(0));
for (int i = 0; i < 50; ++i)
{
x[i]=rand()%20+0;
}
for (int i = 0; i < 50; ++i)
{
y[i]=rand()%20+0;
}
int a[50],b[50];
int c=0;
for (int i = 0; i < 50; ++i)
{
int n=x[i];
int j;
for ( j = 0; j < 50; ++j)
{
if(n==y[j])
break;
}
if(j==50)
a[c++]=n;
}
RemoveDup(a,c);
cout<<"Array is a is ";
for (int i = 0; i < c; ++i)
{
cout<<a[i]<<" ";
}
c=0;
for (int i = 0; i < 50; ++i)
{
int n=y[i];
int j;
for ( j = 0; j < 50; ++j)
{
if(n==x[j])
break;
}
if(j==50)
b[c++]=n;
}
RemoveDup(b,c);
cout<<" Array is b is ";
for (int i = 0; i < c; ++i)
{
cout<<b[i]<<" ";
}
return 0;
}
================================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ random.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Array is a is
17 8
Array is b is
14 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.