[go: up one dir, main page]

0% found this document useful (0 votes)
13 views63 pages

PF Programs Sem1

The document contains multiple C++ programs that demonstrate various algorithms and functionalities, including generating patterns (like pyramids and diamonds), performing matrix convolution, checking for cyclic numbers, converting binary to decimal, identifying palindromes, and analyzing series types. Each program includes user input prompts and outputs results based on the computations performed. The document serves as a collection of coding examples for educational purposes.

Uploaded by

Aiyshah Meerab
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)
13 views63 pages

PF Programs Sem1

The document contains multiple C++ programs that demonstrate various algorithms and functionalities, including generating patterns (like pyramids and diamonds), performing matrix convolution, checking for cyclic numbers, converting binary to decimal, identifying palindromes, and analyzing series types. Each program includes user input prompts and outputs results based on the computations performed. The document serves as a collection of coding examples for educational purposes.

Uploaded by

Aiyshah Meerab
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/ 63

alphabets pyramid:

#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter the number of rows: ";


cin >> rows;

for(int row = 0; row < rows; row++)


{
int spaces = rows - row;

for(int j = 0; j < spaces; j++) cout << " ";

char ch = 'A';
for(int j = 0; j < row * 2 + 1; j++)
{
if (j >= row) cout << ch--;
else cout << ch++;
}
cout << endl;
}
return 0;
}
Sample Input:

Sample Output:

ABA

ABCBA

ABCDCBA

ABCDEDCBA

convolution of a 5x5 matrix with a 3x3 matrix, the


values have been hardcoded

#include <iostream>

int cellvalue(int d[5][5], int f[3][3], int i, int j)


{
int val;
for(int x = 0, di = i; x < 3; x++, di++)
{
for(int y = 0, dj = j; y < 3; y++, dj++)
{
val += d[di][dj] * f[x][y];
}
}
return val;
}

void convolve(int d[5][5], int f[3][3], int res[3][3])


{
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
res[i][j] = cellvalue(d, f, i, j);
}

int main()
{
int d[5][5] = {{1,2,3,4,5},
{0,1,2,3,4},
{1,0,5,4,2},
{3,2,1,5,3},
{1,2,1,1,0}};

int f[3][3] = {{1,0,1},


{1,0,1},
{1,0,1}};

int res[3][3] = {0};

convolve(d, f, res);

for(int i = 0; i < 3; i++)


{
for(int j = 0; j < 3; j++)
std::cout << res[i][j] << " ";
std::cout << std::endl;
}

return 0;
}

program to tell if a number is cyclic or not

#include <iostream>
using namespace std;

bool isCyclic(int n)
{
int length = 0;
int temp = n;

while(temp > 0)
{
length++;
temp /= 10;
}

int currentPerm = n;
int modulus = 1;

for(int i = 0; i < length; i++) modulus *= 10;

for(int i = 1; i < length; i++)


{
int lastDigit = currentPerm % 10;
currentPerm = (lastDigit * modulus / 10) + (currentPerm /
10);

if (currentPerm == n) return false;

if (currentPerm % n != 0) return false;


}

return true;
}

int main()
{
int n;

cout << "Enter a number u faggot: ";


cin >> n;

if (isCyclic(n))
cout << "yay its cyclic u happy jerk";
else
cout << "ha! its not cyclic u retard";

cout << endl;

return 0;
}
program to convert binary number to decimal

#include <iostream>
using namespace std;

int main()
{
int binaryDigits[100];
int size = 0;

do
{
cout << "Enter the binary digit " << size << "(-1 to stop
asking): ";
cin >> binaryDigits[size];
if (binaryDigits[size] == 0 || binaryDigits[size] == 1 ||
binaryDigits[size] == -1)
size++;
} while(binaryDigits[size - 1] != -1);

size--;

int number = 0;

for(int i = 0; i < size; i++)


{
int digit = 1;

for(int j = 0; j < i; j++) digit *= 2;


digit *= binaryDigits[i];

number += digit;
}

cout << "The decimal number is " << number << endl;

return 0;

palindrome or not

#include <iostream>
using namespace std;

int main()
{
int number, temp, size = 0;

cout << "Input any number: ";


cin >> number;

while(number % 10 == 0) number /= 10;

temp = number;
while(temp > 0)
{
temp /= 10;
size++;
}

int iterations = size / 2;

int digits[100];

temp = number;

for(int i = 0; i < size; i++)


{
int digit = temp % 10;
temp /= 10;
digits[i] = digit;
}

int isPalindrome = 1;

for(int i = 0; i < iterations && isPalindrome; i++)


{
if (digits[i] != digits[size -i - 1]) isPalindrome = 0;
}

if (isPalindrome) cout << number << " is a palindrome";


else cout << number << " is not a palindrome";

cout << endl;


return 0;
}

