Thread: Inserting into array

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    15

    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; 07-09-2020 at 01:42 PM. Reason: Removed crayola

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