Thread: How to print the unique elements in two seperate arrays??

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    4

    How to print the unique elements in two seperate arrays??

    For this program is have to first input a list of up to 10 positive integers and end with -1, then enter another list of up to 10 positive integers and end with -1. Therecan be duplicate numbers in the list and the numbers are not in any particular order. There can be numbers inthe second list that are also in the first list.

    the program is to print a list, in ascending order, of all the unique values that appear in these lists.

    I have been able to make it so that the user enters both lists and ends with -1, but I can figure out how to get the unique values to print.

    This is what I have so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void) {
    
    
    	int arrayOne[10];
    	int arrayTwo[10];
    	int k, x, y, z;
    	int counter = 1, dupCounter = 1;
    	int swap;
    
    
    	setvbuf(stdout, NULL, _IONBF, 0);
    
    
    	printf("Enter first list of positive numbers: \n");
    
    
    	for (x = 0; x < 10; x++) {
    		scanf("%d", &arrayOne[x]);
    		if ((arrayOne[x]) == -1)
    			break;
    		counter = counter + 1;
    	}
    	
    	printf("Enter second list of positive numbers: \n");
    
    
    	for (y = 0; y < 10; y++) {
    		scanf("%d", &arrayTwo[y]);
    		if ((arrayTwo[y]) == -1)
    			break;
    		dupCounter = dupCounter + 1;
    	}
    
    
    	//sort numbers
    	for (x = 0; x < counter - 1; x++) {
    		for (y = 0; y < counter - x - 1; y++) {
    
    
    			if (arrayOne[y] > arrayOne[y + 1]) {
    				swap = arrayOne[y];
    				arrayOne[y] = arrayOne[y + 1];
    				arrayOne[y + 1] = swap;
    			}
    		}
    	}
    
    
    	for (x = 0; x < counter; x++) {
    		for (y = 0; y < dupCounter; y++) {
    			if ((arrayOne[x]) != (arrayTwo[y])) {
    
    
    				printf("%d %d ", arrayOne[x], arrayTwo[y]);
    			}
    
    
    		}
    	}
    
    
    	return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your specification is unclear. It sounds like there's no point in having two lists since it sounds like you could just make one list of both of them, sort it, and print the unique values. I feel that's probably not what you actually want.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Consider this loop in place of your final effort.
    Code:
    		for (y = 0; y < dupCounter; y++) {
    			if ((arrayOne[x]) != (arrayTwo[y])) {
    				printf("diff at pos %d\n", y);
    			} else
    				printf("same as pos %d\n", y );
    			}
    		}
    		printf("Done checking\n");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing unique array elements
    By sean_cantab in forum C Programming
    Replies: 22
    Last Post: 04-12-2016, 06:12 PM
  2. Replies: 4
    Last Post: 03-19-2013, 08:37 AM
  3. Using seperate elements in a string array?
    By opeth in forum C Programming
    Replies: 3
    Last Post: 04-28-2012, 09:07 PM
  4. Importing from 2 columns to seperate arrays
    By tomeatworld in forum C Programming
    Replies: 5
    Last Post: 12-04-2010, 10:29 AM
  5. Using write() to print array elements
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-01-2002, 09:18 PM

Tags for this Thread