0% found this document useful (0 votes)
2 views5 pages

1D Array Programs

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

1D Array Programs

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

Program 1

//Storing elements in one dimensional array using three types of initializtion


#include<stdio.h>
void main()
{
//1st intialization at the time of declaration
int a1[5]={1,2,3,4,5};
int a2[5];
//2nd initialization of the array by using the array index value
a2[0]=11;
a2[1]=12;
a2[2]=13;
a2[3]=14;
a2[4]=15;
//3rd initialization of the array by using for loop
int a3[5],i;
printf("Enter third array element");
for(i=0;i<5;i++)
scanf("%d",&a3[i]);

//printing 1st array elements


printf("\nFirst array elements are:");
for(i=0;i<5;i++)
printf("%d\t",a1[i]);

//printing 2nd array elements


printf("\nSecond array elements are:");
for(i=0;i<5;i++)
printf("%d\t",a2[i]);

//printing 3rd array elements


printf("\nThird array elements are:");
for(i=0;i<5;i++)
printf("%d\t",a3[i]);
}

Program 2
//sorting the array in ascending order.
#include<stdio.h>
void main ()
{
int i,j,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n",a[i]);
}
}

Program 3
//print the largest and second largest element of the array.
#include<stdio.h>
void main ()
{
int arr[10],i,n,largest,smallest;
printf("Enter the size of the array:");
scanf("%d",&n);
printf("Enter the elements of the array:");
for(i = 0; i<n; i++)
{
scanf("%d",&arr[i]);
}
largest = arr[0];
smallest=arr[0];
for(i=0;i<n;i++)
{
if(arr[i]>largest)
{
largest = arr[i];
}
else if (arr[i]<smallest)
{
smallest=arr[i];
}
}
printf("largest = %d smallest = %d",largest,smallest);
}

Program-4
//addtion of two 1D arrays
#include<stdio.h>
void main()
{
int a3[5],a1[5],a2[5],i;
printf("enter the first array");
for(i=0;i<5;i++)
{
scanf("%d",&a1[i]);
}
printf("enter the second array");
for(i=0;i<5;i++)
{
scanf("%d",&a2[i]);
}
//add two arrays
for(i=0;i<5;i++)
{
a3[i]=a1[i]+a2[i];
}
printf("print the result array");
for(i=0;i<5;i++)
{
printf("\n%d\t",a3[i]);
}
}

You might also like