Thread: Merging arrays help

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    13

    Merging arrays help

    Hello all. I'm having trouble merging arrays here. So first I allow the user to enter the size of the array, followed by the elements. Then I use a bubble sort to get them in ascending order. And I do this for two sets of values. But I can't seem to get them to merge. Please look at my code and help if you can ...

    Code:
    #include<stdio.h>
    #include<conio.h>
    #define SIZE 4
    
    int bubble_sort(int array[],int w);
    int merge(int array1[], int w1, int array2[], int w2);
    
    int main(void)
    {
        /*Accept values for Values1*/
        
        int array1[10],val_1,i,x;
        
        printf("Please enter the array size of Values1.\n");
        scanf("%d",&i);
        
        printf("Please enter the elements of Values1.\n");
        for(x = 0; x < i; x++)
        {
              scanf("%d",&val_1);
              array1[x] = val_1;
        }
        
        
        bubble_sort(array1,i);
        
    
        /*Accept values for Values2*/
        
        int array2[10],val_2,y,z;
        
        printf("Please enter the array size of Values2.\n");
        scanf("%d",&y);
        
        printf("Please enter the elements of Values2.\n");
        for(z = 0; z < y; z++)
        {
              scanf("%d",&val_2);
              array2[z] = val_2;
        }
        bubble_sort(array2,y);
        
        printf("The sorted elements of Values1 are:");
        for(x = 0; x < i; x++)
        {
              printf("%d",array1[x]);
        }
        
        printf("\n");
        printf("The sorted elements of Values2 are:");
        for(z = 0; z < y; z++)
        {
              printf("%d",array2[z]);
        }
        
        merge(array1,i,array2,y);
        
        getch();
        
        
        
    }
    
    int bubble_sort(int array[],int w)
    {
        int c1,c2,hold;
        for (c1 = 1; c1 < w; c1++)
        {
            for (c2 = 0; c2 < w-1; c2 ++) 
            {
                 if (array[c2] > array[c2+1])
                 {
                          hold = array[c2];
                          array[c2] = array[c2+1];
                          array[c2+1] = hold;
                 }
                 }
        }
    }
    
    int merge(int array1[], int w1, int array2[], int w2)
    {
        int f,c, array3[20];
        
        for(f = 0; f < (w1+w2) ; f++)
        {
                
              if(array1[f] < array2[f])
              {
                 
                 array3[f] = array1[f]; 
                 array3[f+1] = array2[f];                     
              }
              else 
              {
                 array3[f] = array2[f];
                 array3[f+1] = array1[f];
              }
        }    
        
        
            
    
        printf("The final values are:\n");
        for(f = 0; f < (w1+w2); f++)
        {
              printf("%d ",array3[f]); 
        }
    }
    I keep getting hexidecimal digits for the output of the final 3 numbers. I think its because the values for each half to be merged are less than the counter for the loop, and so when it loops they have no value in that array element.
    Last edited by key4life; 12-04-2009 at 09:43 PM. Reason: Adding extra info

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. merging arrays
    By acohrockz21 in forum C Programming
    Replies: 28
    Last Post: 03-09-2008, 02:28 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. merging arrays using pointers help!!!
    By edshaft in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2001, 07:19 AM

Tags for this Thread