error with the code... help out

here is the code that takes two arrays(with no two elements of array same) as input and outputs a third array which is the union of the two arrays. i.e the third array contains all elements of 1st array and non repeating elements of second array.
ex: a={1,2,3,4}
b={4,5,6} , then it should return
c={1,2,3,4,5,6}
I have marked the errors in the code. please help.
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream.h>
#include<conio.h>

int * uni(int[],int[],int,int);

void main()
{
 int a[10],b[10],n1,n2,c[20];
 cin>>n1;
 for(int i=0;i<n1;i++)
 cin>>a[i];

 cin>>n2;
 for(i=0;i<n2;i++)
 cin>>b[i]; 

 c= uni(a,b,n1,n2); // error1: Lvalue required
 
 for(i=0;i<n1+n2;i++)
 cout<<c[i]<<"\t";

getch();
}

int * uni(int a[],int b[],int n1,int n2)   
{
 int c[n1+n2],k=n1;    // error2: constant expression required
 for(int i=0;i<n1;i++)
 c[i]=a[i];

 for(i=0;i<n2;i++)
 {
   for(int j=0;j<n1;j++)
    {
      if(a[j]==b[i]) break;
      else{
            c[k]=b[i];
            k++;
           }
    }
 }
return c;
}
Last edited on
You did not define the variable c in the main before its using in the statement

c= uni(a,b,n1,n2); // error1: Lvalue required

m sorry... in the original program i have declared c(forgot to include it here)... i will just edit the code... it is still giving the same error
Variable c is defined as

int c[20];

but you are trying to assign to it a pointer

c= uni(a,b,n1,n2); // error1: Lvalue required

because return type of function un is the type int *. See the declaration of un

int * uni(int[],int[],int,int);

Moreover dimension of arrays shall be specified as const expressions. So the statement

int c[n1+n2],k=n1; // error2: constant expression required

is invalid.


I think you should rewrite function un by adding one more parameter that will be specify the destination array.

Last edited on
i think my concept about pointers and arrays is not very clear.
when i made the statement return c in uni function, i thought that it is returning nothing but the pointer to the base address of c and that it would be appropriate to store this value in array name c( which is again a pointer to the base address of c).

can you please help me understand the concept. and i would appreciate if you could just show me what changes i should make in my code.
Try the following realization of the function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int uni( int c[], int a[], int b[], int n1, int n2 )   
{
   int k = 0;
   for( ; k < n1; k++ )  c[k] = a[k];

   for( int i = 0; i < n2; i++ )
   {
      int j = 0;
      for( ; j < n1 && a[j] != b[i]; j++ );
      if ( j == n1 ) c[k++] = b[i];
   }

   return ( k );
}


And the main

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

int  uni( int[], int[], int[], int, int );

int main()
{
    int a[10], b[10], n1, n2, c[20];
    std::cin >> n1;
    for ( int i=0; i < n1; i++ ) std::cin >> a[i];

    std::cin >> n2;
    for( int i=0; i<n2; i++ ) std::cin >> b[i]; 

    int k = uni(c, a, b, n1, n2 );
 
    for ( int i=0; i < k; i++ )  std::cout << c[i] << " ";

    getch();

    return 0;
}
Last edited on
Topic archived. No new replies allowed.