Thread: Merge two arrays together

  1. #1
    Fedryan
    Guest

    Merge two arrays together

    Hi :-)

    I need to write a function that will merge the content of two sorted arrays of type double values. The function should not assume that both its input parameter arrays are the same length.

    Here is my program so far - I know there is alot of errors and mistakes, but I would be happy if somebody could point some of them out for me:

    Code:
    #include<stdio.h>
    
    void merge(int firstArray[],int secondArray[],int size)
    {
    int mergedArray[size],i=0,j=0,k=0;
      while(i<size||j<size)
    {
      if(i==size)
    {
      while(j<size)
    {
      mergedArray[k]=secondArray[j];
      j++;
    }
    }
      if(j==size)
    {
      while(i<size)
    {
      mergedArray[k]=secondArray[i];
      i++;
    }
    }
    
    
      if(firstArray[i]>secondArray[j])
    {
      mergedArray[k]=secondArray[j];
      j++;
    }
      else
    {
      mergedArray[k]=firstArray[i];
      i++;
    }
      k++;
    }
    }
    
    int main(void){
    
      int firstArray[sizeFirst], secondArray[sizeSecond], mergedArray[size], i, j, k, size, sizeFirst, sizeSecond;
      double firstArray[a], secondArray[b];
    
      printf("Enter the size of the first array:\n");
      scanf("%d", &sizeFirst);
    
      printf("Enter %d numbers seperated by blanks or <return>s first array\n", sizeFirst);
      for (i=0; i<size; ++i)
        scanf("%lf", &firstArray[a]);
    
      printf("Enter the size of the second array:\n");
      scanf("%d", &sizeSecond);
    
      printf("Enter %d numbers seperated by blanks or <return>s second array\n", sizeSecond);
      for (i=0; i<size; ++i)
        scanf("%lf", &secondArray[b]);
    
        merge (firstArray, secondArray, mergedArray);
    
        printf("The merged array is: %d\n", mergedArray);
    
     return (0);
    }
    /Fedryan

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you are not assuming the two sub-arrays are not the same size, you will need to pass two sizes into your function.

    I really hope you have real indentation in your real source code file, as I find it impossible to tell without counting braces whether k++ happens inside your run-out-of-data loops at the top. If it doesn't, you'll be overwriting a bunch of data points.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Fedryan View Post
    merge the content of two sorted arrays of type double values.
    but the merge function is merging arrays of type integer. Also main() is declaring the arrays before the sizes are defined, and for "classic" C, the size of the arrays should be input, and the arrays allocated using malloc(), and later released using free().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sub-arrays for merge Sort
    By abood1190 in forum C Programming
    Replies: 3
    Last Post: 05-15-2013, 05:35 PM
  2. Can you merge arrays?
    By SeriousTyro in forum C Programming
    Replies: 14
    Last Post: 09-21-2009, 10:38 AM
  3. how to merge two arrays recursevly..
    By transgalactic2 in forum C Programming
    Replies: 117
    Last Post: 01-11-2009, 04:47 PM
  4. Merge sort (sorted arrays)
    By myle in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2007, 02:48 AM
  5. How to merge 2 arrays?
    By planet_abhi in forum C Programming
    Replies: 3
    Last Post: 02-16-2003, 12:23 AM

Tags for this Thread