program to tell if a series is arithmetic, geometric or


fibonnaci

#include <iostream>
using namespace std;

int main()
{
int series[100];
int no = 0;

cout << "Enter the value of series (-9999 to stop asking): " <<
endl;

do
{
cin >> series[no];
no++;

} while(no <= 3 || series[no - 1] != -9999);

no--;

int common_difference = series[2] - series[1];


int isArithmetic = 1;
for(int i = 0; i < no - 1 && isArithmetic; i++)
{
if (series[i + 1] - series[i] != common_difference)
isArithmetic = 0;
}

float common_ratio = (float) series[2] / series[1];


int isGeometric = 1;

for(int i = 0; i < no - 1 && isGeometric; i++)


{
if ((float) series[i + 1] / series[i] != common_ratio)
isGeometric = 0;
}

int isFibonacci = 1;

for(int i = 0; i < no - 2; i++)


{
if (series[i] + series[i + 1] != series[i + 2]) isFibonacci
= 0;
}

if (isArithmetic) cout << "The series is Arithmetic";


else if (isGeometric) cout << "The series is Geometric";
else if (isFibonacci) cout << "The series is Fibonacci";
else cout << "This is not a series";

cout << endl;


return 0;
}

find the place of a digit in a number

#include <iostream>
using namespace std;

int main()
{
int number, toFound, size;

cout << "Enter a number: ";


cin >> number;

do
{
cout << "Enter the digit who's place value is to be found:
";
cin >> toFound;

size = 0;
int temp = toFound;

while(temp > 0)
{
temp /= 10;
size++;
}

} while(toFound < 0 || size > 1);

size = 0;
int temp = number;

while(temp > 0)
{
size++;
temp /= 10;
}

int digits[20];

temp = number;

for(int i = 0; i < size; i++)


{
int digit = temp % 10;
temp /= 10;
digits[i] = digit;
}

int isFound = 0;
int index = 0;

for(int i = 0; i < size && !isFound; i++)


{
if (digits[i] == toFound)
{
isFound = 1;
index = size - i - 1;
}
}

if (isFound)
{
switch (index)
{
case 0:
cout << "unit";
break;
case 1:
cout << "tens";
break;
case 2:
cout << "hundred";
break;
case 3:
cout << "thousands";
break;
case 4:
cout << "ten thousands";
break;
case 5:
cout << "hundred thousands";
break;
case 6:
cout << "millions";
break;
case 7:
cout << "ten millions";
break;
default:
cout << "out of my range meow";
break;
}
}
else
{
cout << "digit not found";
}

cout << endl;

return 0;

Sample Input: 9873 7

Sample Output: hundred

rotation of an array

#include <iostream>
using namespace std;
int main()
{
int arr[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int newarr[15];

int rotation;
int size = 15;

do
{
cout << "How much rotation: ";
cin >> rotation;
} while(rotation < 0);

for(int i = 0; i < 15; i++)


{
int new_index = i - rotation;
if (new_index < 0) new_index += size;
newarr[new_index] = arr[i];
}

for(int i = 0; i < size; i++) cout << newarr[i] << " ";
cout << endl;

return 0;
}

a program that stores the common values of two arrays


in a new array
#include <iostream>
using namespace std;

int main()
{
int A1[10] = {0,2,4,8,10,12,14,16,18,20};
int A2[10] = {0,3,6,9,12,15,18,21,24,27};
int A3[10];

cout << "A1: ";


for(int number : A1) cout << number << " ";
cout << endl;

cout << "A2: ";


for(int number : A2) cout << number << " ";
cout << endl;

int i = 0, j = 0, k = 0;

while(i <= 10 && j <= 10)


{
if (A1[i] == A2[j])
{
A3[k] = A1[i];
i++; j++; k++;
}
else if (A1[i] < A2[j]) i++;
else j++;
}
cout << "A3: ";
for (int i = 0; i < k; i++) cout << A3[i] << " ";
cout << endl;
}

pattern: diamond

#include <iostream>
using namespace std;

void spaces(int n)
{
for(int i = 0; i < n; i++) cout << " ";
return;
}

void pluses(int n)
{
for(int i = 0; i < n; i++) cout << "+";
return;
}

int main()
{
int rows;

do
{
cout << "Enter the number of rows: ";
cin >> rows;

} while(rows <= 0);

int sp = rows - 1;
int p1 = 1;

int i = 1;

while(i <= rows)


{
spaces(sp);
pluses(p1);
sp--;
p1 += 2;
i++;
cout << endl;
}

sp += 2;
p1 -= 4;

i = 1;

while(i <= rows - 1)


{
spaces(sp);
pluses(p1);
sp++;
p1 -= 2;
i++;
cout << endl;
}

return 0;
}

pattern: hollow diamond

#include <iostream>
using namespace std;

void spaces(int n)
{
for(int i = 0; i < n; i++) cout << " ";
return;
}

void pluses(int n)
{
for(int i = 0; i < n; i++) cout << "+";
return;
}

int main()
{
int n;
do
{
cout << "Enter the number of rows: ";
cin >> n;
} while(n < 0);

int sp1 = n -1;

spaces(sp1);
pluses(1);

sp1--;

int sp2 = 1;

int i = 1;

cout << endl;

while(i <= n - 1)
{
spaces(sp1);
pluses(1);
spaces(sp2);
pluses(1);
sp1--;
sp2 += 2;
i++;

cout << endl;


}

sp1 += 2;
sp2 -= 4;

i = 1;

while(i <= n - 2)
{
spaces(sp1);
pluses(1);
spaces(sp2);
pluses(1);

sp1++;
sp2 -= 2;

i++;

cout << endl;


}

spaces(n - 1);
pluses(1);

cout << endl;

return 0;
}
pattern: numbers pyramid

#include <iostream>
using namespace std;

int main()
{
int rows;

do
{
cout << "Enter the number of rows: ";
cin >> rows;
} while(rows <= 0);

int counter = 1;

for(int i = 1; i <= rows; i++)


{
int spaces = rows - i;

// prints the spaces for each row


for(int j = 0; j < spaces; j++)
{
cout << " ";
}

for(int j = 0; j < i; j++)


{
cout << counter << " ";
counter++;
}

cout << endl;


}

return 0;
}

pattern: pascal triangle

#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter the number of rows: ";


cin >> rows;

int row = 0;

while(row < rows)


{
int spaces = rows - row - 1;
int i = 1;

while(i <= spaces)


{
cout << " ";
i++;
}

int neum = 1;

i = 1;

while(i <= row)


{
neum *= i;
i++;
}

int j = 0;

while(j <= row)


{
int deno1 = 1;
int deno2 = 1;

int i = 1;

while(i <= j)
{
deno1 *= i;
i++;
}

i = 1;

while(i <= (row - j))


{
deno2 *= i;
i++;
}

cout << neum / (deno1 * deno2) << " ";

j++;
}

cout << endl;


row++;
}

return 0;
}

a password generator

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
srand(time(NULL));
string ch;
string password;
int size;
cout << "Enter the length of the password: ";
cin >> size;
for(int i = 0; i < size; i++)
{
ch = static_cast<char>(rand() % 94 + 33);
password.append(ch);
}
cout << "Password: " << password << endl;
return 0;
}

