Thread: Help for school assignment

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    Unhappy Help for school assignment

    Code:
    I've been trying to resolve a problem that was given at school, but as a rookie in C, I've been having problems. Here's what's asked:
    Create a function named intersection (void) that receives two vectors (int), the number of elements of the first vector (int), the number of elements of the second vector (int), and returns a third vector constituted by the elements that appear on both the first and second vector. The function should also return the number of elements that are present in the third vector.
    
    Eg:
    If the two vectors are: u= <1,2,3,4>, v<2,5,6,7>
    It should return:
     w=<2>, size= 1
    
    Note: I’m sorry for the miswritten words…I’m not English, and am translating the problem from my birth language :/  And I'm using Visual C++2008
    
    Here's what I've done so far :
    #include <stdio.h>
    
    void intersection (int *a, int n_a, int *b, int n_b, int *w, int *n_w)
    {
    	int i;
    	int x;
    	x = 0;
    	for (i=0; i<n_a; i++)
    	{
    		if (a[i] == b[i])
    			w[i]=a[i];
    		else
    			0;
    	}
    	for (i=0; i<n_a; i++)
    	{
    		if (w[i] == x)
    			x++;
    		else
    			0;
    	}
    	
    }
    
    So, when I compile the function, it has no errors or warnings, but when I try to submit it, it says "Compile Time Error". Can someone help me pointing out what's wrong?? Thank you very much. I used int *w, int *n_w to express the output arguments.

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    You have to have a main function. Something like this:

    Code:
    int main(void)
    {
    	int a;
    	int b;
    	int w;
    	int n_a;
    	int n_b;
    	int n_w;
    	
    // Don't forget to actually populate your variables!
    
    	intersection(&a, n_a, &b, n_b, &w, &n_w);
    	
    	printf("w=<&#37;d>, size=%d\n", w, n_w);
    	fflush(stdout);
    	
    	return 0;
    }
    Also, you'll need a prototype for your intersection function at the top of your source:
    Code:
    void intersection (int *a, int n_a, int *b, int n_b, int *w, int *n_w);

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    We haven't been using main in these exercises...It's only required that we send the function that's asked. So I figured the function would be wrong. I'll try what you said anyway, thank you so much!

  4. #4
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    That's all fine and good that you don't need to submit your main() to your teacher, but you won't be able to test your function in any compiler without giving it a main().

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Right. I'll try it out then, maybe I'll figure out what's wrong.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    for each element in vector 1, if it is in vector 2 add it to vector 3 and increment the count of items in vector 3.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    else 0;
    This is meaningless.

  8. #8
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by MacGyver View Post
    Code:
    else 0;
    This is meaningless.
    Yes, and most compilers will actually tell him that it is.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Code:
    How about this? Whats wrong here?
    
    void intersection (int *a, int n_a, int *b, int n_b, int *w, int *n_w)
    {
    	int i;
    	int j;
    	*n_w = 0;
    	for (i=0; i<n_a; i++)
    	{
    		for (j=0; j<n_b; j++)
    			if (a[i] == b[j])
    				w[i]=a[i];
    				n_w++;
    	}
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're leaving "gaps" in your result vector w -- if a[i] does not appear anywhere in b, then w[i] doesn't get written to. So, you'll need to keep a separate count, maybe in the variable n_w, of where you should write to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM