int a[N], arr1[N], arr2[N], i, pos, k1 = 0, k2 = 0;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Enter position to split the array in to Two\n");
scanf("%d", &pos);
for(i = 0; i < N; i++)
{
if(i < pos)
arr1[k1++] = a[i];
else
arr2[k2++] = a[i];
}
printf("\nElements of First Array -> arr1[%d]\n", k1);
for(i = 0; i < k1; i++)
printf("%d\n", arr1[i]);
printf("\nElements of Second Array -> arr2[%d]\n", k2);
for(i = 0; i < k2; i++)
printf("%d\n", arr2[i]);
printf("\n");
return 0;
}
#include<stdio.h>
#define N 10
int main()
{
int a[N], arr1[N], arr2[N], i, pos, k1 = 0, k2 = 0;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Enter position to split the array in to Two\n");
scanf("%d", &pos);
for(i = 0; i < N; i++)
{
if(i < pos)
arr1[k1++] = a[i];
else
arr2[k2++] = a[i];
}
printf("\nElements of First Array -> arr1[%d]\n", k1);
for(i = 0; i < k1; i++)
printf("%d\n", arr1[i]);
printf("\nElements of Second Array -> arr2[%d]\n", k2);
for(i = 0; i < k2; i++)
printf("%d\n", arr2[i]);
printf("\n");
return 0;
}
Output: Enter 10 integer numbers 1 2 3 4 5 6 7 8 9 1 Enter position to split the array in to Two 3
Elements of First Array -> arr1[3] 1 2 3
Elements of Second Array -> arr2[7] 4 5 6 7 8 9 1
Logic To Divide/Split An Array Into Two At Specified Position
We accept N integer numbers from the user and store it inside array variable a[N]. Now we ask the user to input the position at which we need to split the array and create two separate arrays. Next, we iterate through array elements of a, using for loop(loop counter variable i is initialized to 0 and for loop executes until i < N and for each iteration of for loop i value increments by 1) and transfer all the elements of array a to array variable arr1 until i is less than user input position. Once i is greater than or equal to user input position, we transfer elements of a to array variable arr2.
Note: We’ve initialized k1 and k2 to 0, as index starts from 0. Whenever we push / insert values inside arr1 and arr2, we increment the index value of k1 and k2 respectively.
Note: We’re assigning same size to all 3 array variables, because if user enters all odd numbers, then we need to transfer all the elements to array variable odd, for that we need the size of array variable odd to be same as that of the original array. Similarly, if user enters all even numbers we’ll need to have the same size for array variable even.
Logic To Split Even and Odd Elements of An Array Into Two Arrays
First we accept all the elements of an array from the user. Next we iterate through the array elements one by one using a for loop. Inside this for loop we check each individual element, if its even or odd. If a number is perfectly divisible by 2, then its even number else its odd number. If the fetched/selected number is even number, then we copy that number into array variable even, else we copy the element into array variable odd.
Next we use for loop to display the elements of array even and odd, which will have the even and odd elements of the original array.
In above source code we check if the number input by the user is even or odd immediately after the user enters a number. We don’t wait until user inputs all the elements of the array. This way we eleminate the need for a separate for loop to check even and odd elements in the original array and then assigning it to array variables even and odd. This is the best approach, which reduces resource usage and has faster execution time.
Lets write a C program to count number of positive, negative and zeros in an Array.
Logic
If input number is greater than 0, then its positive number. If input number is less than 0, then its negative. If its neither greater than 0, nor less than 0, then the input number must be 0.
Logic To Find Number of Positive, Negative and Zeros In An Array
First we initialize 0 to variables p, n and z – which represents positive, negative and zero. We accept 10 integer numbers from the user. Next we iterate through the array using for loop and check if the fetched array element is greater than 0, which means its a positive number, so we increment the value of variable p by one. If fetched array element is less than 0, which means its a negative number, so we increment the value of variable n by one. If both the cases fail, then the number must be zero, so we increment the value of z by 1.
After the completion of for loop execution, variables p, n and z will have the number of positive, negative and zeros present in the array.
In above source code, once the user inputs a number, we check if the input number is greater than 0 or less than zero or is equal to zero, and increment the values of variable p, n and z accordingly.
This is the best solution for this problem statement, as we only write for loop once and we calculate the result as and when user inputs array elements. Less overhead and more efficient.
printf("Enter %d integer numbers, for first array\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Enter %d integer numbers, for second array\n", N);
for(i = 0; i < N; i++)
scanf("%d", &b[i]);
printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N, N, M);
for(i = 0; i < N; i++)
c[index++] = a[i];
for(i = 0; i < N; i++)
c[index++] = b[i];
printf("\nElements of c[%d] is ..\n", M);
for(i = 0; i < M; i++)
printf("%d\n", c[i]);
return 0;
}
#include<stdio.h>
#define N 5
#define M (N * 2)
int main()
{
int a[N], b[N], c[M], i, index = 0;
printf("Enter %d integer numbers, for first array\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Enter %d integer numbers, for second array\n", N);
for(i = 0; i < N; i++)
scanf("%d", &b[i]);
printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N, N, M);
for(i = 0; i < N; i++)
c[index++] = a[i];
for(i = 0; i < N; i++)
c[index++] = b[i];
printf("\nElements of c[%d] is ..\n", M);
for(i = 0; i < M; i++)
printf("%d\n", c[i]);
return 0;
}
Output: Enter 5 integer numbers, for first array 0 9 8 7 6 Enter 5 integer numbers, for second array 5 4 3 2 1
Merging a[5] and b[5] to form c[10] ..
Elements of c[10] is .. 0 9 8 7 6 5 4 3 2 1
Logic To Concatenate Two Array To Form Third Array
Here size of array a and b are same, so the for loops are same to accept elements of array a and b. We initialize variable index to 0. We write a for loop and iterate from 0 to 4, and copy the individual elements of array a to array c. Here index of array a and c varies from 0 to 4.
In the second for loop again we iterate the for loop from 0 to 4. This time value of variable index varies from 5 to 9, where as value of index of variable b varies from 0 to 4. So the individual elements of array b are copied to array c from index 5 to 9.
printf("Enter %d integer numbers, for first array\n", N1);
for(i = 0; i < N1; i++)
scanf("%d", &a[i]);
printf("Enter %d integer numbers, for second array\n", N2);
for(i = 0; i < N2; i++)
scanf("%d", &b[i]);
printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N1, N2, M);
for(i = 0; i < N1; i++)
c[index++] = a[i];
for(i = 0; i < N2; i++)
c[index++] = b[i];
printf("\nElements of c[%d] is ..\n", M);
for(i = 0; i < M; i++)
printf("%d\n", c[i]);
return 0;
}
#include<stdio.h>
#define N1 5
#define N2 6
#define M (N1 + N2)
int main()
{
int a[N1], b[N2], c[M], i, index = 0;
printf("Enter %d integer numbers, for first array\n", N1);
for(i = 0; i < N1; i++)
scanf("%d", &a[i]);
printf("Enter %d integer numbers, for second array\n", N2);
for(i = 0; i < N2; i++)
scanf("%d", &b[i]);
printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N1, N2, M);
for(i = 0; i < N1; i++)
c[index++] = a[i];
for(i = 0; i < N2; i++)
c[index++] = b[i];
printf("\nElements of c[%d] is ..\n", M);
for(i = 0; i < M; i++)
printf("%d\n", c[i]);
return 0;
}
Output: Enter 5 integer numbers, for first array 0 1 2 3 4 Enter 6 integer numbers, for second array 5 6 7 8 9 10
Merging a[5] and b[6] to form c[11] ..
Elements of c[11] is .. 0 1 2 3 4 5 6 7 8 9 10
Here the logic to concatenate array elements is same, but only difference is the size of array variable a and b. So we need to be careful while writing conditions in for loop.
printf("We have %d no of elements in array\n", count);
return 0;
}
#include<stdio.h>
int main()
{
int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};
int count = sizeof(a) / sizeof(int);
printf("We have %d no of elements in array\n", count);
return 0;
}
Output: We have 25 no of elements in array.
We know that array variable name contains base address or the address of first array element. But sizeof(array_name) gives back the number of bytes occupied by the entire array. Now divide this value by the memory occupied by 1 element – this can be determined either by using sizeof() on the data type(as shown in above source code) or any of the element in the array(as shown in below source code).
printf("We have %d no of elements in array\n", count);
return 0;
}
#include<stdio.h>
int main()
{
int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};
int count = sizeof(a) / sizeof(a[0]);
printf("We have %d no of elements in array\n", count);
return 0;
}
Output: We have 25 no of elements in array.
We assume that the array will at least have 1 element, so we find the number of bytes occupied by it. This is the best approach because, this way we’ll not be compelled to change the data type passed to sizeof() operator whenever we need to check the number of elements present in a array of different data type.
printf("We have %d no of elements in array.\n", SIZE(a));
return 0;
}
#include<stdio.h>
#define SIZE(a) ( sizeof(a) / sizeof(a[0]) )
int main()
{
int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};
printf("We have %d no of elements in array.\n", SIZE(a));
return 0;
}
Output: We have 25 no of elements in array.
Here we pass the array variable name to macro SIZE(). Again the macro expansion will be replacing the macro template before passing the source code to the compiler.
Output: Integer variable a[25]. Character variable ch[6]. Double variable dbl[7].
Here we find the number of elements present in array variable a and ch using the same macro SIZE.
Where Is It useful?
Whenever we want to iterate through individual elements of the array and we’ve no idea whats the size of the array or whats the largest designator of that array. In such cases we can’t write condition inside loop.
Using above formula we could easily find the number of elements present in the array and make use of the result in our for loop condition:
Note: Do not write i < SIZE(a) condition directly inside for loop. This would create unnecessory overhead, as for each iteration SIZE(a) [which will be replaced by ( sizeof(a) / sizeof(a[0]) ) while preprocessing] has to calculate the size of the array. Instead calculate the size of array once and store it inside a variable and then use that variable in your for loop as shown in above source code.