Thread: Merging two arrays into one

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    16

    Merging two arrays into one

    Hi, I am writing a program to read in two sets of values from the user and store them in seperate arrays. Then combine those two arrays into a third array and display them. I think my logic to do this is correct,maybe not!

    Code:
    #include <stdio.h>
    
    int main ()
    
    
    {
    
    
    int a[10],b[10],c[20];
    
    
    int k,i,j;
    
    
    i=0;
    
    
    printf("Please enter first 10 digits: ");
    
    
    	while (i<10)
    	{
    
    
    	scanf("%d", &a[i]);
    
    
    	i++;
    	
    	}
    
    
    printf("Please enter second 10 digits: ");
    
    
    k=0;
    
    
    	while (k<10)
    	{
    
    
    	scanf("c%d",&b[k]);
    
    
    	k++;
    	
    	}
    
    
    j=0;
    
    
    	while (j<10)
    	{
    	c[j]=a[i];
    	
    	j++;
    	}
    
    
    	while (j<20)
    	{
    	
    	c[j]=b[k];
    	
    	j++;
    	}
    
    
    	j=0;
    
    
    	while(j<20)
    	{
    
    
    	printf("%d\n", c[j]);
    
    
    	}
    
    
    return 0;
    
    
    }
    Last edited by Thedon; 10-29-2011 at 09:18 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Seems OK - except for a distinct lack of incrementing j anywhere in the loops.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    I edit my original post is that better?When i run the program after entering the last digit on the first loop it runs into a continious loop

  4. #4
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Please don't edit your original post as it makes the thread void by removing the initial problem that you have solved, no matter how trivial it is.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Sorry i wasnt aware i didnt want to be blocking up the page!!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    What about j in your print loop?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    I was missing a J++!!The program is running as expected the third array prints the first 10 digits but then the first digit of the second array ten times

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    I think you should be resetting your initial values back to 0 before copying them into the 3rd array. Like 'i' and 'k' then incrementing along with j

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    why are you using i and k in j loops? aren't they done after their loops?

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    16
    Hi guys i fixed this last night i was missing a number of things should i post up my code?

  11. #11
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Quote Originally Posted by Thedon View Post
    Hi guys i fixed this last night i was missing a number of things should i post up my code?
    if you still need help, then yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merging Arrays
    By yigster in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2010, 03:16 AM
  2. Merging arrays help
    By key4life in forum C Programming
    Replies: 12
    Last Post: 12-05-2009, 06:46 PM
  3. merging arrays
    By acohrockz21 in forum C Programming
    Replies: 28
    Last Post: 03-09-2008, 02:28 AM
  4. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM
  5. merging arrays using pointers help!!!
    By edshaft in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2001, 07:19 AM