0% found this document useful (0 votes)
181 views28 pages

WAP To Find The Largest Element in An Array

The document contains code snippets for various array operations in C programming. Specifically, it includes code to: 1) Find the largest element in an array, insert an element into an array, delete an element from an array, and search for an element in an array. 2) Calculate the sum and average of elements in an array. 3) Sort arrays using insertion sort and bubble sort. 4) Check if a number is prime using recursion, print the Fibonacci series using recursion, and calculate the factorial of a number using recursion. 5) Add two matrices stored in 2D arrays and display the result. 6) Perform stack operations like push and pop on an array-based stack.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views28 pages

WAP To Find The Largest Element in An Array

The document contains code snippets for various array operations in C programming. Specifically, it includes code to: 1) Find the largest element in an array, insert an element into an array, delete an element from an array, and search for an element in an array. 2) Calculate the sum and average of elements in an array. 3) Sort arrays using insertion sort and bubble sort. 4) Check if a number is prime using recursion, print the Fibonacci series using recursion, and calculate the factorial of a number using recursion. 5) Add two matrices stored in 2D arrays and display the result. 6) Perform stack operations like push and pop on an array-based stack.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

1

WAP to find the largest element in an array:


#include<stdio.h>
#include<conio.h>
void main()
{
int array[50],size,i,largest;
printf("\nEnter the size of the array:");
scanf("%d",&size);
printf("\nEnter %d elements of the array:",size);
for(i=0;i<size;i++)
scanf("%d",&array[i]);
largest=array[0];
for(i=1;i<size;i++)
{
if(largest<array[i])
largest=array[i];
}
printf("\n largest element present in the given array is:
%d",largest);
getch();
}
2

WAP to insert an element in an array:

#include<stdio.h>
#include<conio.h>
void main()
{
int array[100], position, c, n, value;
clrscr();
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to insert an element\n");
scanf("%d", &position);
printf("Enter the value to insert\n");
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c];
array[position-1] = value;
printf("Resultant array is\n");
3

for (c = 0; c <= n; c++)


printf("%d\n", array[c]);
getch();
}
4

WAP to delete an element from an array:

#include<stdio.h>
#include<conio.h>
void main()
{
int array[100], position, c, n;
clrscr();
printf("Enter number of elements in array:\n");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element:\n");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.\n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
5

array[c] = array[c+1];
printf("Resultant array is:\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}

getch();
}
6

WAP to search an element in an array:

#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], search, c, n;
7

clrscr();
printf("Enter the number of elements in array:\n");
scanf("%d",&n);
printf("Enter %d integer(s):\n", n);
for (c=0;c<n;c++)
scanf("%d",&array[c]);
printf("Enter a number to search:\n");
scanf("%d",&search);
for (c=0;c<n;c++)
{
if (array[c]== search) /* If required element is found */
{
printf("%d is present at location: %d.\n",search,c+1);
break;
}
}
if (c==n)
printf("%d isn't present in the array.\n",search);
getch();
}
8
9

WAP to enter 10 numbers in an array.calculate the sum


and average:

#include <stdio.h>
#include<conio.h>
int main()
{
int Arr[100],n,i,sum=0;
clrscr();
printf("Enter the number of elements you want to insert:");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter element %d:",i+1);
scanf("%d",&Arr[i]);
sum+=Arr[i];
}
printf("\nThe sum of the array is:%d",sum);
printf("\nThe average of the array is:%0.2f",(float)sum/n);
getch();
}
10
11

WAP to sort the elements of an array using Insertion


sort:
#include <stdio.h>
#include<conio.h>
void main()
{
int n,array[10],i,j,temp;
clrscr();
printf("Enter number of elements:\n");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for (i=0;i<n;i++)
{
scanf("%d",&array[i]);
}

for(i=1;i<=n-1;i++)
{
j=i;
while(j>0&&array[j-1]>array[j])
{
temp=array[j];
array[j]=array[j-1];
array[j-1]=temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i=0;i<=n-1;i++)
{
printf("%d\n",array[i]);
}
getch();
}
12
13

WAP to sort the elements of an array using bubble sort:


#include<stdio.h>
#include<conio.h>
void main()
{
int a[40],size,i,j,temp,swap;
clrscr();
printf("enter size of array:");
scanf("%d",&size);
printf("enter elements:");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<(size-1);i++)
{
swap=0;
for(j=0;j<(size-i)-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
swap=1;
}
}
if(swap==0)
{
break;
}
}
14

