Array
Array
Array
printf("[");
for(i=0; i<s-1; i++)
{
printf("%d, ", a[i]);
}
for(i=s-1; i<s; i++)
{
printf("%d", a[i]);
}
printf("]");
return 0;
}
Output:
Code 2:
#include <stdio.h>
int Search(int a[], int n, int searched)
{
for (int i = 0; i < n; i++)
{
if (a[i] == searched)
{
return i;
}
}
return -1;
}
int main()
{
int array[]={1,2,3,4,5,6,7,8,9,10};
printf("The array is {1,2,3,4,5,6,7,8,9,10}\n");
int size = sizeof(array) / sizeof(array[0]);
int forsearch = 8;
printf("From the array We are searching 8\n");
if (result != -1)
{
printf("Target %d found at index %d\n", forsearch, result);
}
else
{
printf("Target %d not found in the array\n", forsearch);
}
return 0;
}
Output:
Code 3:
#include <stdio.h>
int insert(int a[],int *size,int position,int element)
{
for (int i=*size-1;i>=position;i--)
{
a[i+1]=a[i];
}
a[position]=element;
(*size)++;
}
int main()
{
int maxSize = 10;
int array[maxSize];
int size = 0;
printf("Enter the size of the array (max %d): ", maxSize);
scanf("%d", &size);
printf("Enter %d elements for the array:\n", size);
for (int i=0;i<size;i++)
{
scanf("%d", &array[i]);
}
printf("Original Array: ");
for (int i=0;i<size;i++)
{
printf("%d ",array[i]);
}
int newPosition, newElement;
printf("\nEnter the position to insert(element no. starts from 0): ");
scanf("%d", &newPosition);
printf("Enter the element to insert: ");
scanf("%d", &newElement);
if (newPosition>=0 && newPosition<=size)
{
insert(array,&size,newPosition,newElement);
printf("Array after insertion at position %d: ", newPosition);
for (int i=0;i<size;i++)
{
printf("%d ",array[i]);
}
}
else
{
printf("Invalid position for insertion.\n");
}
return 0;
}
Output:
Code 4:
#include <stdio.h>
Output:
2. Implement programs to find the sum, average, maximum, and
minimum values in an array.
Code:
#include <stdio.h>
#include <limits.h>
int main()
{
int maxn = 10;
int array[maxn];
int n = 0;
printf("Enter the size of the array (max %d): ", maxn);
scanf("%d", &n);
printf("Enter %d elements for the array:\n", n);
for (int i=0;i<n;i++)
scanf("%d", &array[i]);
int sum=0;
for (int i=0;i<n;i++)
{
sum=sum+array[i];
}
printf("Sum: %d\n",sum);
int add = 0;
for (int i=0;i<n;i++)
{
add=add+array[i];
}
printf("Average: %.2f\n",(double)add / n);
int max = INT_MIN;
for (int i=0;i<n;i++)
{
if (array[i] > max)
max = array[i];
}
printf("Maximum: %d\n",max);
int min = INT_MAX;
for (int i = 0; i < n; i++)
{
if (array[i] < min)
min = array[i];
}
printf("Minimum: %d\n",min);
return 0;
}
Output:
// Matrix subtraction
printf("\nMatrix Subtraction (a - b):\n");
for (int r = 0; r < i; r++)
{
for (int s = 0; s < j; s++)
{
result[r][s] = a[r][s] - b[r][s];
printf("%d\t", result[r][s]);
}
printf("\n");
}
return 0;
}
Output:
4.Implement a program to merge two sorted arrays into a single sorted
arrays.
Code:
#include <stdio.h>
int mergeSortedArrays(int a[],int n1,int b[],int n2,int result[])
{
int i=0,j=0,k=0;
while (i<n1 && j<n2)
{
if (a[i]<=b[j])
{
result[k]=a[i];
i++;
}
else
{
result[k]=b[j];
j++;
}
k++;
}
while (i<n1)
{
result[k]=a[i];
i++;
k++;
}
while (j<n2)
{
result[k]=b[j];
j++;
k++;
}
}
int main()
{
int n1,n2;
printf("Enter the size of the first array: ");
scanf("%d", &n1);
int a[n1];
printf("Enter the elements of the first array in sorted order:\n");
for (int i=0;i<n1;i++)
{
scanf("%d", &a[i]);
}
printf("Enter the size of the second array: ");
scanf("%d", &n2);
int b[n2];
printf("Enter the elements of the second array in sorted order:\n");
for (int i=0;i<n2;i++)
{
scanf("%d", &b[i]);
}
int result[n1 + n2];
mergeSortedArrays(a, n1, b, n2, result);
printf("Merged Sorted Array:\n");
for (int i=0;i<n1+n2;i++)
{
printf("%d ", result[i]);
}
return 0
Output: