-
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;
}
-
> 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.
-
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];
}
-
what you mean by: just by assigning values out of bounds ?
-
Array a is m elements long. Array b is n elements long.
So neither of them is big enough to hold n + m elements.
-
So what do i do now ?
can you elaborate with the code plz
-
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 ?
-
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;
}
-
thanks , that's way better now , I understand perfectly now
-
on my way to learning C , awesome John.c , you help me so much!