0% found this document useful (0 votes)
17 views

Arrays

2
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Arrays

2
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

write a program to print all the negative numbers in the array

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE]; // Declare array of MAX_SIZE
int i, N;

/* Input size of the array */


printf("Enter size of the array : ");
scanf("%d", &N);

/* Input elements in the array */


printf("Enter elements in array : ");
for(i=0; i<N; i++)
{
scanf("%d", &arr[i]);
}

printf("\nAll negative elements in array are : ");


for(i=0; i<N; i++)
{
/* If current array element is negative */
if(arr[i] < 0)
{
printf("%d\t", arr[i]);
}
}

return 0;
}

***********************************************************************************
********************************************************
2. Write a program to find the frequency of the all the numbers in the array

#include <stdio.h>

void main()
{
int arr1[100], fr1[100];
int n, i, j, ctr;

printf("\n\nCount frequency of each element of an array:\n");

printf("Input the number of elements to be stored in the array :");


scanf("%d",&n);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
fr1[i] = -1;
}
for(i=0; i<n; i++)
{
ctr = 1;
for(j=i+1; j<n; j++)
{
if(arr1[i]==arr1[j])
{
ctr++;
fr1[j] = 0;
}
}

if(fr1[i]!=0)
{
fr1[i] = ctr;
}
}
printf("\nThe frequency of all elements of array : \n");
for(i=0; i<n; i++)
{
if(fr1[i]!=0)
{
printf("%d occurs %d times\n", arr1[i], fr1[i]);
}
}
}

***********************************************************************************
******************************************************
3.write a program to merge two arrays

#include<stdio.h>
#include<conio.h>
int main()
{
int arr1[50], arr2[50], size1, size2, i, k, merge[100];
printf("Enter Array 1 Size: ");
scanf("%d", &size1);
printf("Enter Array 1 Elements: ");
for(i=0; i<size1; i++)
{
scanf("%d", &arr1[i]);
merge[i] = arr1[i];
}
k = i;
printf("\nEnter Array 2 Size: ");
scanf("%d", &size2);
printf("Enter Array 2 Elements: ");
for(i=0; i<size2; i++)
{
scanf("%d", &arr2[i]);
merge[k] = arr2[i];
k++;
}
printf("\nThe new array after merging is:\n");
for(i=0; i<k; i++)
printf("%d ", merge[i]);
getch();
return 0;
}

***********************************************************************************
******************************************************

4.write a program to count the total number of even and odd elements in the array

#include <stdio.h>
#include <conio.h>

int main()
{
int a[1000],i,n,even=0,odd=0;

printf("Enter size of the array : ");


scanf("%d",&n);

printf("Enter elements in array : ");


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

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


{
if(a[i]%2==0)
even++;
else
odd++;

}
printf("even numbers in array: %d",even);
printf("\n odd numbers in array: %d",odd);

return 0;
}

***********************************************************************************
******************************************************

5.write a program to delete an element in the array by giving the specific position

#include <stdio.h>
#define MAX_SIZE 100

int main()
{
int arr[MAX_SIZE];
int i, size, pos;

printf("Enter size of the array : ");


scanf("%d", &size);
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("Enter the element position to delete : ");


scanf("%d", &pos);

if(pos < 0 || pos > size)


{
printf("Invalid position! Please enter position between 1 to %d", size);
}
else
{

for(i=pos-1; i<size-1; i++)


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

size--;

printf("\nElements of array after delete are : ");


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

return 0;
}

***********************************************************************************
*****************************************************
6.write a program to insert an array lament in the array given position

#include <stdio.h>

int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;

for (i = 0; i < 10; i++)


arr[i] = i + 1;

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


printf("%d ", arr[i]);
printf("\n");

x = 50;
pos = 5;

n++;

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


arr[i] = arr[i - 1];

arr[pos - 1] = x;

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


printf("%d ", arr[i]);
printf("\n");

return 0;
}

***********************************************************************************
******************************************************

7.write a program to count the total number of duplicate elements in the array

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE];
int i, j, size, count = 0;

/* Input size of array */


printf("Enter size of the array : ");
scanf("%d", &size);

/* Input elements in array */


printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

/*
* Find all duplicate elements in array
*/
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
/* If duplicate found then increment count by 1 */
if(arr[i] == arr[j])
{
count++;
break;
}
}
}

printf("\nTotal number of duplicate elements found in array = %d", count);

return 0;
}

***********************************************************************************
*****************************************************

You might also like