Thread: Inserting into array

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    14

    Inserting into array

    This is my sample code , however i seem to get an odd answer , can someone explain why i get my ans as :

    This is the Terminal output , LOOK BELOW FOR THE CODE

    Enter Element: 1 1

    Enter Size of Array:1 4


    Enter 4 Elements into the Array:


    Enter Element: 1 1


    Enter Element: 2 2


    Enter Element: 3 3


    Enter Element: 4 4


    Enter Size of Array:2 4


    Enter 4 Elements into the Array:


    Enter Element: 1 1


    Enter Element: 2 2


    Enter Element: 3 3


    Enter Element: 4 4


    Insert Array:1 into Array:2 at Position: 1


    Size of Array :2 is 4
    The New array Size 8 :


    1
    1
    2
    3
    4
    2
    3
    2. <why 2 here IT SHOULD BE 4 ?????

    code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      int n, m, key, loc, i;
    
      system("clear");
    
      //const int n=10;//n =sizeof(a)/sizeof(int);
    
      printf("\nEnter Size of Array:1\t");
      scanf("%d", &m);
      int a[m];
      printf("\nEnter %d Elements into the Array:\n", m);
      for (i = 0; i < m; i++) {
        printf("\nEnter Element: %d\t", i + 1);
        scanf("%d", &a);
      }
    
      printf("\nEnter Size of Array:2\t");
      scanf("%d", &n);
      int b[n];
      printf("\nEnter %d Elements into the Array:\n", n);
      for (i = 0; i < n; i++) {
        printf("\nEnter Element: %d\t", i + 1);
        scanf("%d", &b);
      }
    
      printf("\nInsert Array:1 into Array:2 at Position:\t ");
      scanf("%d", &loc);
    
      printf("\nSize of Array :2 is %d", n);
    
      //shifting
      for (i = m - 1; i >= loc; i--) {
        a[i + n] = a;
      }
    
      for (i = 0; i < n; i++)       //insert n elements from 2nd array
      {
        a[loc + i] = b;
      }
    
      n = m + n;
      printf("\nThe New array Size %d :", n);
    
      for (i = 0; i < n; i++) {
        printf("\n%d", a);
      }
    
      printf("\n");
      return 0;
    }
    Last edited by Salem; 4 Weeks Ago at 01:42 PM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,114
    > why 2 here IT SHOULD BE 4 ?????
    Because an array doesn't get bigger just by assigning values out of bounds.

    Code:
      //shifting
      for (i = m - 1; i >= loc; i--) {
        a[i + n] = a;
      }
    You need to make another array, which is the new total size.
    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
    Sep 2019
    Posts
    14
    i don't get it , but what i'm doing is shifting the values by n number then inserting the second array in the space , so what is the issue here ?
    //shifting
    for(i=m-1;i>=loc;i--)
    {
    a[i+n] = a[i];
    }

    for(i=0;i<n;i++) //insert n elements from 2nd array
    {
    a[loc+i]=b[i];
    }
    Last edited by nevx; 4 Weeks Ago at 02:18 PM.

  4. #4
    Registered User
    Join Date
    Sep 2019
    Posts
    14
    what you mean by: just by assigning values out of bounds ?

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    951
    Array a is m elements long. Array b is n elements long.
    So neither of them is big enough to hold n + m elements.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  6. #6
    Registered User
    Join Date
    Sep 2019
    Posts
    14
    So what do i do now ?
    can you elaborate with the code plz

  7. #7
    Registered User
    Join Date
    Sep 2019
    Posts
    14








    Code:
    n =m+n;
    
    int new[n];
    
    printf("\nThe New array Size %d :",n);
    
    for(i=0;i<n;i++)
    {
       new[i] =a[i];
        printf("\n%d",new[i]);
    }


    Ok, got it now , but i still don't get why i have to use another array ?

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    951
    You created the arrays to be a certain size.
    That is the size they are.
    They do not magically change their size just because you try to access memory outside the array bounds.

    BTW, I didn't say you had to make a new array. You just need to ensure that the array you insert into has enough space.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
        printf("\nEnter Size of Array:1\t");
        int m;
        scanf("%d", &m);
        int a[m];
     
        printf("\nEnter %d Elements into the Array:\n", m);
        for (int i = 0; i < m; i++)
        {
            printf("\nEnter Element: %d\t", i + 1);
            scanf("%d", &a[i]);
        }
        
        printf("\nEnter Size of Array:2\t");
        int n;
        scanf("%d", &n);
        int b[n + m];  // enough size for both
     
        // but we're only using the first n at first
        printf("\nEnter %d Elements into the Array:\n", n);
        for(int i = 0; i < n; i++)
        {
            printf("\nEnter Element: %d\t", i + 1);
            scanf("%d", &b[i]);
        }
     
        printf("\nInsert Array:1 into Array:2 at Position:\t ");
        int loc;
        scanf("%d", &loc);
     
        // ... you should ensure that loc is within range ...
     
        for (int i = n - 1; i >= loc; i--) 
            b[i + m] = b[i];
     
        for (int i = 0; i < m; i++) //insert m elements from 1st array
          b[loc + i] = a[i];
     
        n += m;
        printf("\nThe New array Size %d:\n", n);
     
        for (int i = 0; i < n; i++)
            printf("%d\n", b[i]);
     
        return 0;
    }
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  9. #9
    Registered User
    Join Date
    Sep 2019
    Posts
    14
    thanks , that's way better now , I understand perfectly now

  10. #10
    Registered User
    Join Date
    Sep 2019
    Posts
    14
    on my way to learning C , awesome John.c , you help me so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting bytes into float array
    By samtal in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2011, 03:45 AM
  2. inserting values in an array
    By fairyjerry14 in forum C Programming
    Replies: 1
    Last Post: 10-12-2007, 01:15 AM
  3. inserting an element into an array
    By galmca in forum C Programming
    Replies: 14
    Last Post: 01-09-2005, 06:34 AM
  4. Inserting elements into array
    By sean in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2003, 04:56 PM
  5. Inserting words into an array
    By Rizage in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2002, 03:29 AM

Tags for this Thread