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

CPP Practical List 1 To 10

The document contains multiple C++ programs demonstrating various functionalities, including calculating the sum of a series, removing duplicates from an array, counting character frequencies, and performing string manipulations. It also includes implementations for finding the GCD both recursively and iteratively, as well as a matrix class that supports addition, multiplication, and transposition of matrices. Each program is designed to be user-interactive, prompting for input and displaying results accordingly.

Uploaded by

shchimanocha2006
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 views6 pages

CPP Practical List 1 To 10

The document contains multiple C++ programs demonstrating various functionalities, including calculating the sum of a series, removing duplicates from an array, counting character frequencies, and performing string manipulations. It also includes implementations for finding the GCD both recursively and iteratively, as well as a matrix class that supports addition, multiplication, and transposition of matrices. Each program is designed to be user-interactive, prompting for input and displaying results accordingly.

Uploaded by

shchimanocha2006
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
You are on page 1/ 6

1. Sum of Series (S = 1 - 1/2^2 + 1/3^3 - ...

+ 1/n^n)

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

int main(int argc, char *argv[]) {


int n;
if (argc == 2) {
n = atoi(argv[1]);
} else {
cout << "Enter number of terms (n): ";
cin >> n;
}
double sum = 1.0;
for (int i = 2; i <= n; i++) {
if (i % 2 == 0)
sum -= 1.0 / pow(i, i);
else
sum += 1.0 / pow(i, i);
}
cout << "Sum = " << sum << endl;
return 0;
}

2. Remove duplicates from array

#include <iostream>
using namespace std;

int main() {
int n;
cout << "Enter size of array: ";
cin >> n;
int arr[n], temp[n], k = 0;
cout << "Enter elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];

for (int i = 0; i < n; i++) {


bool found = false;
for (int j = 0; j < k; j++) {
if (arr[i] == temp[j]) {
found = true;
break;
}
}
if (!found) temp[k++] = arr[i];
}

cout << "Array without duplicates: ";


for (int i = 0; i < k; i++) cout << temp[i] << " ";
return 0;
}

3. Frequency of characters from command line

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

int main(int argc, char *argv[]) {


int freq[26] = {0};
for (int i = 1; i < argc; i++) {
for (int j = 0; argv[i][j]; j++) {
char ch = tolower(argv[i][j]);
if (ch >= 'a' && ch <= 'z')
freq[ch - 'a']++;
}
}

cout << "Character Frequencies:\n";


for (int i = 0; i < 26; i++) {
if (freq[i])
cout << char('a' + i) << ": " << freq[i] << endl;
}
return 0;
}

4. Menu-driven string manipulation (no inbuilt functions)

#include <iostream>
using namespace std;

void showAddress(char *str) {


for (int i = 0; str[i]; i++)
cout << "Address of " << str[i] << ": " << (void*)&str[i] << endl;
}

void concat(char *a, char *b, char *res) {


int i = 0;
while (a[i]) {
res[i] = a[i];
i++;
}
int j = 0;
while (b[j]) {
res[i++] = b[j++];
}
res[i] = '\0';
}

bool compare(char *a, char *b) {


int i = 0;
while (a[i] && b[i]) {
if (a[i] != b[i]) return false;
i++;
}
return a[i] == b[i];
}

int length(char *str) {


int len = 0;
while (*str++) len++;
return len;
}

void toUppercase(char *str) {


for (int i = 0; str[i]; i++) {
if (str[i] >= 'a' && str[i] <= 'z')
str[i] -= 32;
}
}

void reverse(char *str) {


int len = length(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}

void insertString(char *a, char *b, int pos, char *res) {


int i = 0;
for (i = 0; i < pos; i++) res[i] = a[i];
int j = 0;
while (b[j]) res[i++] = b[j++];
j = pos;
while (a[j]) res[i++] = a[j++];
res[i] = '\0';
}

int main() {
char str1[100], str2[100], result[200];
cout << "Enter string 1: ";
cin >> str1;
cout << "Enter string 2: ";
cin >> str2;
int choice, pos;
do {
cout << "\n1.Show
Address\n2.Concat\n3.Compare\n4.Length\n5.Uppercase\n6.Reverse\n7.Insert\n8.Exit\nChoice
: ";
cin >> choice;
switch (choice) {
case 1: showAddress(str1); break;
case 2: concat(str1, str2, result); cout << "Concat: " << result << endl;
break;
case 3: cout << (compare(str1, str2) ? "Equal" : "Not Equal") << endl;
break;
case 4: cout << "Length: " << length(str1) << endl; break;
case 5: toUppercase(str1); cout << "Upper: " << str1 << endl; break;
case 6: reverse(str1); cout << "Reversed: " << str1 << endl; break;
case 7: cout << "Position: "; cin >> pos; insertString(str1, str2, pos,
result); cout << "Inserted: " << result << endl; break;
}
} while (choice != 8);
return 0;
}

7a. GCD with Recursion (User Input)

#include <iostream>
using namespace std;

int gcdRecursive(int a, int b) {


if (b == 0)
return a;
return gcdRecursive(b, a % b);
}

int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "GCD (recursive) of " << a << " and " << b << " is: " << gcdRecursive(a, b)
<< endl;
return 0;
}

7b. GCD without Recursion (User Input)

#include <iostream>
using namespace std;

int gcdIterative(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "GCD (iterative) of " << a << " and " << b << " is: " << gcdIterative(a, b)
<< endl;
return 0;
}

8. Matrix Class Menu Program

#include <iostream>
using namespace std;

class Matrix {
int a[10][10], r, c;
public:
void read() {
cout << "Enter rows and columns: ";
cin >> r >> c;
cout << "Enter elements:
";
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
cin >> a[i][j];
}
void display() {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
cout << a[i][j] << " ";
cout << endl;
}
}
Matrix add(Matrix m) {
Matrix temp;
if (r != m.r || c != m.c) {
cout << "Matrix dimensions do not match for addition.
";
temp.r = temp.c = 0;
} else {
temp.r = r;
temp.c = c;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
temp.a[i][j] = a[i][j] + m.a[i][j];
}
return temp;
}
Matrix multiply(Matrix m) {
Matrix temp;
if (c != m.r) {
cout << "Matrix dimensions incompatible for multiplication.
";
temp.r = temp.c = 0;
} else {
temp.r = r;
temp.c = m.c;
for (int i = 0; i < r; i++)
for (int j = 0; j < m.c; j++) {
temp.a[i][j] = 0;
for (int k = 0; k < c; k++)
temp.a[i][j] += a[i][k] * m.a[k][j];
}
}
return temp;
}
Matrix transpose() {
Matrix temp;
temp.r = c;
temp.c = r;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
temp.a[j][i] = a[i][j];
return temp;
}
};
int main() {
Matrix m1, m2, result;
int choice;
cout << "Enter first matrix:
";
m1.read();
cout << "Enter second matrix:
";
m2.read();

do {
cout << "\nMenu:\n1. Add\n2. Multiply\n3. Transpose First Matrix\n4. Exit\nEnter
choice: ";
cin >> choice;
switch (choice) {
case 1:
result = m1.add(m2);
cout << "Resultant Matrix:\n";
result.display();
break;
case 2:
result = m1.multiply(m2);
cout << "Resultant Matrix:\n";
result.display();
break;
case 3:
result = m1.transpose();
cout << "Transpose Matrix:\n";
result.display();
break;
}
} while (choice != 4);

return 0;
}

You might also like