0% found this document useful (0 votes)
4 views13 pages

C Array Operations and Examples

The document outlines an experiment focused on implementing arrays in C programming, detailing objectives such as declaring, displaying, inserting, deleting elements, and finding statistical values. It includes multiple code examples demonstrating these operations, such as inserting elements at various positions, deleting elements, calculating the mean, and identifying duplicates. The document serves as a practical guide for understanding and manipulating arrays in C.

Uploaded by

billyprasad049
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

C Array Operations and Examples

The document outlines an experiment focused on implementing arrays in C programming, detailing objectives such as declaring, displaying, inserting, deleting elements, and finding statistical values. It includes multiple code examples demonstrating these operations, such as inserting elements at various positions, deleting elements, calculating the mean, and identifying duplicates. The document serves as a practical guide for understanding and manipulating arrays in C.

Uploaded by

billyprasad049
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXPERIMENT - 1

IMPLEMENTING ARRAYS
OBJECTIVE :
To work with arrays by performing different operations such as declaring and displaying
elements, inserting and deleting elements at different positions, finding the mean,
identifying
smallest and largest values, printing duplicate values, and reversing the entire array.
THEORY :
In C language, an array is a fixed-size collection of elements of the same data type
stored in
continuous memory locations. We can store multiple values like int, char, float, etc. in
one array.
Each element is accessed using an index number that starts from 0. To refer to an
element, the
array name is followed by its index inside square brackets. Arrays help to manage large
sets of
data using a single variable name.
1) Write a code to declare and display the array elements.
#include <stdio.h>
int main() {
int numbers[100], count;
printf("Enter total elements: ");
scanf("%d", &count);
printf("Enter %d elements: ", count);
for(int i=0;i<count;i++){
scanf("%d", &numbers[i]);
}
printf("Array elements are: ");
for(int i=0;i<count;i++){
printf("%d ", numbers[i]);
}
return 0;
}
OUTPUT :
Enter total elements: 5
Enter 5 elements: 10 20 30 40 50
Array elements are: 10 20 30 40 50
2) Write a program to insert element in an array at first, last and any position.
#include <stdio.h>

int main() {
int n = 10;
int arr[n];

for (int i = 0; i < n; i++) {


printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}

int num;
printf("Enter the number to insert: ");
scanf("%d", &num);

for (int i = n - 1; i >= 0; i--) {


arr[i + 1] = arr[i];
}

arr[0] = num;

printf("Array after insertion:\n");


for (int i = 0; i < n + 1; i++) {
printf("%d ", arr[i]);
}

printf("\n");

return 0;
}

OUTPUT
Enter element 1: 9
Enter element 2: 8
Enter element 3: 7
Enter element 4: 6
Enter element 5: 5
Enter element 6: 4
Enter element 7: 3
Enter element 8: 2
Enter element 9: 1
Enter element 10: 0
Enter the number to insert: 5
Array after insertion:
59876543210

2b.
#include <stdio.h>

int main() {
int n = 5;
int arr[n + 1];

for (int i = 0; i < n; i++) {


printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}

int num;
printf("\nEnter the number to insert: ");
scanf("%d", &num);

for (int i = n; i >= 0; i--) {


arr[i + 1] = arr[i];
}

arr[0] = num;
printf("Array after insertion:\n");
for (int i = 0; i < n + 1; i++) {
printf("%d ", arr[i]);
}

printf("\n");

return 0;
}
OUTPUT
Enter element 1: 9
Enter element 2:
8
Enter element 3: 7
Enter element 4: 6
Enter element 5: 5

Enter the number to insert: 88


Array after insertion:
88 9 8 7 6 5

=== Code Execution Successful ===

2C.
#include <stdio.h>

int main() {
int n = 5;
int arr[n + 1];

for (int i = 0; i < n; i++) {


printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}

int pos, num;


printf("\nEnter the position to insert (1 to %d): ", n + 1);
scanf("%d", &pos);

if (pos < 1 || pos > n + 1) {


printf("Invalid position\n");
return 1;
}

printf("Enter the number to insert: ");


scanf("%d", &num);

for (int i = n - 1; i >= pos - 1; i--) {


arr[i + 1] = arr[i];
}

arr[pos - 1] = num;

n = n + 1;

printf("Array after insertion:\n");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

printf("\n");

return 0;
}
OUTPUT
Enter element 1: 9
Enter element 2: 8
Enter element 3: 7
Enter element 4: 6
Enter element 5: 5

Enter the position to insert (1 to 6): 4


Enter the number to insert: 2
Array after insertion:
987265

=== Code Execution Successful ===

1.​ Write a program to delete the element from first, end and at any position from an Array.
3A
#include <stdio.h>
#define MAX_SIZE 100

int main() {
int arr[MAX_SIZE];
int n;
int i;

printf("Enter the number of elements: ");


scanf("%d", &n);

if (n > MAX_SIZE || n <= 0) {


printf("Invalid size.\n");
return 1;
}

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

for (i = 0; i < n - 1; i++) {


arr[i] = arr[i + 1];
}

n--;

printf("Array after deletion:\n");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}
OUTPUT
Enter the number of elements: 3
Enter 3 elements:
2
5
4
Array after deletion:
54
=== Code Execution Successful ===
3B. DELETING FROM END
#include <stdio.h>

int main() {
int n = 5;
int arr[n];

for (int i = 0; i < n; i++) {


printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}

printf("Array before deletion:\n");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

printf("Array after deletion:\n");


for (int i = 0; i < n - 1; i++) {
printf("%d ", arr[i]);
}

printf("\n");

return 0;
}
OUTPUT
Enter element 1: 4
Enter element 2: 4
Enter element 3: 5
Enter element 4: 8
Enter element 5: 21
Array before deletion:
4 4 5 8 21
Array after deletion:
4458

=== Code Execution Successful ===


3C DELETING FROM ANY POSITION
#include <stdio.h>
int main() {
int n = 5;
int arr[n];

for (int i = 0; i < n; i++) {


printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}

int pos;
printf("Enter the position to delete (1 to %d): ", n);
scanf("%d", &pos);

if (pos < 1 || pos > n) {


printf("Invalid position\n");
return 1;
}

for (int i = pos - 1; i < n - 1; i++) {


arr[i] = arr[i + 1];
}

n = n - 1;

printf("Array after deletion:\n");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

printf("\n");

return 0;
}
OUTPUT
Enter element 1: 8
Enter element 2: 9
Enter element 3: 6
Enter element 4: 5
Enter element 5: 4
Enter the position to delete (1 to 5): 2
Array after deletion:
8654
=== Code Execution Successful ===5

4. Write a program to find the mean of all the elements of an


array.
>> #include <stdio.h>
void MeanOfArray(int arr[],int n){
float result=0;
float mean;
for(int i=0;i<n;i++){
result=result+arr[i];
mean=(result/n);}
printf("Mean of all the elements of array is : ");
printf("%f",mean); }
int main() {
int arr[100],n;
printf("enter the number of element in array : ");
scanf("%d",&n);
printf("enter %d elements of array : ");
for(int i=0;i<n;i++){
scanf("%d",&arr[i]); }
MeanOfArray(arr,n);
return 0;}

OUTPUT : enter the number of element in array : 6


enter 6 elements of array : 1 2 3 4 5 6
Mean of all the elements of array is : 3.500000

7. Write a program to find and display the duplicate elements in an array.


OUTPUT

8. Write a program to reverse an array


OUTPUT

You might also like