An algroithm definition is as simple as a set of step-by-step procedures or a list of rules to follow for completing a specific task or solving a particular problem.
Example of an algorithm for baking a cake ->
1) Gather all the ingredients
2) Pre-heat the oven
3) Measure out the ingredients
4) Mix together to make the batter
5) Put the butter into the pan
6) Pour the batter into the pan
7) Put the pan in the oven
8) Set a timer
9) When the timer goes off, take the pan out of the over
10) Eat the cat and ENJOY!
Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts
30 Dec 2023
29 Dec 2023
Algorithm to Multiply two numbers
function multiplyTwoNumbers(a, b):
result = 0 // Initialize the result to zero
// Iterate through each digit of the second number (y)
while y > 0:
// If the rightmost digit of y is 1, add the current value of x to the result
if y is odd:
result = result + x
// Shift both x and y to the right
x = x << 1
// Multiply x by 2 (left shift)
y = y >> 1
// Divide y by 2 (right shift)
return result
result = 0 // Initialize the result to zero
// Iterate through each digit of the second number (y)
while y > 0:
// If the rightmost digit of y is 1, add the current value of x to the result
if y is odd:
result = result + x
// Shift both x and y to the right
x = x << 1
// Multiply x by 2 (left shift)
y = y >> 1
// Divide y by 2 (right shift)
return result
28 Dec 2023
Find Maximum Value Input: An array of numbers
Find Maximum Value Input: An array of numbers, arr
1. If arr is empty, return an error or a special value indicating no maximum.
2. Set max_value to the first element of arr (arr[0]).
3. For each element num in arr starting from the second element (index 1):
a. If num is greater than max_value, update max_value to num.
4. Return max_value as the maximum value in the array. Example: arr = [5, 2, 9, 1, 5, 6]
Result: The maximum value is 9.
1. If arr is empty, return an error or a special value indicating no maximum.
2. Set max_value to the first element of arr (arr[0]).
3. For each element num in arr starting from the second element (index 1):
a. If num is greater than max_value, update max_value to num.
4. Return max_value as the maximum value in the array. Example: arr = [5, 2, 9, 1, 5, 6]
Result: The maximum value is 9.
Algorithm - Binary Search
// Write an algorithm to do a binary search in an array
- Find the midpoint of the sorted array
- Compare the midpoint to the value of interest
- If the midpoint is larger than the value, perform binary search on right half of the array
- If the midpoint is smaller than the value, perform binary search on left half of the array
- Repeat these steps until the midpoint value is equal to the value of interest or we know the value is not in the array
- Find the midpoint of the sorted array
- Compare the midpoint to the value of interest
- If the midpoint is larger than the value, perform binary search on right half of the array
- If the midpoint is smaller than the value, perform binary search on left half of the array
- Repeat these steps until the midpoint value is equal to the value of interest or we know the value is not in the array
10 Mar 2013
STAR PATTERN4
Q.) Write a Program to Print The Following Star Pattern in c++
*****
****
***
**
*
Sol)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,n;
cout<<"Enter number of rows"<<endl;
cin>>n;
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
getch();
}
=OUTPUT=
Enter number of rows 5
*****
****
***
**
*
PRINTING A MATRIX IN C++
Q.) Write a C++ Program to input the elements of a matrix and then print them in matrix form.
Sol)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10];
int i, j, n, m;
cout<<"enter no of rows \n";
cin>>n;
cout<<"enter no of colomn \n";
cin>>m;
cout<<"enter the values of matrix \n";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
cin>>a[i][j];
}
}
cout<<"matrix u have entered is \n ";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}
getch();
}
=OUTPUT=
29 Jan 2013
Algo for even or odd Number
Q.) Write a Algorithm to find out number is odd or even .
Sol.)
Step 1: Start
Step 2: Input Number
Step 3: rem=Number mod 2
Step 4: if rem=0 then,
print "EVEN number"
else
print "ODD number"S
Step 5: Stop 24 Jan 2013
Algorithm for GRAPH
Q.) Write a ALGORITHM to show whether the GRAPH is Connected or Not.
Sol)
# Comp( Graph )
{
For each vertex v belongs to Vertices[Graph]
{
make-set(v)
}
For each edge (u,v) belongs to Edges[Graph]
{
if(find-set(u) not equal to find-set(v)
{
Union(u,v)
}
}
}
# Connected-Comp(u,v)
{
if (find-set(u)=find-set(v))
{
print "CONNECTED"
}
else
print "NOT CONNECTED"
}
Algo for MErge sort using PSUEDO-CODE
Q.) Write a ALGORITHM for the set of elements using the MERGE SORT.
Sol)
MergeSort( A, p, r)
{
if (p<r)
{
mid=[(p+r)/2]
}
MergeSort(A, p, mid)
MergeSort(A, mid+1, r)
Merge(A, p, r, mid, Z)
}
Merge(A, p, r, mid, Z)
{
Initialize two variables 'i' and 'j'
while( (i<=mid) AND (j<=r) )
{
if(x[i]<=x[j])
{
Z={x[i]}
i=i+1
}
else
{
Z={x[j]}
j=j+1
}
}
if( i>mid )
{
Z= Z + Append(x[j]....x[r])
}
else
{
Z=Z + Append(x[i].....x[mid])
}
}
Algo of QUICK SORT using psuedo-code
Q.) Write a ALGORITHM for the given set of elements using QUICK SORT .
Sol)
# Quicksort(Arr, P, R)
{
if(P<R)
{
q=partition(Arr, P, R)
}
Quicksort(Arr, P, Q)
Quicksort(Arr, Q+1, R)
}
Partition(Arr, P, R)
{
x=Arr[P]
i=p-1
j=r+1
while(TRUE)
{
do
{
j=j-1
}
while(Arr[j]>x)
do
{
i=i+1
}
while(Arr[i]<x)
if(i<j)
{
Exchange(Arr[i],Arr[j])
}
else
{
return j
}
}
Subscribe to:
Posts (Atom)