Question

Write a c program that finds the uncommon elements from two array elements using pointers only...

Write a c program that finds the uncommon elements from two array elements using pointers only .

For example : Enter the length of the array one : 5

Enter the elements of the array one: 9 8 5 6 3

Enter the length of the array two: 4

Enter the elements of the array two: 6 9 2 1

output: 8 5 3 2 1

void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size);

The function should use pointer arithmetic – not subscripting–to visit array elements. In

other words, eliminate the loop index variables and all use of the [] operator in the function.

The uncommon_ele function finds the elements that in either array a or array b, but not both, and stores the result in array c. The function takes an int array parameters a1 and 2 as the first parameter and third parameters and the number of elements of the arrays as the second parameter.

The sixth parameter size points to a variable in whi ch the function will store thenumber of

actual elements in array c.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//code is explained in comments

#include <stdio.h>
#include <stdlib.h>

void uncommon_ele(int *a,int n1,int*b,int n2,int *c,int *size){
   int actualSize=0;
   for(int i=0;i<n1;++i){//to get uncommon elements in a
   int statusflag=1;
   for(int j=0;j<n2;++j){
  
   if(a[i]==b[j]){
   statusflag=0;
   break;
   }
   }
   if(statusflag==1){
   c[actualSize++]=a[i];
   }
   }
   for(int i=0;i<n2;++i){//to get uncommon elements in b
   int statusflag=0;
   for(int j=0;j<n1;++j){
  
   if(b[i]==a[j]){
   statusflag=1;
   break;
   }
   }
   if(statusflag==0){
   c[actualSize++]=b[i];
   }
   }
  
   *size=(actualSize);//assign size
   c=(int*)realloc(c,sizeof(int)*(actualSize));//reallocate with uncommon element memory
}

int main(){
   int *a;
   int *b;
   int *c;
   int size=0;
   int n1;
   int n2;
   printf("Enter length of array one : ");
   scanf("%d",&n1);
   a=(int*)malloc(sizeof(int)*n1);//allocate memory of n1 size
   printf("Enter the elements of the array one ");
   for(int i=0;i<n1;i++){//enter elements into arrray a
       scanf("%d",&a[i]);
   }
   printf("Enter length of array two : ");
   scanf("%d",&n2);
   b=(int*)malloc(sizeof(int)*n2);//allocate memory of n2 size
   printf("Enter the elements of the array two ");
   for(int i=0;i<n2;i++){//enter elements into arrray b
       scanf("%d",&b[i]);
   }
   c = (int*) malloc((n1+n2) * sizeof(int));//allocate memory of size n1+n2
   uncommon_ele(a,n1,b,n2,c,&size);//now call function by passing ref
   printf("uncommon_elements are %d ",size);
   for(int i=0;i<(size);++i){//print the uncommon_elements
  
   printf("%d ",c[i]);
   }
   if(n1>0){//deallocate the memory allocated in heap
   free(a);
   }
   if(n2>0){
   free(b);
   }
   if(size>0){
   free(c);
   }
  
   return 0;
}

//output:


//PS:If you like the answer ,please upvote as it enhances my profile

Add a comment
Know the answer?
Add Answer to:
Write a c program that finds the uncommon elements from two array elements using pointers only...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • average Function Write a function that finds an average of the array using only pointers (and...

    average Function Write a function that finds an average of the array using only pointers (and pointer arithmetic) to move through the array. Test the function in a driver program. Function header: double average(int* array, int size)

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • - Write a program that performs several operations on an array of positive ints. The program...

    - Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array...

  • How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...

    How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() {    // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic    // tic tac toe board is an array of int pointers    // each int pointer in the board points to a row    // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)    const...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • Assembly Language Program Help Write a procedure named CountNearMatches that receives pointers to two arrays of...

    Assembly Language Program Help Write a procedure named CountNearMatches that receives pointers to two arrays of signed doublewords, a parameter that indicates the length of the two arrays, and a parameter that indicates the maximum allowed difference (called diff) between any two matching elements. For each element x(i) in the first array, if the difference between it and the corresponding y(i) in the second array is less than or equal to diff, increment a count. At the end, return a...

  • C++. Write a program that copies the contents of one array into another array but in...

    C++. Write a program that copies the contents of one array into another array but in reverse order using pointers.           - in main()                    - define 2 arrays of type int, 10 elements each                              - initialize one array with numbers 1 through 10                              - initialize all elements of the second array to 0                    - call function reverseCopy with both arrays as arguments                    - display the values of array number 2 (reversed order)           - reverseCopy...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT