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

Array Operations

The document discusses various array operations in C including finding the maximum and minimum elements, sorting arrays in ascending and descending order, and searching for a specific element. It provides code examples to find the max element, sort arrays in ascending and descending order, and search for an element in an array.

Uploaded by

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

Array Operations

The document discusses various array operations in C including finding the maximum and minimum elements, sorting arrays in ascending and descending order, and searching for a specific element. It provides code examples to find the max element, sort arrays in ascending and descending order, and search for an element in an array.

Uploaded by

Navdeep sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Array operations:

Finding max element from an array:


#include<stdio.h>
int main()
{
int a[10],temp,max=0,i;
for(i=0;i<10;i++)
{
scanf("%d",&a[i]); //inputted an array
}
max=a[0]; //max is assigned the first value of array a
for(i=0;i<10;i++)
{
if(a[i]>a[i+1]) //Comparing a[0]with a[1] if greater
then interchanging values
{
temp=max;
max=a[i];
a[i]=a[i+1];//values swapped
}
}
//printed the value of max number in an array
printf("The maximum number from entered array is:
%d \n",max);

return 0;
}
Minimum will be vice versa of it.
Sorting an array in ascending order:
#include<stdio.h>
int main()
{
int a[10],j,temp,i,n;
printf("enter nmber of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]); //inputted an array
}

for(i=1;i<=n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1]) //Comparing a[0]with a[1] if
greater then interchanging values
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;//values swapped
}
}
}
printf("sorted array:\n");
for(i=0;i<+n;i++)
{
printf("%d \n",a[i]);
}
return 0;
}
For example:
Elements are 23,12,29,5
It wil give output like
5
12
23
29
In descending order we just need to make change in
comparison condition
Like:
for(j=0;j<n-1;j++)
{
if(a[j]<a[j+1]) //Comparing a[0]with a[1] if
smaller then interchanging values
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;//values swapped
}
}
}
Searching an element from array:
#include <stdio.h>

int main()
{
int array[100], search, i, n;
printf("Enter the number of elements in array\n");
scanf("%d",&n);

printf("Enter %d integer(s)\n", n);

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


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

printf("Enter the number to search\n");//asking user to


eneter the number to be searched
scanf("%d", &search);

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


{
if (array[i] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, i+1);
break;
}
}
if (i == n)
printf("%d is not present in array.\n", search);
return 0;
}

You might also like