1D Array programs List
1D Array programs List
1.Reading the elements into the array and sum of elements in the array
program:
#include <stdio.h>
int main() {
int a[5],sum=0;
printf("enter the elements into the array\n");
for(int i=0;i<5;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("Sum of elements in the array is %d",sum);
return 0;
}
}
printf("printing elements in the array in reverse \n");
for(int i=n-1;i>=0;i--)
{
printf(" %d",a[i]);
}
return 0;
}
3.Implement bubble sort using function
program:
#include<stdio.h>
void bubblesort(int arr[]);
int main()
{
int arr[5]={10,7,2,3,4};
bubblesort(arr);
}
void bubblesort(int arr[])
{
int i, j, temp;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5-i-1; j++)
{
if( arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(i=0;i<5;i++)
{
printf(" %d",arr[i]);
}
}
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(i=0;i<5;i++)
{
printf(" %d",arr[i]);
}
}
5.Implement Linear search
program:
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
// Now that we have the size, we can declare the array
int a[n], key, found = 0;
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Enter the key element to search: ");
scanf("%d", &key);
if (!found) {
printf("Element not found in the array.\n");
}
return 0;
}
#include<stdio.h>
int main() {
int n, key;
int a[n];
if (a[mid] == key) {
printf("Element found at index %d\n", mid);
found = 1;
break;
}
else if (a[mid] < key) {
first = mid + 1;
}
else {
last = mid - 1;
}
}
if (!found) {
printf("Element not found.\n");
}
return 0;
}
7.Find out sum of elements and find out number of elements greater than average
program:
#include<stdio.h>
int main()
{
int arr[20],n,sum=0,avg,count=0;
printf("enter array elements ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
sum=sum+arr[i];
}
avg=sum/n;
printf("average of the numbers");
printf("%d\n",avg);
for(int i=0;i<n;i++)
{
printf("%d\n",arr[i]);
if(arr[i]>avg)
count++;
}
printf("the numbers greater than average are");
printf("%d\n",count);
return 0;
}
8.implement c program to find out maximum element in the array
program:
#include <stdio.h>
int main() {
int n;
printf("enter the size of an array\n");
scanf("%d",&n);
int a[n];
printf("enter the elements into the array\n");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int max=0;
for(int i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
printf("maximum element in the array %d",max);
return 0;
}