Array

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

AIM: Arrays

1.Write programs to manipulate arrays(sorting,searching,insertion,deletion).


Code 1:
#include<stdio.h>
int main()
{
int n,i,s,j,r,k,count;
printf("Enter the size of array: ");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
printf("Enter the %d elements value of array: ",i);
scanf("%d",&a[i]);
}
printf("Your given array is:-");
printf("[");
for(s=0;s<i-1;s++)
{
printf("%d, ",a[s]);
}
for(s=i-1;s<i;s++)
{
printf("%d",a[s]);
}
printf("]");
for(r=0, count; r<=s; r++)
{
if(count!=0)
{
for(int temp, count=0, k=0; k<=s-1; k++)
{
if (a[k] >= a[k+1])
{
temp=a[k];
a[k]=a[k+1];
a[k+1]=temp;
count=1;
}
}
}
else
{
break;
}
}
printf("\n");

printf("New 1d array is: \n");

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");

int result = Search(array, size, forsearch);

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>

void deleteElement(int a[], int *size, int position) {


if (position<0||position>=*size)
{
printf("Invalid position for deletion.\n");
return;
}
printf("Deleted value:%d\n",a[position]);
for (int i=position;i<*size-1;i++)
{
a[i]=a[i+1];
}
(*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 deletePosition;
printf("\nEnter the position to delete(position is from 0): ");
scanf("%d", &deletePosition);
deleteElement(array,&size,deletePosition);
printf("Array after deletion at position %d: ", deletePosition);
for (int i=0;i<size;i++)
{
printf("%d ", array[i]);
}
return 0;
}

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:

3.Practice using multidimensional arrays for matrix operations.


Code:
#include<stdio.h>
int main()
{
printf("For Matrix Operation of 2 matrixes....\n");
printf("For first Matrix:\n");
int i,j,r,s;
printf("Enter the maximum rows you want in matrix: ");
scanf("%d",&i);
printf("Enter the maximum columns you want in matrix: ");
scanf("%d",&j);
int a[i][j];
for( r=0;r<i;r++)
{
for( s=0;s<j;s++)
{
printf("Enter the a[%d,%d] element: ",r,s);
scanf("%d",&a[r][s]);
}
}
printf("\nEntered 1st Matrix:\n");
for ( r = 0; r < i; r++)
{
for ( s = 0; s < j; s++)
{
printf("%d\t", a[r][s]);
}
printf("\n");
}
printf("For secoond Matrix:\n");
printf("Enter the maximum rows you want in matrix: ");
scanf("%d",&i);
printf("Enter the maximum columns you want in matrix: ");
scanf("%d",&j);
int b[i][j];
for( r=0;r<i;r++)
{
for( s=0;s<j;s++)
{
printf("Enter the a[%d,%d] element: ",r,s);
scanf("%d",&b[r][s]);
}
}
printf("\nEntered 2nd Matrix:\n");
for ( r = 0; r < i; r++)
{
for ( s = 0; s < j; s++)
{
printf("%d\t", b[r][s]);
}
printf("\n");
}
int result[r][s];
printf("\nMatrix Addition (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");
}

// 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:

5.Write a program to find the second largest element in an array.


Code:
#include <stdio.h>
int main()
{
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
if (n<=1)
{
printf("Array should have at least two elements.\n");
return 1;
}
int a[n];
printf("Enter %d elements of the array:\n", n);
for (int i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("[");
for(int i=0;i<n-1;i++)
{
printf("%d, ",a[i]);
}
for(int i=n-1;i<n;i++)
{
printf("%d",a[i]);
}
printf("]");
printf("\n");
// Find the second largest element
int largest = a[0];
int secondLargest = a[1];
if (largest < secondLargest)
{
int temp = largest;
largest = secondLargest;
secondLargest = temp;
}
for (int i = 2; i < n; i++)
{
if (a[i] > largest)
{
secondLargest = largest;
largest = a[i];
}
else if (a[i] > secondLargest && a[i] != largest)
{
secondLargest = a[i];
}
}
printf("The second largest element is: %d\n", secondLargest);
return 0;
}
Output:

6.Create a program to reverse the elements of any array.


Code:
#include<stdio.h>
int main()
{
int n,i,s;
printf("Enter the size of array: ");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
printf("Enter the %d elements value of array: ",i);
scanf("%d",&a[i]);
}
printf("Your given array is:-");
printf("[");
for(s=0;s<i-1;s++)
{
printf("%d, ",a[s]);
}
for(s=i-1;s<i;s++)
{
printf("%d",a[s]);
}
printf("]");
printf("\n");
//for reverse printing
for(i=0;i<n/2;i++)
{
int reverse=a[i];
a[i]=a[n-1-i];
a[n-1-i]=reverse;
}
printf("Reversed Array:-");
printf("[");
for(s=0;s<n-1;s++)
{
printf("%d, ",a[s]);
}
for(s=n-1;s<n;s++)
{
printf("%d",a[s]);
}
printf("]");
return 0;
}
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy