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

Tute 8

The document contains 7 code snippets demonstrating different array operations in C including: reversing an array, inserting an element into an array at a given location, copying one array to another, deleting a given element from an array, performing a linear search on an array, calculating the sum and average of array elements, and finding the largest and smallest elements in an array.

Uploaded by

arush
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)
32 views5 pages

Tute 8

The document contains 7 code snippets demonstrating different array operations in C including: reversing an array, inserting an element into an array at a given location, copying one array to another, deleting a given element from an array, performing a linear search on an array, calculating the sum and average of array elements, and finding the largest and smallest elements in an array.

Uploaded by

arush
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
You are on page 1/ 5

1.Reversing of array.

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

printf("Enter no of elements:");
scanf("%d",&n);

int i,r[n];

for(i=0;i<n;i++)
{
printf("Enter value of r[%d]:",i);
scanf("%d",&r[i]);
}
for(i=0;i<n/2;i++)
{
temp=r[i];
r[i]=r[n-i-1];
r[n-i-1]=temp;
}

for(i=0;i<n;i++)
{
printf("Value of r[%d]:%d \n",i,r[i]);
}
return 0;
}
2.To insert an element at a
given location of an array
#include <stdio.h>
int main()
{
int n;
printf("Enter no of elements:");
scanf("%d",&n);

int i,a[n];

for(i=0;i<n;i++)
{
printf("Enter value of a[%d]:",i);
scanf("%d",&a[i]);
}

int x;
printf("\n\nEnter loaction to insert
element:");
scanf("%d",&x);

int b[n+1];
for(i=0;i<1+n;i++)
{
if(i<x)
{
b[i]=a[i];
}
else if(i==x)
{
printf("enter Value at r[%d]:",x);
scanf("%d",&b[i]);
}
else
b[i]=a[i-1];
}

printf("\nValue of r[]=");
for(i=0;i<n+1;i++)
{
printf("%d ",b[i]);
}

return 0;
}
3. To copy one array to another
array.
#include <stdio.h> int main()
{
int main() int n;
{
int n; printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter No of element:");
scanf("%d",&n); int i,r[n];

int i,r[n],s[n]; for(i=0;i<n;i++)


{
for(i=0;i<n;i++) printf("Enter value of r[%d]:",i);
{ scanf("%d",&r[i]);
printf("Enter value of r[%d]:",i); }
scanf("%d",&r[i]);
} int x;
printf("\nEnter element want to delete:");
for(i=0;i<n;i++) scanf("%d",&x);
{
s[i]=r[i]; for(i=x;i<n-1;i++)
} {
r[i]=r[i+1];
printf("\n\nValue of s[]={"); }
for(i=0;i<n;i++)
{ printf("\nValue of r[]=");
printf("%d ",s[i]); for(i=0;i<n-1;i++)
} {
printf("}"); printf("%d ",r[i]);
return 0; }
}
}

4. Delete a given element in an


array.
5. Linear search for element. 6. Sum and average of array
#include <stdio.h> elements
int main()
{ #include <stdio.h>
int n;
int main()
printf("Enter no of elements:"); {
scanf("%d",&n);
int n;
int i,a[n]; float sum=0;
printf("Enter value of a[]:"); printf("Enter no of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ int i,a[n];
scanf("%d",&a[i]);
} for(i=0;i<n;i++)
{
int search,jasoos=0; printf("Enter value of a[%d]:",i);
scanf("%d",&a[i]);
printf("Enter Value you want to search:"); }
scanf("%d",&search); for(i=0;i<n;i++)
{
printf("\n\n%d is ",search); sum=sum+a[i];
for(i=0;i<n;i++) }
{
if(a[i]==search) printf("SUM=%.2f\nAVERAGE=%.2f",sum,sum
{ /n);
printf("\nat %d ",i);
jasoos++; return 0;
} }
}
if(jasoos==0)
printf("not present.");

return 0;
}
7. Find largest and smallest in
array
#include <stdio.h>
int main()
{

int n;

printf("Enter no of elements:");
scanf("%d",&n);

int i,a[n];

for(i=0;i<n;i++)
{
printf("Enter value of a[%d]:",i);
scanf("%d",&a[i]);
}

int L=a[0],S=a[0];
for(i=0;i<n;i++)
{
if(L<a[i])
L=a[i];

if(S>a[i])
S=a[i];
}

printf("Smallest And Largest No :%d and


%d",S,L);

return 0;
}

You might also like