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

1D Array programs List

The document contains a list of C programs that demonstrate various operations on 1D arrays, including summing elements, reversing order, bubble sorting, linear and binary searching, calculating averages, and finding the maximum element. Each program is presented with its source code and a brief description of its functionality. The programs cover fundamental array manipulations useful for beginners in programming.

Uploaded by

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

1D Array programs List

The document contains a list of C programs that demonstrate various operations on 1D arrays, including summing elements, reversing order, bubble sorting, linear and binary searching, calculating averages, and finding the maximum element. Each program is presented with its source code and a brief description of its functionality. The programs cover fundamental array manipulations useful for beginners in programming.

Uploaded by

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

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;
}

2.Reading the elements and printing the elements in reverse order


program:
#include <stdio.h>
int main() {
int n;
printf("enter the size of an array\n");
scanf("%d",&n);
int a[n],sum=0;
printf("enter the elements into the array\n");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);

}
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]);
}
}

4.Bubble sort without using functions


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]);
}
}
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);

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


if (a[i] == key) {
printf("Element found at index %d\n", i);
found = 1; // Set found to 1 when the element is found
break;
}
}

if (!found) {
printf("Element not found in the array.\n");
}

return 0;
}

6.Implement Binary Search


program:

#include<stdio.h>
int main() {
int n, key;

printf("Enter the number of elements: ");


scanf("%d", &n);

int a[n];

printf("Enter %d elements in sorted order:\n", n);


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

printf("Enter the element to search: ");


scanf("%d", &key);

int first = 0, last = n - 1, mid;


int found = 0;

while (first <= last) {


mid = (first + last) / 2;

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;
}

You might also like