Thread: Linear Search of Two Arrays (without user input)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Linear Search of Two Arrays (without user input)

    Hi! How can you search an array to see if two arrays contain the same number.

    For example:

    Code:
    arr1 = {1, 2, 3, 4, ...}
    arr2 = {6, 5, 1, 0, ...}
    
    
    int foundAt = linearSearch(arr1, 10, arr2[0]);
    This will only compare if the first number of arr2 exists in arr1, but I want to compare for all of the numbers that exist in arr2.

    I get that I probably should use a for-loop somehow, but don't really get how.

    Idea

    Code:
    int i; 
    for (i = 0; i < 10; i++) {
        int j;
        for (j = 0; j < 10; j++) {
            foundAt[i] = linearSearch(arr1, 10, arr2[j]);
            if (foundAt[i] == -1) {
                 // Add number to array 
            }
             else {
                  // Don't add number to array
             }
        }
    }
    Code:
    int linearSearch(const int arr[], int arrSize, int taken) {
    	int found = 0; 
    	int location = -1; 
    	
    	int i;
    	for (i = 0; i < arrSize && !found; i++) {
    		if (taken == arr[i]) {
    			found = 1;
    			location = i;
    		}
    	}
    	return location;
    }
    Last edited by DecoratorFawn82; 12-02-2017 at 10:51 AM.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    you are using two arrays, each one contains numbers, you are wanting to compare each number one at a time in one array against all of the numbers in the other array.

    1 2 3 4 5 6 7 8
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    the two lines, they all contain a separate number of equal or not values. how do you hold one in place while going down the line in the other array and comparing its value to the one you're holding on to?
    then what do you need to do the that second array when you are done checking all of its number values against the first arrays number to get it to start all over again from the beginning?
    1 2 3 4 5 6 7 8
    hold that one
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    move down the line on the second one,
    then restart the second line from 1 while moving the first line down one place, then just repeat until the first line has been compared to all of the numbers values in the second line.

    you should now how to printf if found to let you know it got a hit.

    I think you should get it to work first then figure out how to make that into a function.Yes it requires loops and one if statement to gett'er done
    Last edited by userxbw; 12-02-2017 at 11:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-20-2015, 01:03 PM
  2. Replies: 3
    Last Post: 12-20-2015, 01:03 PM
  3. Difference Between A Linear Search And Binary Search
    By ImBack92 in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 08:47 AM
  4. Search file for user input
    By ch68813 in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2007, 02:24 PM
  5. Linear search and 2d arrays?
    By sven in forum C Programming
    Replies: 0
    Last Post: 02-08-2002, 05:26 AM

Tags for this Thread