0% found this document useful (0 votes)
8 views9 pages

Array

The document contains multiple C programs demonstrating various operations on arrays, including inserting an element, searching for an element, reversing an array, sorting an array using bubble sort, and finding the transpose of a matrix. Each program includes input/output examples to illustrate functionality. The code snippets are incomplete and contain several syntax errors.

Uploaded by

chikbhandary2810
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)
8 views9 pages

Array

The document contains multiple C programs demonstrating various operations on arrays, including inserting an element, searching for an element, reversing an array, sorting an array using bubble sort, and finding the transpose of a matrix. Each program includes input/output examples to illustrate functionality. The code snippets are incomplete and contain several syntax errors.

Uploaded by

chikbhandary2810
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

4.

6 Write a Cprogram to insert an element in an Array at any location


#include<stdio.h>
int main(0

int arr(30], item, num, i, location;

printf("\nEnter no of elements in an array: ");


scanf("%d", &num);
printf("Enter array elements:");
for (i 0;i<num;i++)
=
scanf("%d", &arr|i);
inserted:";
item to be
printf("lnEnter the
scanf("%d", &item);
to insert
the item:")
printf("lnEnter the location where

scanf%d"&location);
//Find location to insert item

for(i n u m ; i>=location; i--)


arfi = ar|[i - 1];

numtt;

arr[location- 1]=item
IIAfter insertion of item, the array is:
Printf"After insertion operation array isn:");
for (i = 0; i < num; it+)

printf" %d", arrfi]);


return (0);

INPUT/OUTPUT:
Enter no of elements in an array :6
Enter array elements:3
9

Enter the item to be inserted :8

Enter the location where to insert the


item:5
After insertion operation
array is:
39 27 8 15

47 Write a C Program to Search an element in


Arrav,
#include<stdio.h>
int main()

int a
30), item, num, i
printf("nEnter no of elements:");
scanf("%d",&num);

printf("nEnterthe Aray elements:"),


for (i =0; i<num;it+

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

I/Read the element to be searched

printf("nEnter the item to be searched:");

scanf(%d", &item);
//Search starts from the Oth location
i0;
while (i < num && item != a[i])

it+;
ifi< num)
printf("Item found at the location = %d", i +1);

else

printf"Item not found");


return (0);

INPUT/OUTPUT:
Enter no of elements :5

Enter the Arrray elements:12


23
34
45
56

Enter the item to be searched :45


Item found at the location =4
A
#include<stdio.h>

int main)

int a[30], i, j, num, temp;


printf("nEnter no of elements: ")
scanf("%d", &num);

/Read elements of the array


printf"Enter array elements:");

for (i = 0; i < num; it+)

scanf"%d", &alil);

j=i-1; /j will Point last


to Element
element
i=0; l i will be pointing to first

while(i <j)

temp = a

ali ajil
ajl=tempP
itt
j

printf("nResultant array after reversal: ");

for(i0; i <num; it+)


printf("%d lr', afi]);
return (0);

INPUT/OUTPUT:
Enterno of elements:5
Enter array elements:2

98 7 2
Resultant array after reversal:1|

107
in bubble
sort method
A13 Write a C program to sort array elements

#include<stdio.h>
#include<stdlib.h>
int main0

int a[40],n,ij,temp, s=0;


printf("n How many numbers");
scanf("%d",&n);
if(n>40)

printf("n Too many Numbers");


exit(0);

printf("n Enter the array elements \n");


for(i-0; i<n; it+)
scanf(%d", &a[i]);
for(i =
0; i<n-1 && s-=0; it++)

s=1;
forg 0 ; j <(n - i) -1;j++)

iffaj]>a[i+1])

temp= alj]:
ajl aj+1];
alj+1]= temp;
s-0;

printf("n sorted array: n");


for(i-0;i <n; ++i)
printf("n %d", a[i]);
return 0;
program to find the transpose of a matrx.
A.20 Write a
#include<stdio.h>

#include<conio.h>

void main()

int a[10][10].m,n,[Link]

clrscr)
rows and columns... ");
printf("Enter the number of
scanf"%d%d",&m,&n);

printf"Enter the elements of the matrixn");

for(i-0;i<m;it+)
forg-0;j<nj++)
scanf("%d",&afi]gl
printf("orginal matrix is\n");

for(i-0;i<m;i++)

forg-0j<n:j++)
printf("%dlt",a[i]g]);
printf"n");

113
printf("tranpose
matrix... ...n");
for(i-0;i<m;it+)

fori-0in;jt+)

printf("%d"alilli);

printf("ln")

getch):

Input: Enter the number ofrows and columns.. 3 3

Enter the elements of the matrix

2 1 2 1 2 3 3 3

Output: orginal matrix is

121

212

3 33

tranpose matrix...

123

213

123

You might also like