printf("\n\tarray after bubble sort:");


for(i=0;i<size;i++)
{
printf("\n%d",a[i]);
}
getch();
}
15

WAP tousing recursion to check if the number is prime


or not:
#include <stdio.h>
#include<conio.h>
int primeno(int,int);
int main()
{
int num,check;
clrscr();
printf("Enter a number:");
scanf("%d",&num);
check=primeno(num,num/2);
if(check==1)
{
printf("%d is a prime number\n",num);
}
else
{
printf("%d is not a prime number\n",num);
}
getch();
}
int primeno(int num,int i)
{
if(i==1)
{
return 1;
}
else
{
if(num%i==0)
{
16

return 0;
}
else
{
return primeno(num,i-1);
}
}
}
17

WAP to using recursion to print fibonacci seriesfor first


15 numbers:
#include<stdio.h>
#include<conio.h>
int fab(int n)
{
if(n==0||n==1)
{
return n;
}
else
{
return fab(n-1)+fab(n-2);
}
}
void main()
{
int i;
clrscr();
for(i=1;i<=15;i++)
{
printf("%d\t",fab(i));
}
getch();
}
18

WAP to (using recursion) to calculate the factorial:


#include<stdio.h>
#include<conio.h>
int fact(int n)
{
if(n==0||n==1)
return n;
else
return n*fact(n-1);
}
void main()
{
int num;
clrscr();
printf("enter a number:");
scanf("%d",&num);
printf("factorial =%d",fact(num));
getch();
}
19

WAP to enter 2 matrix using 2-D arrays,calculate the


sum and display the resultant:

#include<stdio.h>
#include<conio.h>
void main()
{
int A[3][3],B[3][3],C[3][3],i,j;
clrscr();

printf("\nenter 9 numbers for first matrix:\n");


for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
scanf("%d",&A[i][j]);
printf("\nenter 9 numbers for second matrix:\n");
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
scanf("%d",&B[i][j]);
printf("\nsum of matrices is :\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
printf("\n");
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t",C[i][j]);
20

}
printf("\n");
}
getch();
}
21

WAP to perform stack operations,push() and pop()


usingarrays:

#include<stdio.h>
#include<conio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
22

pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
23

top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
24
25

WAP to reverse a string using an array:


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50];
char stack[50];
int len,top=-1,i=0;
clrscr();
printf("enter the string:");
gets(str);
len=strlen(str);
while(i<len)
{
top=top+1;
stack[top]=str[i];
i=i++;
}
while(top!=-1)
{
printf("%c",stack[top]);
top=top-1;
}
getch();
}
26

WAP toinsert an element ,delete an element and


display the elements of a queue using array:

#include<stdio.h>
#include<conio.h>
#define SIZE 10
void enQueue(int);
void deQueue();
void display();
int queue[SIZE],front=-1,rear=-1;
void main()
{
int value,choice;
clrscr();
while(1){
printf("\n* MENU *\n");
printf("Enter Choice:1.Insertion\t2.Deletion\t3.Display\t4.Exit");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert:");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection,Try again");
}
}
27

}
void enQueue(int value){
if(rear==SIZE-1)
printf("\nQueue is Full Insertion is not possible");
else{
if(front==-1)
front=0;
rear++;
queue[rear]=value;
}
}
void deQueue(){
if(front==rear)
printf("\nQueue is Empty Deletion is not possible");
else{
printf("\nDeleted: %d",queue[front]);
front++;
if(front==rear)
front=rear=-1;
}
}
void display(){
if(rear==-1)
printf("\nQueue is Empty!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
}
28

You might also like