Exp 1 103 Madf
Exp 1 103 Madf
}
cout << "LARGEST NUMBER IS " << large;
return 0;
}
int min(int a[10], int n)
{
int small;;
small = a[1];
for (int i = 2;i <= n;i++)
{
if (a[i]<small)
{
small = a[i];
}
}
cout << "\nSMALLEST NUMBER IS " << small;
return 0;
}
int main()
{
int a[10], n, large, small;
cout << "HOW MANY NUMBERS YOU WANT TO ENTER\n";
cin >> n;
cout << "ENTER NUMBERS\n";
for (int i = 1;i <= n;i++)
{
cin >> a[i];
}
max(a, n);
min(a, n);
return 0;
} Output:
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103
Padre Conceicao College of Engineering
2) Selection sort
Input:
#include <iostream>
using namespace std;
void sort(int a[10], int n)
{
int min = 0;
for (int i = 1;i <= n;i++)
{
min = i;
for (int j = i + 1;j <= n;j++)
{
if (a[j]<a[min])
{
min = j;
}
}
int temp;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
cout << "AFTER SORTING\n";
for (int i = 1;i <= n;i++)
{
cout << a[i] << " ";
}
}
int main()
{
int a[10], n, min;
cout << "ENTER SIZE OF ARRAY\n";
cin >> n;
cout << "ENTER ELEMENTS OF ARRAY\n";
for (int i = 1;i <= n;i++)
{
cin >> a[i];
}
sort(a, n);
return 0;
}
Output:
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103
Padre Conceicao College of Engineering
3) Iterative Sum without counting statements.
Input:
#include <iostream>
using namespace std;
int sum(int a[10], int n)
{
int sum = 0;
for (int i = 1;i <= n;i++)
{
sum += a[i];
}
return sum;
}
int main()
{
int a[10], n, s = 0;
cout << "ENTER SIZE OF ARRAY\n";
cin >> n;
cout << "ENTER ELEMENTS OF ARRAY\n";
for (int i = 1;i <= n;i++)
{
cin >> a[i];
}
s = sum(a, n);
cout << "SUM OF ELEMENTS OF ARRAY IS " << s;
}
Output:
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103
Padre Conceicao College of Engineering
4) Iterative Sum with count statements.
Add statements to increment count at each program step and display the total number of
program steps at the end.
Input:
*#include <iostream>
using namespace std;
int count = 0;
int sum(int a[10], int n)
{
int sum = 0;
count += 1;//sum=0
for (int i = 1;i <= n;i++)
{
count += 1;
sum += a[i];
count += 1;
}
count += 1;//LAST FOR
count += 1;//RETURN
return sum;
}
int main()
{
int a[10], n, s = 0;
cout << "ENTER SIZE OF ARRAY\n";
cin >> n;
cout << "ENTER ELEMENTS OF ARRAY\n";
for (int i = 1;i <= n;i++)
{
cin >> a[i];
}
s = sum(a, n);
cout << "SUM OF ELEMENTS OF ARRAY IS " << s;
cout << "\nTOTAL STEPS ARE " << count;
}
Output:
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103
Padre Conceicao College of Engineering
5) Add two m x n matrices.
Input:
#include <iostream>
using namespace std;
void add(int a[10][10], int b[10][10], int c[10][10], int m, int n)
{
for (int i = 1;i <= m;i++)
{
for (int j = 1;j <= n;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
cout << "SUM OF BOTH MATRICES\n";
for (int i = 1;i <= m;i++)
{
for (int j = 1;j <= n;j++)
{
cout << c[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int a[10][10], b[10][10], c[10][10], n, m;
cout << "ENTER NO. OF ROWS & COLUMNS OF MATRIX\n";
cin >> m >> n;
cout << "ENTER ELEMENTS OF MATRIX 1\n";
for (int i = 1;i <= m;i++)
{
for (int j = 1;j <= n;j++)
{
cin >> a[i][j];
}
}
cout << "ENTER ELEMENTS OF MATRIX 2\n";
for (int i = 1;i <= m;i++)
{
for (int j = 1;j <= n;j++)
{
cin >> b[i][j];
}
}
add(a, b, c, m, n);
}
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103
Padre Conceicao College of Engineering
Output:
6) Fibonacci numbers
Input:
#include <iostream>
using namespace std;
void fibonacci(int n)
{
cout << n << "th FIBONACCI NO. IS ";
if (n <= 1)
{
cout << n;
}
else
{
int f1 = 0, f2 = 1, f3;
for (int i = 3;i <= n;i++)
{
f3 = f1 + f2;
f1 = f2;
f2 = f3;
}
cout << f3;
}
}
int main()
{
int n;
cout << "ENTER N(POSITION)\n";
cin >> n;
fibonacci(n);
return 0;
} Output:
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103
Padre Conceicao College of Engineering
7) Exponentiation
Input:
#include <iostream>
using namespace std;
int exponentiate(int x, int n)
{
int m = n, power = 1, z = x;
while (m>0)
{
while (m % 2 == 0)
{
m = m / 2;
z = z*z;
}
m = m - 1;power = power*z;
}
return power;
}
int main()
{
int x, n, ans;
cout << "ENTER NUMBER AND POWER\n";
cin >> x >> n;
ans = exponentiate(x, n);
cout << x << "^" << n << " IS " << ans;
}
Output:
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103
Padre Conceicao College of Engineering
8) Insertion sort
Input:
#include <iostream>
using namespace std;
void insertionsort(int a[10], int n)
{
for (int j = 2;j <= n;j++)
{
int item = a[j], i = j - 1;
while ((i) >= 1 && item<a[i])
{
a[i + 1] = a[i];
i--;
}
a[i + 1] = item;
}
cout << "AFTER SORTING\n";
for (int i = 1;i <= n;i++)
{
cout << a[i] << " ";
}
}
int main()
{
int a[10], n;
cout << "ENTER SIZE OF ARRAY\n";
cin >> n;
cout << "ENTER ELEMENTS OF ARRAY\n";
for (int i = 1;i <= n;i++)
{
cin >> a[i];
}
insertionsort(a, n);
}
Output:
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103
Padre Conceicao College of Engineering
9) Sequential search
Input:
#include <iostream>
using namespace std;
int seqsearch(int a[10], int x, int n)
{
int i = n;
a[1] = x;
while (a[i] != x)
{
i--;
}
return i;
}
int main()
{
int a[10], n, x;
cout << "ENTER SIZE OF ARRAY\n";
cin >> n;
cout << "ENTER ELEMENTS OF ARRAY\n";
for (int i = 1;i <= n;i++)
{
cin >> a[i];
}
cout << "ENTER ELEMENT TO BE SEARCHED\n";
cin >> x;
cout << "YPUR SEARCH ELEMENT IS FOUND AT POSITION " << seqsearch(a, x, n);
}
Output:
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103
Padre Conceicao College of Engineering
10) . Matrix transpose
Input;
#include <iostream>
using namespace std;
void transpose(int a[10][10], int n)
{
int t;
Output:
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103
Padre Conceicao College of Engineering
11) Multiplication of n x n matrices.
Input:
#include<iostream>
using namespace std;
void mult(int a[10][10], int b[10][10], int c[10][10], int n)
{
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
c[i][j] = 0;
for (int k = 1;k <= n;k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
cout << "AFTER MULTIPLICATION\n";
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
cout << c[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int a[10][10], b[10][10], c[10][10], n;
cout << "ENTER NO. OF ROWS/COLUMNS OF MATRIX(ROWS=COLUMN)\n";
cin >> n;
cout << "ENTER ELEMENTS OF MATRIX 1\n";
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
cin >> a[i][j];
}
}
cout << "ENTER ELEMENTS OF MATRIX 2\n";
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
cin >> b[i][j];
}
}
mult(a, b, c, n);
}
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103
Padre Conceicao College of Engineering
Output:
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103
Padre Conceicao College of Engineering
}
cout << "ENTER ELEMENTS OF MATRIX 2\n";
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= p;j++)
{
cin >> b[i][j];
}
}
mult(a, b, c, m, n, p);
}
Output:
Modern Algorithm Design Foundation Lab, Department of Computer Engineering RollNo. 23CE103