find and replace a word from a string

#include <iostream>
#include <string.h>
using namespace std;

void replace(char text[], char find[], char rep[])


{
if (strlen(text) < strlen(find)) return;
int s2 = strlen(find);
int s3 = strlen(rep);

for(int i = 0; i < strlen(text) - s2 + 1; i++)


{
int matches = 0;
for(int j = 0; j < s2; j++)
{
if (text[i + j] == find[j])
matches++;
}
if(matches == s2)
{
if (s3 == s2)
{
for(int j = 0; j < s2; j++)
text[i + j] = rep[j];
}
else if (s3 < s2)
{
for(int j = 0; j < s3; j++)
{
text[i + j] = rep[j];
}
for(int k = 0; k < s2 - s3; k++)
{
for(int j = i + s3; j <= strlen(text); j++)
text[j] = text[j + 1];
}
}
else if (s3 > s2)
{
for(int k = 0; k < s3 - s2; k++)
{
for(int j = strlen(text) + s3 - s2; j >= i
+ s2; j--)
{
text[j] = text[j - 1];
}
}

for(int j = 0; j < s3; j++)


{
text[i + j] = rep[j];
}
}
}
}
return;
}

int main()
{
char text[400] = "Cheating is the getting of a reward for ability
by dishonest means or finding an easy way out of an unpleasant
situation. It is generally used in situations to gain unfair advantage
in a competitive situation. This broad definition will necessarily
include acts of bribery, cronyism, sleaze, nepotism and any situation
where individuals are given preference using inappropriate criteria";
char word[20];
char rep[20];

cout << text << endl << endl;

cout << "What to find: ";


cin >> word;

cout << "What to replace: ";


cin >> rep;

replace(text, word, rep);

cout << text << endl;

return 0;
}

square root of a number by babylonian method

#include <iostream>
#include <cmath>
using namespace std;

double sqrt(double num)


{
double guess = 0.0;
double tolerance = 0.0001;
while(abs(num - guess * guess) > tolerance)
{
guess += 0.0001;
}

return guess;
}

int main()
{
double num;

cout << "Enter a number: ";


cin >> num;

cout << "Sqaure Root: " << sqrt(num) << endl;


return 0;
}

sqrt of a number by Newton Ralphson Method

#include <iostream>
using namespace std;

double abs(double a, double b)


