C PROGRAM ON ARRAY
C Program to Find the Number of Elements in an Array
/*
* C Program to Find the Number of Elements in an Array
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int array[] = {15, 50, 34, 20, 10, 79, 100};
int n;
n = sizeof(array);
printf("Size of the given array is %d\n", n/sizeof(int));
return 0;
}
Runtime Test Cases
Size of the given array is 7
C program to Delete an Element from an Array
/*
* C program to delete an element from an array by index or value
*/
#include <stdio.h>
#include <stdlib.h>
int n;
void delete_element_by_index(int arr[], int index)
{
int i;
for (i = index; i < n - 1; i++)
arr[i] = arr[i + 1];
n -= 1;
}
void delete_element_by_value(int arr[], int value)
{
int i;
for (i = 0; i < n; i++)
{
if (arr[i] == value)
{
for (; i < n - 1; i++)
arr[i] = arr[i + 1];
n -= 1;
break;
}
}
}
void print_array(int arr[])
{
int i;
printf("\n[ ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("]\n");
}
void init_array(int arr[])
{
int i;
for (i = 0; i < n; i++)
{
printf("%d: ", i);
scanf("%d", &arr[i]);
}
}
int main(void)
{
int index, value, arr[10], choice;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: \n");
init_array(arr);
printf("You can delete an element by index or value.\n1. Delete by index\n2. Delete by value\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1)
{
printf("Enter the index of the element to be deleted: ");
scanf("%d", &index);
delete_element_by_index(arr, index);
}
else
{
printf("Enter the value of the element to be deleted: ");
scanf("%d", &value);
delete_element_by_value(arr, value);
}
print_array(arr);
}
Runtime Test Cases
Testcase 1: In this case, the index of the element to be deleted is “2”
Enter the size of the array: 4
Enter the elements of the array:
arr[0] = 2
arr[1] = 8
arr[2] = 4
arr[3] = 9
You can delete an element by index or by value.
1. Delete by index
2. Delete by value
Enter your choice: 1
Enter the index of the element to be deleted: 2
[ 2 8 9 ]
Testcase 2: In this case, the value of the element to be deleted is “32”
Enter the size of the array: 6
Enter the elements of the array:
arr[0] = 12
arr[1] = 65
arr[2] = 32
arr[3] = 75
arr[4] = 48
arr[5] = 11
You can delete an element by index or by value.
1. Delete by index
2. Delete by value
Enter your choice: 2
Enter the value of the element to be deleted: 32
[ 12 65 75 48 11 ]
C Program to Cyclically Permute the Elements of an Array
/*
* C program to cyclically permute the elements of an array A.
* i.e. the content of A1 become that of A2. And A2 contains
* that of A3 & so on as An contains A1
*/
#include <stdio.h>
void main ()
{
int i, n, number[30];
printf("Enter the value of the n = ");
scanf("%d", &n);
printf("Enter the numbers\n");
for (i = 0; i < n; ++i)
{
scanf("%d", &number[i]);
}
number[n] = number[0];
for (i = 0; i < n; ++i)
{
number[i] = number[i + 1];
}
printf("Cyclically permuted numbers are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
}
Runtime Test Cases
Enter the value of the n = 4
Enter the numbers
3
40
100
68
Cyclically permuted numbers are given below
40
100
68
3
C Program to Split the Array and Add First Part to the End
/*
* C program to read an array, accept a key & split the array.
* Add the first half to the end of second half.
*/
#include <stdio.h>
void main ()
{
int number[30];
int i, n, a, j;
printf("Enter the value of n\n");
scanf("%d", &n);
printf("enter the numbers\n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
printf("Enter the position of the element to split the array \n");
scanf("%d", &a);
for (i = 0; i < a; ++i)
{
number[n] = number[0];
for (j = 0; j < n; ++j)
{
number[j] = number[j + 1];
}
}
printf("The resultant array is\n");
for (i = 0; i < n; ++i)
{
printf("%d\n", number[i]);
}
}
Runtime Test Cases
Enter the value of n
4
enter the numbers
3
678
345
876
Enter the position of the element to split the array
3
The resultant array is
876
3
678
345
C Program to Implement Variable Length Array
#include <stdio.h>
#include <stdlib.h>
#ifndef VECTOR_H
#define VECTOR_H
#define VECTOR_INIT_CAPACITY 4
#define VECTOR_INIT(vec) vector vec; vector_init(&vec)
#define VECTOR_ADD(vec, item) vector_add(&vec, (void *) item)
#define VECTOR_SET(vec, id, item) vector_set(&vec, id, (void *) item)
#define VECTOR_GET(vec, type, id) (type) vector_get(&vec, id)
#define VECTOR_DELETE(vec, id) vector_delete(&vec, id)
#define VECTOR_TOTAL(vec) vector_total(&vec)
#define VECTOR_FREE(vec) vector_free(&vec)
typedef struct vector {
void **items;
int capacity;
int total;
} vector;
void vector_init(vector *);
int vector_total(vector *);
static void vector_resize(vector *, int);
void vector_add(vector *, void *);
void vector_set(vector *, int, void *);
void *vector_get(vector *, int);
void vector_delete(vector *, int);
void vector_free(vector *);
#endif
void vector_init(vector *v) {
v->capacity = VECTOR_INIT_CAPACITY;
v->total = 0;
v->items = malloc(sizeof(void *) * v->capacity);
}
int vector_total(vector *v) {
return v->total;
}
static void vector_resize(vector *v, int capacity) {
#ifdef DEBUG_ON
printf("vector_resize: %d to %d\n", v->capacity, capacity);
#endif
void **items = realloc(v->items, sizeof(void *) * capacity);
if (items) {
v->items = items;
v->capacity = capacity;
}
}
void vector_add(vector *v, void *item) {
if (v->capacity == v->total)
vector_resize(v, v->capacity * 2);
v->items[v->total++] = item;
}
void vector_set(vector *v, int index, void *item) {
if (index >= 0 && index < v->total)
v->items[index] = item;
}
void *vector_get(vector *v, int index) {
if (index >= 0 && index < v->total)
return v->items[index];
return NULL;
}
void vector_delete(vector *v, int index) {
if (index < 0 || index >= v->total)
return;
v->items[index] = NULL;
int i;
for (i = 0; i < v->total - 1; i++) {
v->items[i] = v->items[i + 1];
v->items[i + 1] = NULL;
}
v->total--;
if (v->total > 0 && v->total == v->capacity / 4)
vector_resize(v, v->capacity / 2);
}
void vector_free(vector *v) {
free(v->items);
}
int main(void) {
int i;
vector v;
vector_init(&v);
vector_add(&v, "Bonjour");
vector_add(&v, "tout");
vector_add(&v, "le");
vector_add(&v, "monde");
for (i = 0; i < vector_total(&v); i++)
printf("%s ", (char *) vector_get(&v, i));
printf("\n");
vector_delete(&v, 3);
vector_delete(&v, 2);
vector_delete(&v, 1);
vector_set(&v, 0, "Hello");
vector_add(&v, "World");
for (i = 0; i < vector_total(&v); i++)
printf("%s ", (char *) vector_get(&v, i));
printf("\n");
vector_free(&v);
}
Output:
$ gcc VariableLengthArray.c
$ ./a.out
Bonjour tout le monde
Hello World