Thread: Merging arrays

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    1

    Merging arrays

    Hi, i have some problem with merging two arrays. Array "out" should merge with itself - merge all new user input. I cant figure out how to do this ... please help and sorry for my english - its not my native language

    Code:
    #include<stdio.h>
    
    #define MIN 80 
    #define MAX 1000
    
    int getl(char in[], int min);
    void copy(char data[], char actual[]);
    
    main(){
    
        char line[MAX];
        char out[MAX];
        int ll;
    
        ll = 0;
        while(ll = getl(line, MIN) && line[0] != '.')
        {
            copy(out, line);
    
        }
    
        printf("%s", out);
        return 0;
    }
    
    int getl(char in[], int min)
    {
        int i,c;
        for(i = 0; i < MAX-1 && (c = getchar()) != EOF && c != '\n'; ++i)
        {
            in[i] = c;
        }
    
        if(c == '\n')
        {
            in[i] = c;
            ++i;
        }
    
        return i;
    }
    
    void copy(char data[], char actual[]){
    
        int i;
    
        for(i = 0; i < MAX ; ++i)
        {
            data[i] = actual[i];
        }
    
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    So what excatly are you having problems with? I see there is bit of logic error in your code. Perhaps, when you copy the array from line to out, as you need to append the array to out array. You first need to find out the length of the array out. Or if you dont you find a mechanism to traverse through the array and find the end index and start appending the line array to out. At the movement you do that have that, as a reason you will keep overwritting the out and not producing and array with the user input appended to out.

    The error lies in the function void copy(char data[], char actual[]) function.

    Code:
    void copy(char data[], char actual[])
    {
        int i;
        // for(i = 0; i < MAX ; ++i)
        for(i=0;data[i] != '\0';i++);
        
        for(j=i,i=0;actual[i]!='\0';i++,j++)
            data[j] = actual[i];
        data[j]='\0';
    }
    And also include the null char at the end of out array. Before printing it using printf statment. Otherwise the you will see some random char appearing, when are junk values of the memory, unless you have initialled your array befpre use.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merging two arrays into one
    By Thedon in forum C Programming
    Replies: 10
    Last Post: 10-30-2011, 01:34 PM
  2. Merging Arrays
    By yigster in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2010, 03:16 AM
  3. Merging arrays help
    By key4life in forum C Programming
    Replies: 12
    Last Post: 12-05-2009, 06:46 PM
  4. merging arrays
    By acohrockz21 in forum C Programming
    Replies: 28
    Last Post: 03-09-2008, 02:28 AM
  5. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM