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

C Arrays

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

C Arrays

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

Elements of array in reverse order:

#include <stdio.h>

void main()

int i,n,a[100];

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

scanf("%d",&n);

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

scanf("%d",&a[i]);

printf("\nThe values stored into the array are : \n");

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

printf("% d",a[i]);

printf("\n\nThe values store into the array in reverse are :\n");

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

printf("% d",a[i]);

printf("\n\n");

Number of duplicate elements of the array:

#include <stdio.h>

int main()

int arr[100];
int n,mm=1,ctr=0;

int i, j;

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",&arr[i]);

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

for ( j = i + 1; j < n; j++)

if (arr[i] == arr[ j])

ctr++;

break;

printf("Total number of duplicate elements found in the array: %d\n", ctr);

return 0;

Merging and sorting the array in descending order:

#include <stdio.h>

void main()
{

int arr1[100], arr2[100], arr3[200];

int s1, s2, s3;

int i, j, k;

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

scanf("%d",&s1);

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

printf("element - %d : ",i);

scanf("%d",&arr1[i]);

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

scanf("%d",&s2);

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

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

printf("element - %d : ",i);

scanf("%d",&arr2[i]);

/* size of merged array is size of first array and size of second array */

s3 = s1 + s2;

/*----------------- insert in the third array------------------------------------*/

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

arr3[i] = arr1[i];

}
for( j=0;j<s2; j++)

arr3[i] = arr2[ j];

i++;

/*----------------- sort the array in decending order ---------------------------*/

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

for(k=0;k<s3-1;k++)

if(arr3[k]<=arr3[k+1])

j=arr3[k+1];

arr3[k+1]=arr3[k];

arr3[k]=j;

/*--------------- Prints the merged array ------------------------------------*/

printf("\nThe merged array in decending order is :\n");

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

printf("%d ", arr3[i]);

printf("\n\n");

}
Ascending order:

#include <stdio.h>

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

printf("\n\nsort elements of array in ascending order :\n ");


printf("----------------------------------------------\n");

printf("Input the size of 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]);
}

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


{
for(j=i+1; j<n; j++)
{
if(arr1[j] <arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
printf("\nElements of array in sorted ascending order:\n");
for(i=0; i<n; i++)
{
printf("%d ", arr1[i]);
}
printf("\n\n");
}

You might also like