PLEASE!!! help intersections problems

This is a discussion on PLEASE!!! help intersections problems within the C Programming forums, part of the General Programming Boards category; i have to write a program that takes two inputs as arrays and then outputs the intersection. please help as ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    20

    PLEASE!!! help intersections problems

    i have to write a program that takes two inputs as arrays and then outputs the intersection.

    please help as im really new at this, lol.

    here is what i have come up with so far.

    Code:
    void main()
    {
        char a[7];
        char b[7];
        char out[7];
    	
    
        printf( "Please enter your name in lower case: " );
        fgets( a, 7, stdin );
    
       
       
       
        printf( "Enter your last name: " );
        fgets( b, 7, stdin );
        
    	printf( "\nString 'a' %s \n", a);
    	printf( "\nString 'b' %s \n", lastname);
       
    	if (a[0] == b [0]) {
    		out[0] = a[0];
    	}
    	else if (a[1] == b [1]) {
    		out[1] = a[1];
    	}
    	else if (a[2] == b[2]) {
    		out[2] = a[2];
    	}
    	else if (a[3] == b [3]) {
    		out[3] = a[3];
    	}
    	else if (a[4] == b[4]) {
    		out[4] = a[4];
    	}
    	else if (a[5] == b[5]) {
    		out[5] = a[5];
    	}
    	else if (a[6] == b[6]) {
    		out[6] = a[6];
    	}
        
        
        
    	
        printf( "\nThe intersection is  %s \n", out);
    
        getchar();
    }
    when i run this program, if any of the if statments are met it just seems to put the whole lot in to the output (fullname)

    any help welcome

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I'm not completely sure what you are trying to achieve here. If you want 'out' to display all characters that are the same and match array positions then you will want to run a seperate 'if' check for each character to test for a match otherwise you will only be able to get the first match. You would also need to initialize the out character array as well.

    Alternatively if you only want to print the first character that matches both in value and array position then this code should do what you want:
    Code:
    #include <stdio.h>
    int main()
    {
        char a[7];
        char b[7];
        char out='\0';
    
        printf( "Please enter your name in lower case: " );
        fgets( a, 7, stdin ); 
       
        printf( "Enter your last name: " );
        fgets( b, 7, stdin );
        
    	printf( "\nString 'a' %s \n", a);
    	printf( "\nString 'b' %s \n", b);
       
    	if (a[0] == b [0]) 
    		out = a[0];
    	else if (a[1] == b [1])
    		out = a[1];
    	else if (a[2] == b[2])
    		out = a[2];
    	else if (a[3] == b [3]) 
    		out = a[3];
    	else if (a[4] == b[4])
    		out = a[4];
    	else if (a[5] == b[5]) 
    		out = a[5];
    	else if (a[6] == b[6])
    		out = a[6];
        	
        printf( "\nThe intersection is  %c \n", out);
    
        getchar();
    }
    A couple of other things worth noting is that fgets() places the '\n' character in the buffer when you hit return, and also its best to use 'int main()'.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include <stdio.h>
    
    int main()
    {
        char a[10];
        char b[10];
        char out[20];
    
        printf( "Please enter your name in lower case: " );
        fgets( a, sizeof a, stdin );
       
        printf( "Enter your last name: " );
        fgets( b, sizeof b, stdin );
        
    	printf( "\nString 'a' &#37;s \n", a);
    	
    	/* You havn't declared lastname have you 
    	printf( "\nString 'b' %s \n", lastname); */
        printf( "\nString 'b' %s \n", b); 
       
    	if (a[0] == b [0]) 
    		out[0] = a[0];
    	
        if (a[1] == b [1]) 
    		out[1] = a[1];
    	
        if (a[2] == b[2]) 
    		out[2] = a[2];
    	
        if (a[3] == b [3]) 
    		out[3] = a[3];
    	
        if (a[4] == b[4]) 
    		out[4] = a[4];
    	
        if (a[5] == b[5]) 
    		out[5] = a[5];
    	
        if (a[6] == b[6]) 
    		out[6] = a[6];
    		
    	out[7] ='\0';
        
        printf( "\nThe intersection is  %s \n", out);
        getchar();
        return 0;
    }
    
    /* my outout
    Please enter your name in lower case: test
    Enter your last name: test
    
    String 'a' test
    
    
    String 'b' test
    
    
    The intersection is  test
    */
    Have a look at the code, give more attention on the changes done on the code. Few thing which need to be noted

    1. main should return value
    2. lastname wasn't declared
    3. compare would have been very easy if it was implemented using loops.
    4. Need more error checking

    Hope that will get you start.

    ssharish2005

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    20
    i have made a small mistake on the code i put up, it should not say enter first name and last name, i want it to just work with intergers. sorry about that. il have a go now, thanks very much

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Hope that the following code will give you an idea of how to do things..
    You want to create a string with the first and last name of a person.
    Thats how you may do it mate:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char *Interesection(char *string1, char* string2)
    {
         if(string1 && string2)
         {
                    int i;
                    int j = 0;
                    //For the given exercise we apply a space between the two strings so + 1 for the space, + 1 for the '\0' = +2
                    char *interS = (char *)calloc(strlen(string1) + strlen(string2) + 2, sizeof(char));
                    if(interS)
                    {
                              for(i = 0; i < strlen(string1); interS[j++] = string1[i], i++);
                              interS[j++] = ' ';
                              for(i = 0; i < strlen(string2); interS[j++] = string2[i], i++);
                              
                              //Remember that the interesection of two stings will have max the sum of the strlen characters + 1 for space
                              //and string[j] will show the '\0' meaning we have strlen(string1) + strlen(string2) + 2 characters.
                              if(j == strlen(string1) + strlen(string2)+1)
                              {
                                   interS[j] = '\0';
                                   return interS;
                              }
                              else
                              {
                                  free(interS);
                                  return NULL;
                              }
                    }
                    else
                    {
                        printf("Memory Error.\n");
                        return NULL;
                    }
         }
         else
         {
             printf("NULL strings.\n");
             return NULL;
         }
    }
    
    int main(int argc, char *argv[])
    {
      char string1[80] = "Cristiano";
      char *string2 = "Ronaldo";
      char *r = NULL;
      
      r = Interesection(string1, string2);
      
      printf("%s\n", r);
      
      //Strcat does the trick but it gives no space between the strings, and need the destination first string to be large 
      //enough to hold the result.
      strcat(string1, string2);
      
      printf("%s\n", string1);
      system("PAUSE");	
      return 0;
    }
    Printed Results..

    Cristiano Ronaldo
    CristianoRonaldo
    Press any key to continue . . .

    Hope i helped..

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    20
    ok, so now with some excellent help from you guys ive got this.
    Code:
    
    #include <stdio.h>
    
    int main()
    {
        char a[10];
        char b[10];
        char out[20];
    
        printf( "Please enter a Maximum of 7 whole numbers: " );
        fgets( a, sizeof a, stdin );
    	
    	
    
        printf( "Enter your last name: " );
        fgets( b, sizeof b, stdin );
        
    
    
    
    
    	printf( "\nString 'a' &#37;s \n", a);
    	
    	
        printf( "\nString 'b' %s \n", b); 
       
    
    
    	if (a[0] == b [0]) 
    		out[0] = a[0];
    	else {
    		out[0] = 45;
    	}
        if (a[1] == b [1]) 
    		out[1] = a[1];
    	else {
    		out[1] = 45;
    	}
        if (a[2] == b[2]) 
    		out[2] = a[2];
    	else {
    		out[2] = 45;
    	}
        if (a[3] == b [3]) 
    		out[3] = a[3];
    	else {
    		out[3] = 45;
    	}
        if (a[4] == b[4]) 
    		out[4] = a[4];
    	else {
    		out[4] = 45;
    	}
        if (a[5] == b[5]) 
    		out[5] = a[5];
    	else {
    		out[5] = 45;
    	}
        if (a[6] == b[6]) 
    		out[6] = a[6];
    	else {
    		out[6] = 45;
    	}
    	if (a[7] == b[7]) 
    		out[7] = a[7];
    	else {
    		out[7] = 45;
    	}
    	//out[8] ='\0';
        
        printf( "\nThe intersection is  %s \n", out);
        getchar();
        return 0;
    }
    not a big thing, but im playing with trying to get the program to ignore eccess inputs for "a" and "b", but not with much luck.
    any suggestions

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    20
    by the way, thanks again, great help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21