{
if (a > b) return a;
return b;
}

double sqrt(double num)


{
double x = 1;
while(abs(num - x * x) > 0.00000000001)
{
x = (x + num / x) / 2;
}
return x;
}

int main()
{
double num;
cout << "Enter a number: ";
cin >> num;
cout << sqrt(num) << endl;
return 0;
}

transpose of a matrix

#include <iostream>
using namespace std;

#define PRINT(X) cout << X << endl;

void print(int arr[][3], int r, int c)


{
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
cout << arr[i][j] << " ";
cout << endl;
}
}

void transpose(int arr[][3], int r, int c)


{
for(int i = 0; i < r; i++)
for(int j = i + 1; j < c; j++)
swap(arr[i][j], arr[j][i]);
}

int main()
{
int arr[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int r = 3, c = 3;

PRINT("Before transposing");
print(arr, r, c);
transpose(arr, r, c);
PRINT("After transposing");
print(arr, r, c);
}
a program to merge two sorted arrays into a sorted
array

#include <iostream>
using namespace std;

void merge(int A[], int m, int B[], int n, int C[])


{
// pointers to keep track of the array A and B
int i = 0; int j = 0;

// pointer to keep track of array C


int k = 0;

while(i < m && j < n)


{
if (A[i] < B[j])
{
C[k] = A[i];
i++;
k++;
}
else
{
C[k] = B[j];
j++;
k++;
}
}
// this will only run if all of the elements of array B have been
used and there are still some left in A
while(i < m)
{
C[k] = A[i];
i++;
k++;
}

// this will only run if all of the elements of array A have been
used and there are still some left in B
while(j < n)
{
C[k] = B[j];
j++;
k++;
}
}

int main()
{
int A[] = {0,4,5,5,7,9};
int B[] = {-1,0,3,6};

int m = 6, n = 4;

int C[10];

merge(A, m, B, n, C);
for(int i = 0; i < m + n; i++)
cout << C[i] << " ";
cout << endl;

return 0;
}

1. Swap the Longest and Shortest

Implement the function swapWord(…) that takes in an R×20 2D array of null-

terminated strings, finds the largest string (longest in terms of length up to the null-

terminator), the smallest string (shortest in terms of length up to the null-terminator)

and swaps their positions.

bool swapWord(char matrix[][20], int R );

This function must call two other functions:

int largest(….) that returns the row/index number of the largest word. In case of

multiple words return the row number of the first one.

int shortest(….) that returns the row/index number of the shortest word. In case of

multiple words return the row number of the first one.

Example:

Input: matrix = {“apple”, “bat”, “catapult”, “dog”, “hi” };

Output: { “apple”, “bat”, “hi”, “dog”, “catapult” };

#include <iostream>
#include <string.h>
using namespace std;

int largest(char matrix[][20], int rows)


{
int l = strlen(matrix[0]);
int l_index = 0;

for (int i = 0; i < rows; i++)


{
if (strlen(matrix[i]) > l)
{
l = strlen(matrix[i]);
l_index = i;
}
}

return l_index;
}

int shortest(char matrix[][20], int rows)


{
int s = strlen(matrix[0]);
int s_index = 0;

for (int i = 0; i < rows; i++)


{
if (strlen(matrix[i]) < s)
{
s = strlen(matrix[i]);
s_index = i;
}
}

return s_index;
}

void swapCharArr(char w1[], char w2[20])


{
char temp[20];
strcpy_s(temp, w1);

for (int i = 0; i <= strlen(w2); i++)


w1[i] = w2[i];

for (int i = 0; i < strlen(temp); i++)


w2[i] = temp[i];

void swapWord(char matrix[][20], int R)


{
int l = largest(matrix, R);
int s = shortest(matrix, R);
swapCharArr(matrix[l], matrix[s]);
return;
}

void printArr(char matrix[][20], int R)


{
for (int i = 0; i < R; i++)
cout << matrix[i] << endl;
}
int main()
{
char matrix[20][20] = { "apple", "bat", "catapult", "dog", "hi" };
int R = 5;

do
{
cout << "Enter the number of rows: ";
cin >> R;
} while (R <= 0);

for (int i = 0; i < R; i++)


{
cout << "Enter the string " << i + 1 << ": ";
cin >> matrix[i];
}

swapWord(matrix, R);
printArr(matrix, R);

system("pause");

return 0;
}

2. Sub Character matrix

Given a 2D null-terminated character array A and a 1D character array B, determine

if B is a substring of any string in matrix A.

bool MatchSubMatrix(char MatrixA[][20], int R, char B[]);

Example:

A = { “applepie”, “abananafruit”, “cherrycake”, “datebar”, “elderberryjam” };

B = { “banana” };

The function must return true since banana is a substring of abananafruit.

return true;

#include <iostream>
#include <string.h>
using namespace std;

int findWord(char word[], char rep[])


{
bool found = false;

for (int i = 0; word[i] != 0 && !found; i++)


{
int matches = 0;

for (int j = 0; rep[j] != 0 && word[i + j] != 0; j++)


{
if (word[i + j] == rep[j])
matches++;
}

if (matches == strlen(rep))
found = true;
}

return found;
}

bool MatchSubMatrix(char MatrixA[][20], int R, char B[])


{
bool found = false;

for (int i = 0; i < R && !found; i++)


{
found = findWord(MatrixA[i], B);
}

return found;
}

int main()
{
char A[20][20] = { "applepie", "abananafruit", "cherrycake",
"datebar", "elderberryjam" };
char B[] = "banana";
int R = 5;

do
{
cout << "Enter the number of rows: ";
cin >> R;
} while (R <= 0);

for (int i = 0; i < R; i++)


{
cout << "Enter the string " << i + 1 << ": ";
cin >> A[i];
}

cout << "Enter the word: ";


cin >> B;

if (MatchSubMatrix(A, R, B))
{
cout << "Found it";
}
else
{
cout << "Not found";
}

cout << endl;

system("pause");

return 0;
}
#include <iostream>
#include <string.h>
using namespace std;

// set it from 10 to 15 cuz the words didnt fit

void appendChar(char word[15], char letter)


{
int s = strlen(word);
word[s] = letter;
word[s + 1] = 0;
}

void seperate(char A[5][15], char B[5][15])


{
char word[15] = "";
int j = 0;

for (int k = 0; k < 5; k++)


{
for (int i = 0; i < strlen(A[k]); i++)
{
if (A[k][i] == ',')
{
strcpy_s(B[j], word);
j++;
strcpy_s(word, "");
}
else
{
appendChar(word, A[k][i]);
}
}
}
strcpy_s(B[j], word);

return;
}

void print(char A[5][15])


{
for (int i = 0; i < 5; i++)
{
cout << A[i] << endl;
}
}

int main()
{
char A[5][15] = { "apple,oran", "ge,seas", "on,Catterpill",
"ar,ban", "ana" };

char B[5][15];
cout << "Before: " << endl;
print(A);
seperate(A, B);

cout << "\nAfter: " << endl;


print(B);
seperate(A, B);

cout << "\n";

system("pause");

return 0;
}
#include <iostream>
using namespace std;

bool isAlpha(char ch)


{
if (ch >= 'A' && ch <= 'Z')
return true;
if (ch >= 'a' && ch <= 'z')
return true;
return false;
}

void printMatrix(const char matrix[][5], int row)


{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < 5; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

bool isVowel(char ch)


{
if (ch >= 'A' && ch <= 'Z') ch += 32;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
return true;
return false;
}

void replaceVowels(char matrix[][5], int rows)


{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < rows; j++)
{
if (isVowel(matrix[i][j]))
{
matrix[i][j] = '?';
}
}
}
}

void transpose(char matrix[][5], int row)


{
for(int i = 0; i < row; i++)
{
for(int j = i + 1; j < row; j++)
{
swap(matrix[i][j], matrix[j][i]);
}
}
}

void shiftCols(char matrix[][5], int row, int c1, int c2)


{
for(int i = 0; i < row; i++)
{
swap(matrix[i][c1], matrix[i][c2]);
}
}

void rotateMatrix90Clockwise(char matrix[][5], int rows)


{
replaceVowels(matrix, rows);
transpose(matrix, rows);

int mid = rows / 2;

int i = 0;

while(i < mid)


{
shiftCols(matrix, rows, i, rows - i - 1);
i++;
}
}

int main()
{
char matrix[5][5] = {{'A','B','C','D','E'},
{'F','G','H','I','J'},
{'K','L','M','N','O'},
{'P','Q','R','S','T'},
{'U','V','W','X','Y'}};

int rows = 5;

for(int i = 0; i < rows; i++)


{
for(int j = 0; j < rows; j++)
{
do
{
cout << "Enter for row " << i << " & col " <<
j << ": ";
cin >> matrix[i][j];
} while(!isAlpha(matrix[i][j]));
}
}

cout << "\nOriginal Matrix:\n\n";


printMatrix(matrix, 5);
rotateMatrix90Clockwise(matrix, rows);

cout << "\nRotated Matrix:\n\n";


printMatrix(matrix, 5);

#include <iostream>
using namespace std;

int strlen(char word[])


{
int i = 0;
for(; word[i] != 0; i++);
return i;
}

void printMatrix(const char matrix[][9], int rows, int cols)


{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

bool searchHorizontal(const char data[][9], int rows, int cols, char


word[], int& startRow, int& startCol, int& endRow, int& endCol)
{
bool found = false;

for(int i = 0; i < rows && !found; i++)


{
for(int j = 0; j < cols && !found; j++)
{
int matches = 0;

int k;

for(k = 0; word[k] != 0 && (j + k < cols); k++)


{
if (word[k] == data[i][j + k])
{
matches++;
}
}

if (matches == strlen(word))
{

startRow = i;
endRow = i;
startCol = j;
endCol = j + strlen(word) - 1;
found = true;
}
}
}

return found;
}

bool searchReverseHorizontal(const char data[][9], int rows, int cols,


char word[], int& startRow, int& startCol, int& endRow, int& endCol)
{
bool found = false;

for(int i = 0; i < rows && !found; i++)


{
for(int j = cols - 1; j > 0 && !found; j--)
{
int matches = 0;

for(int k = 0; word[k] != 0 && (j - k >= 0); k++)


{
if (word[k] == data[i][j - k])
matches++;
}

if (matches == strlen(word))
{
startRow = i;
endRow = i;
startCol = j;
endCol = j - strlen(word) + 1;
found = true;
}
}
}

return found;
}

bool searchVertical(const char matrix[][9], int rows, int cols, char


word[], int& startRow, int& startCol, int& endRow, int& endCol)
{
bool found = false;

for(int i = 0; i < rows && !found; i++)


{
for(int j = 0; j < cols && !found; j++)
{
int matches = 0;

for(int k = 0; word[k] != 0 && (i + k < rows); k++)


{
if (word[k] == matrix[i + k][j])
{
matches++;
}
}

if (matches == strlen(word))
{
startRow = i;
endRow = i + strlen(word) - 1;
startCol = j;
endCol = j;
found = true;
}
}
}

return found;
}

bool searchReverseVertical(const char matrix[][9], int rows, int cols,


char word[], int& startRow, int& startCol, int& endRow, int& endCol)
{
bool found = false;

for(int i = rows - 1; i >= 0 && !found; i--)


{
for(int j = 0; j < cols && !found; j++)
{
int matches = 0;

for(int k = 0; word[k] != 0 && (i - k >= 0); k++)


{
if (word[k] == matrix[i - k][j])
{
matches++;
}
}

if (matches == strlen(word))
{
startRow = i;
endRow = i - strlen(word) + 1;
startCol = j;
endCol = j;
found = true;
}
}
}

return found;
}

bool searchWord(char matrix[][9], int rows, int cols, char word[], int&
startRow, int& startCol, int& endRow, int& endCol)
{
if (searchHorizontal(matrix, rows, cols, word, startRow, startCol,
endRow, endCol))
return true;
if (searchReverseHorizontal(matrix, rows, cols, word, startRow,
startCol, endRow, endCol))
return true;
if (searchVertical(matrix, rows, cols, word, startRow, startCol,
endRow, endCol))
return true;
if (searchReverseVertical(matrix, rows, cols, word, startRow,
startCol, endRow, endCol))
return true;

return false;
}

void replaceWordHorizontal(char matrix[][9], char rep[], int startRow, int


startCol, int endCol)
{
int i;
int k;
for(i = startCol, k = 0; i <= endCol && rep[k] != 0; i++, k++)
{
matrix[startRow][i] = rep[k];
}

while(i <= endCol)


{
matrix[startRow][i] = '*';
i++;
}
}

void replaceWordReverseHorizontal(char matrix[][9], char rep[], int


startRow, int startCol, int endCol)
{
int i;
int k;
for(i = startCol, k = 0; i >= endCol && rep[k] != 0; i--, k++)
{
matrix[startRow][i] = rep[k];
}

while(i >= endCol)


{
matrix[startRow][i] = '*';
i--;
}

void replaceWordVertical(char matrix[][9], char rep[], int startCol, int


startRow, int endRow)
{

int i;
int k;
for(i = startRow, k = 0; i <= endRow && rep[k] != 0; i++, k++)
{
matrix[i][startCol] = rep[k];
}

while(i <= endRow)


{
matrix[i][startCol] = '*';
i++;
}
}

void replaceWordReverseVertical(char matrix[][9], char rep[], int


startCol, int startRow, int endRow)
{

int i;
int k;
for(i = startRow, k = 0; i >= endRow && rep[k] != 0; i--, k++)
{
matrix[i][startCol] = rep[k];
}

while(i >= endRow)


{
matrix[i][startCol] = '*';
i--;
}
}

void replaceWord(char matrix[][9], int rows, int cols, char word[], char
rep[])
{
int startRow, startCol, endRow, endCol;

while(searchWord(matrix, rows, cols, word, startRow, startCol,


endRow, endCol))
{
if (startRow == endRow && startCol <= endCol)
replaceWordHorizontal(matrix, rep, startRow,
startCol, endCol);
else if (startRow == endRow && endCol <= startCol)
replaceWordReverseHorizontal(matrix, rep, startRow,
startCol, endCol);
else if (startCol == endCol && startRow <= endRow)
replaceWordVertical(matrix, rep, startCol, startRow,
endRow);
else if (startCol == endCol && endRow <= startRow)
replaceWordReverseVertical(matrix, rep, startCol,
startRow, endRow);
}
}

int main()
{
char matrix[9][9];

int rows = 3;
int cols = 4;

char word[5];
char rep[5];

cout << "Enter rows: ";


cin >> rows;

cout << "Enter cols: ";


cin >> cols;

for(int i = 0; i < rows; i++)


{
for(int j = 0; j < cols; j++)
{
cout << "Enter for row " << i << " & col " << j <<
": ";
cin >> matrix[i][j];
}
}

cout << "Enter the word to find: ";


cin >> word;
cout << "Etner the word to replace: ";
cin >> rep;

cout << "\n\nOriginal Matrix:\n\n";


printMatrix(matrix, rows, cols);

replaceWord(matrix, rows, cols, word, rep);

cout << "\n\nChanged Matrix:\n\n";


printMatrix(matrix, rows, cols);
}
#include <iostream>
using namespace std;

void printMatrix(int matrix[][5], int rows)


{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < 5; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
void shiftRows(int matrix[][5], int rows)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < 5; j++)
{
matrix[i][j] *= (i + 1);
}
}
}

int main()
{
int matrix[5][5], rows = 5;

do
{
cout << "Enter the number of rows: ";
cin >> rows;
} while(rows <= 0);

for(int i = 0; i < rows; i++)


{
for(int j = 0; j < 5; j++)
{
cout << "Enter for matrix row " << i << " & col " <<
j << ": ";
cin >> matrix[i][j];
}
}

cout << "\nOriginal Matrix:\n\n";


printMatrix(matrix, rows);

shiftRows(matrix, rows);

cout << "\nCiphered Matrix:\n\n";


printMatrix(matrix, rows);

return 0;
}
#include <iostream>
using namespace std;

double stonum(char num[])


{
for (int i = 0; num[i] != 0; i++)
{
if (!((num[i] >= '0' && num[i] <= '9') || (num[i] == '.')
|| (num[i] == '-' && i == 0)))
return -1;
}

int count = 0;

for (int i = 0; num[i] != 0 && count < 2; i++)


{
if (num[i] == '.')
count++;
}

if (count > 1)
return -1;

int length = 0;

for (int i = 0; num[i] != 0; i++)


{
if (num[i] != '.' && num[i] != '-')
length++;
}

double n = 0;

for (int i = 0; num[i] != 0; i++)


{
if (num[i] != '.' && num[i] != '-')
{
n *= 10;
n += num[i] - 48;
}
}
if (num[0] == '-')
n *= -1;

int decimalIndex = -1;

for (int i = 0; num[i] != 0 && decimalIndex == -1; i++)


{
if (num[i] == '.')
decimalIndex = i;
}

decimalIndex = length - decimalIndex;

if (num[0] == '-')
decimalIndex++;

int mod = 1;

if (decimalIndex > -1)


{
for (int i = 0; i < decimalIndex; i++)
mod *= 10;
}

n /= mod;

return n;
}

int main()
{
char num[20];

cout << "Enter a number: ";


cin >> num;

double n = stonum(num);

if (n == -1)
cout << "Error!!!~!!!";
else
cout << "The number is " << n;

cout << endl;


return 0;
}

#include <iostream>
using namespace std;

bool isTrigger(char t[], char ch)


{
bool found = false;

if (ch >= 'a' && ch <= 'z')


ch -= 32;

for (int i = 0; t[i] != 0 && !found; i++)


{
if (t[i] == ch)
found = true;
}
return found;
}

int lastLetter(char text[], int idx)


{
int i;
for (i = idx; text[i] != ' ' && text[i] != 0; i++);
return --i;
}

bool isNum(char ch)


{
if (ch >= '0' && ch <= '9')
return true;
return false;
}

int sumBetween(char text[], int i, int j)


{
int sum = 0;

for (int k = i; k <= j; k++)


{
if (isNum(text[k]))
sum += text[k] - 48;
}

return sum;
}

void my_swap(char& a, char& b)


{
char temp = a;
a = b;
b = temp;
}

void reverse(char text[], int i, int j)


{
int mid = (i + j) / 2;

for (int k = i, z = j; k <= mid; k++, j--)


{
my_swap(text[k], text[j]);
}
}

void Decode(char text[], char Triggers[], int key)


{
for (int i = 0; text[i] != 0; i++)
{
if (isTrigger(Triggers, text[i]))
{
int last = lastLetter(text, i);

if (isNum(text[last]))
{
int sum = sumBetween(text, i, last);
if (sum > key)
{
reverse(text, i, last);
}
}

}
}
}

int main()
{
char message[200] = "Bi9llio6ns1 of messages contain Qco3de9 and
Myst9erious3 patterns, but only extr9a5 stuff M8encoded4 and Qdeep7 have
secrets.";
int key = 10;
char Triggers[6] = "BQM";

cout << "Enter the message: ";


gets_s(message);

cout << "Enter the key: ";


cin >> key;

cout << "Enter the triggers: ";


cin >> Triggers;

Decode(message, Triggers, key);

cout << message << endl;

system("pause");
return 0;
}
#include <iostream>
using namespace std;

int length(char ch[])


{
int i;
for (i = 0; ch[i] != 0; i++);
return i;
}

void appendUsername(char password[], char username[], int currentLength,


int desiredLength)
{
int i = currentLength;
int j = 0;
while (i < desiredLength)
{
password[i] = username[j];
i++; j++;
}
}

int uppercaseCount(char text[])


{
int count = 0;
for (int i = 0; text[i] != 0; i++)
{
if (text[i] >= 'A' && text[i] <= 'Z')
count++;
}
return count;
}

int lowercaseCount(char text[])


{
int count = 0;
for (int i = 0; text[i] != 0; i++)
{
if (text[i] >= 'a' && text[i] <= 'z')
count++;
}
return count;
}

int digitCount(char password[])


{
int count = 0;
for (int i = 0; password[i] != 0; i++)
{
if (password[i] >= '0' && password[i] <= '9')
count++;
}

return count;
}

void caseMagic(char password[])


{
if (password[0] >= 'a' && password[0] <= 'z')
{
password[0] -= 32;
}

for (int i = 1; password[i] != 0; i++)


{
if (password[i] >= 'A' && password[i] <= 'Z')
password[i] += 32;
}
}

void appendDigit(char password[])


{
int l = length(password);
password[l - 1] = l + 48;
}

int find(char pass[], char s[])


{
int l1 = length(pass);
int l2 = length(s);

for (int i = 0; i < l1; i++)


{
int flag = 0;
for (int j = 0; j < l2; j++)
{
if (pass[i + j] == s[j])
flag++;
}
if (flag == l2)
{
return i;
}
}

return -1;
}

void replace(char password[], int i, int l)


{

for (int k = i; k < i + l; k++)


{
password[k] = 'X';
}
}

bool isPalindrome(char password[])


{
int l = length(password);
int mid = l / 2;

for (int i = 0, j = l - 1; i < mid; i++, j--)


{
if (password[i] != password[j])
return false;
}
return true;
}

void changeMiddle(char password[])


{
int l = length(password);

int change = l / 2 + 1;

password[change]++;

int PasswordModifications(char username[], char password[], char


forbidden[], int minChars)
{
int mods = 0;

int l = length(password);

if (l < minChars)
{
appendUsername(password, username, l, minChars);
mods++;
}

if (uppercaseCount(password) < 1 || lowercaseCount(password) < 1)


{
caseMagic(password);
mods++;
}

if (digitCount(password) == 0)
{
appendDigit(password);
mods++;
}

if (find(password, forbidden)!= -1)


{
int i = find(password, forbidden);
replace(password, i, length(forbidden));
mods++;
}

if (isPalindrome(password))
{
changeMiddle(password);
mods++;
}

return mods;
}

int main()
{
char username[20] = "Bob_K";
char password[20] = "abcba";

char forbiddenString[5] = "Abc";


int minChars = 5;

cout << "Password: ";


cin >> password;

cout << "Username: ";


cin >> username;

cout << "Forbidden: ";


cin >> forbiddenString;

cout << "min chars: ";


cin >> minChars;

int mod = PasswordModifications(username, password,


forbiddenString, minChars);

cout << password << endl;


cout << "Modifications: " << mod << endl;

system("pause");
return 0;
}

Write a function that swaps the values of three variables in a circular manner (i.e., a gets b’s

value, b gets c’s value, and c gets a’s value). The user-defined function should not use 4th

variable for swapping and the main function should display the values after swapping.

Example Input:

Enter three numbers: 10 20 30

Example Output:

Before swap: a = 10, b = 20, c = 30

After swap: a = 20, b = 30, c = 10


#include <iostream>
using namespace std;

void superswap(int& a, int& b, int& c)


{
a = a + b;
b = a - b;
a = a - b;

b = b + c;
c = b - c;
b = b - c;
}

int main()
{
int a, b, c;

cout << "Enter the value of a: ";


cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;

cout << "Before swap: a = " << a << ", b = " << b << ", c = " << c
<< endl;

superswap(a, b, c);

cout << "After swap: a = " << a << ", b = " << b << ", c = " << c
<< endl;

system("pause");
return 0;
}

Note: This code is the property of *****cyberpatcher. U can use it freely as u want. There can be some
mistakes. I’m a human after all.

You might also like