files and stream creating writing and displaying
#include <iostream>
#include <fstream>
using namespace std;
int main() {
char data[100];
// Writing to file
ofstream outfile;
outfile.open("afile.dat");
cout << "writing into the file" << endl;
cout << "enter your name: ";
cin.getline(data, 100);
outfile << data << endl;
cout << "enter your age: ";
cin.getline(data, 100);
outfile << data << endl;
outfile.close();
// Reading from file
ifstream infile;
infile.open("afile.dat");
cout << "reading from file..." << endl;
infile.getline(data, 100); // Read name
cout << "Name: " << data << endl;
infile.getline(data, 100); // Read age
cout << "Age: " << data << endl;
infile.close();
return 0;
}
file handling
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outfile("lol");
if (outfile.is_open()) {
outfile << "first line "<<endl;
outfile << "second line" << endl;
outfile.close();
cout << "data has been written ";
}
else
cout << "failed to write " << endl;
return 0;
}
Oops
#include <iostream>
using namespace std;
class Circle {
int radius;
public:
int const pi = 3.1426;
Circle(int rad) {
radius = rad;
}
int area() {
return radius * pi * radius;
}
int peri() {
return 2 * pi * radius;
}
};
int main() {
Circle circ1(5);
cout << "the area is " << circ1.area() << endl;
cout << "the perimeter is " << circ1.peri()<<endl;
return 0;
}
oops
#include <iostream>
using namespace std;
class yes {
int length;
public:
void set(int len) {
length = len;
}
void get() {
cout << "get length" << endl;
cin >> length;
}
yes() {
cout << "just to cout" << endl;
}
yes(int x) {
cout << "the object with one value " << x << endl;
}
yes(int y, int j) {
cout << "object with two values " << endl;
}
~yes() {
cout << "deleting object " << endl;
}
};
int main() {
yes lol;
yes lol1(4);
yes lol2(3, 5);
lol.set(8);
lol.get();
return 0;
}
oops inheritance
#include <iostream>
using namespace std;
class shape {
protected:
int width, height;
public:
shape() {
cout << "object being created" << endl;
}
void set(int wid,int hei) {
width = wid;
height = hei;
};
class rect :public shape {
public:
int area() {
return (width * height);
}
};
int main() {
rect rec1;
rec1.set(3, 4);
cout<<"the area is "<<rec1.area();
return 0;
oop multilevel inheritance
#include <iostream>
using namespace std;
class circ {
protected:
int radius;
public:
void set(int rad) {
radius = rad;
cout << "this is a circle" << endl;
}
};
class shape :public circ {
public:
void disp() {
cout << "this is a shape and its radius is "<<radius << endl;
}
};
class poly :public shape {
public:
void display() {
cout << "this is a poly" << endl;
}
};
int main() {
poly pol1;
pol1.display();
pol1.set(6);
pol1.disp();
return -1;
reverse a string
#include <iostream>
using namespace std;
void rev(string str) {
for (int i = str.size() - 1;i >= 0;i--) {
cout << str[i];
}
}
int main() {
string str = "aasjor";
rev(str);
return 0;
}
shifting every char of string to next char
#include <iostream>
using namespace std;
void repl(string str) {
for (int i = 0;i < str.size();i++) {
if (str[i] >= 'a' || str[i] <= 'z' || str[i] >= 'A' || str[i] <= 'Z') {
int code = int(str[i] + 1);
str[i] = char(code);
}
cout << str[i];
}
}
int main() {
string str = "aashir";
repl(str);
return 0;
}
linear search
#include <iostream>
using namespace std;
void linear(int arr[], int size,int key) {
int count = 0;
for (int i = 0;i < size;i++) {
if (arr[i] == key) {
cout << "the key has been found " << endl;
count++;
}
}cout << "it was found " << count << " times " << endl;
}
int main() {
int arr[] = { 4,7,3,46,89,1,3,1 };
int size = sizeof(arr) / sizeof(arr[0]);
linear(arr, size, 1);
bubble sort
#include <iostream>
using namespace std;
void bubble(int arr[], int size) {
for (int i = 0;i < size;i++) {
int swaps = 0;
for (int j = 0;j < size - i - 1;j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
swaps++;
}
}if (!swaps) {
break;
}
}
}
int main() {
int arr[] = { 3,6,22,7,85,2 };
int size = sizeof(arr) / sizeof(arr[0]);
bubble(arr, size);
for (int i = 0;i < size;i++) {
cout << arr[i]<<" ";
binary search the mid one
#include <iostream>
using namespace std;
void binary(int arr[], int low, int high, int key) {
int mid = (low + high) / 2;
if (low <= high) {
if (arr[mid] == key) {
cout << "element found at index " << mid << endl;
}
else if(arr[mid]>key) {
binary(arr, low, mid - 1, key);
}
else if (arr[mid] < key) {
binary(arr, mid + 1, high, key);
}
else if (low > high) {
cout << "unsuccessful search" << endl;
}
}
}
int main() {
int arr[] = { 3,6,7,9,89,788 };
int size = sizeof(arr) / sizeof(arr[0]);
int low = 0;
int high = size - 1;
int key = 788;
binary(arr, low, high, key);
return 0;
}
insertion osrt
#include <iostream>
using namespace std;
void insert(int arr[], int size) {
int key, j;
for (int i = 1;i < size;i++) {
key = arr[i];
j = i;
while (j > 0 && arr[j - 1] > key) {
arr[j] = arr[j - 1];
j--;
}arr[j] = key;
}
}
int main() {
int arr[] = { 3,6,74,346,897,1 };
int size = sizeof(arr) / sizeof(arr[0]);
insert(arr, size);
for (int i = 0;i < size;i++) {
cout << arr[i] << " ";
}
return 69;
seletion sort
#include <iostream>
using namespace std;
void select(int arr[], int size) {
for (int i = 0;i < size - 1;i++) {
for (int j = i + 1;j < size;j++) {
if (arr[j] < arr[i]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main() {
int arr[] = { 5,3,2,9,78,94,3 };
int size = sizeof(arr) / sizeof(arr[0]);
select(arr, size);
for (int i = 0;i < size;i++) {
cout << arr[i] << " ";
}return 69;
}
array ,sum of smallest and largest element
#include <iostream>
using namespace std;
int sum(int arr[], int size) {
int small = arr[0];
int large = arr[0];
for (int i = 0;i < size;i++) {
if (arr[i] > large) {
large = arr[i];
}if (arr[i] < small) {
small = arr[i];
}
}return small + large;
}
int main() {
int arr[] = { 3,56,32,45,87,22 };
int size = sizeof(arr) / sizeof(arr[0]);
cout << "sum is " << sum(arr, size) << endl;
return 0;
most occurring element in array
#include <iostream>
using namespace std;
void most(int arr[], int size) {
int mostocc = arr[0];
int maxcount = 0;
for (int i = 0;i< size;i++) {
int count = 0;
for (int j = 0;j < size;j++) {
if (arr[j] == arr[i]) {
count++;
}if (count > maxcount) {
maxcount = count;
mostocc = arr[i];
}
}
}
cout << "the most repeated element is " << mostocc << " and its is repeated
is " << maxcount << "times" << endl;
}
int main() {
int arr[] = { 3,4,674,345,67,3,44,3 };
int size = sizeof(arr) / sizeof(arr[0]);
most(arr, size);
return 0;
array to segregate 1s and 0s
#include <iostream>
using namespace std;
void sep(int arr[], int n = 9) {
int result[9];
int count = 0;
for (int i = 0;i < n;i++) {
if (arr[i] == 1) {
result[count] = arr[i];
count++;
}
}for (int j = 0;j < n;j++) {
if (arr[j] == 0) {
result[count] = arr[j];
count++;
}
}for (int l = 0;l < n;l++) {
cout << result[l] << " ";
}
}
int main() {
int arr[9] = { 1,0,0,0,1,1,0,1,1 };
sep(arr, 9);
return 0;
}
capitalize first letter
#include <iostream>
#include <string>
using namespace std;
string capital(string str) {
if (str.length() > 0 && str[0] >= 'a' && str[0] <= 'z') {
str[0] = str[0] - 32;
}
for (int i = 1;i < str.length();i++) {
if (str[i - 1] == ' ' && str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32;
}
}return str;
}
int main() {
string str1;
cout << "enter a string " << endl;
getline(cin, str1);
cout << capital(str1);
return 69;
rearranging alphabets ina string
#include <iostream>
using namespace std;
void check(string str) {
string letters = "";
for (int i = 0;i < str.length();i++) {
if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z') {
letters += str[i];
}
}for (int l = 0;l < letters.size();l++) {
int swaps = 0;
for (int k = 0;k < letters.size() - l - 1;k++) {
if (letters[k] > letters[k + 1]) {
int temp = letters[k + 1];
letters[k + 1] = letters[k];
letters[k] = temp;
swaps++;
}
}if (!swaps) {
break;
}
}for (int h = 0;h < letters.size();h++) {
cout << letters[h] << " ";
}
}
int main() {
string str = "aashir";
check(str);
return 9;
}
recursive code to find the max and min in array
#include<iostream>
#include<string>
using namespace std;
int large(int arr[], int low, int high) {
if (low == high) {
return arr[low];
} int mid = (low + high) / 2;
int max1 = large(arr, low, mid);
int max2 = large(arr, mid + 1, high);
return (max1 > max2 ? max1 : max2);
}
int small(int arr[], int low, int high) {
if (low == high) {
return arr[low];
}int mid = (low + high) / 2;
int min1 = small(arr, low, mid);
int min2 = small(arr, mid + 1, high);
return (min1 < min2 ? min1 : min2);
}
int main() {
int a[] = { 5,4,3,7,8,9,22 };
int size = sizeof(a) / sizeof(a[0]);
int max = large(a, 0, size - 1);
int min = small(a, 0, size - 1);
cout << "max: " << max << endl;
cout << "min: " << min << endl;
return 0;
}
exception handling
#include <iostream>
using namespace std;
double division(int a, int b) {
if (b == 0) {
throw "division by zero condition";
}return(a / b);
}
int main() {
int x = 10;
int y = 0;
int z = 0;
try {
z = division(x, y);
cout << z << endl;
}
catch(const char *msg){
cerr << msg << endl;
}
return 0;