Thread: Integer Array Please help

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    Integer Array Please help

    Hi..

    I have to create a code that will prompt the user to enter values for two int arrays. Each array is of length 5, with values such that 0 <= 'i' <= 99. Values entered for each array must be unique... The program will will compute the set intersection of the two arrays. That is, the program will display every value which the two arrays have in common.

    For example, if array A = {5, 4, 3, 2, 1} and array B = {2, 4, 6, 8, 10} then their intersection is {2, 4}. If the two arrays have no common elements, the program should print a 'NULL SET' message. }

    This is what I have created so far..

    Code:
    #include<stdio.h>
    /* Function prints Intersection of arr1[] and arr2[]
       m is the number of elements in arr1[]
       n is the number of elements in arr2[] */
    int print Intersection(int arr1[], int arr2[], int m, int n)
    {
      int i = 0, j = 0;
      while(i < m && j < n)
      {
        if(arr1[i] < arr2[j])
          i++;
        else if(arr2[j] < arr1[i])
          j++;
        else  /* if arr1[i] == arr2[j] */
        {
          printf(" %d ", arr2[j++]);
          i++;
        }
      }
    }



    I cant quite figure out how to further represent the two arrays in my code. Can anyone please give me a hint or two? Am I on the right track? I have a very limited knowledge of C programming and a tutor was helping me before.. Unfortunately now that classes are over he doesnt come to our lab anymore. Thank you so much...

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    I suggest a nested loop over both arrays to test equality of the elements, and storage of the elements in a third array that you can return. Something like:

    Code:
      for (i = 0; i < n; i++)
      {
        for (j = 0; j < m; j++)
        {
          if (arr1[i]==arr2[j])
          {
    	common[count] = arr1[i];
    	count++;
          }
        }
      }
    You'll have to initialize common to an array that is as big as the smaller of arr1 and arr2, and you will have to get count back to the calling function so that you only print out the first count elements of common.

    Is that helpful at all? I am not sure what you were asking, really.


    Note that the way you have done your incrementation, you may not end up comparing all the elements of both arrays. What happens if

    arr1 = [98 1 2 3 4]
    arr2 = [1 2 3 4 5]

    ?

    Trace your program out, you will find that it does not find any intersection at all, I think.
    Last edited by KBriggs; 05-17-2010 at 01:45 PM.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Where is the rest of your code? All programs must have a main().

    Show more work and we'll gladly help you. For example, can you correctly load the data into arrays and print the data back out?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Actually, KBriggs, the original poster's solution is better than yours. Yes, the arrays need to be sorted.

    See, sorting an array can be done in O(n log n). Then his algorithm is correct in O(n), which means a total of O(n + n log n).

    Your solution compares everything, which is O(n^2).

    His solution is better.


    jj121: Your solution is perfect so far. So good that I have to doubt you did it yourself to be honest, if you have to ask the question whether it's good.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Right, I was assuming that they were not sorted. If they are sorted then his solution is indeed better. However, since there was no mention made of any such sorting, his solution will not work in general unless he does that elsewhere, something he is obviously not aware of given his example arrays.

    I find that brute force is a good way to start learning, and you can refine things from there.

  6. #6

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by KBriggs View Post
    Right, I was assuming that they were not sorted. If they are sorted then his solution is indeed better. However, since there was no mention made of any such sorting, his solution will not work in general unless he does that elsewhere, something he is obviously not aware of given his example arrays.

    I find that brute force is a good way to start learning, and you can refine things from there.
    As I stated, sorting and then using this algorithm is faster (generally).
    And yes, it's copied, my expectation was correct ;-).

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    3
    Actually

    I believe, EVOEx, that I explained myself in the first post. I did fear that someone would accuse me of plagiarism - which is why I mentioned a tutor has been helping me. My tutor helped me to right that code. If anyone is doubting me, I understand. I tried posting this in 2 other forums and working on it on my own but without someone explaining it to me, its kind of hard. You see, my father died right before classes began and things have been really hectic for me, as you could assume. I missed a lot of classes because of that and now, at the end of the semester, I am paying for it. I am now just trying not to fail this class that I had to take. In my culture it is tradition that you take the deceased's ashes to India so I will be leaving in 3 days for the entire summer, I have a very limited amount of time to complete this assignment. I dont feel that I owed you this explanation but maybe it can help you to understand my situation. No, I did not write that code on my own. Yes, I posted in two other forums, but when people accused me of plagiarizing there I didnt feel I had to explain my code/I gave up on trying to explain myself before I even tried to.

    I have done all of my other homework assignments either by researching and hoping that I will eventually figure it out, or asking that tutor for help. That tutor is no longer available to me and this is all I have left. I hope that you will find it in your heart to understand.

    Thank you..

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    27
    This is really an assignment from class :-)
    Any how I hope the problem was fixed.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    3
    Are you in my class Adnan?

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by EVOEx View Post
    Actually, KBriggs, the original poster's solution is better than yours. Yes, the arrays need to be sorted.

    See, sorting an array can be done in O(n log n). Then his algorithm is correct in O(n), which means a total of O(n + n log n).

    Your solution compares everything, which is O(n^2).

    His solution is better.
    Not for the problem domain, which is defined as n=5. Or maybe it is, but big-O isn't going to tell you which approach is faster. And that's assuming faster is a useful way to evaluate "better" in this case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Realloc()
    By krazyxazn in forum C Programming
    Replies: 10
    Last Post: 01-27-2010, 10:05 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  4. Declaring an array with an integer
    By rpalmer in forum C Programming
    Replies: 4
    Last Post: 04-30-2007, 12:39 AM
  5. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 02:11 AM