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;
}