CPP Book
CPP Book
Function
1. Write a program that reads two integer numbers
from a user and call a function to add these two
numbers and call another function to sub these
numbers using local and global variables.
int sum (int x, int y)
{
return x+y;
}
int diff (int x, int y)
{
return x-y;
}
int main ()
{
int x,y;
cin >> x>>y;
cout << diff(x,y)<<" "<< sum(x,y);
}
\\
int x,y;
int sum ()
{
return x+y;
}
int diff ()
{
return x-y;
}
int main ()
{
#include <iostream>
using namespace std;
int main() {
int num;
if (num <= 0) {
cout << "Invalid input! Please enter a positive
number.\n";
return 0; // Exit program if the input is invalid
}
return 0;
}
#include <iostream>
using namespace std;
double firstFraction( )
{
double sum=0,product=1 ; int x ;
for(x = 3 ; x <= 6 ; x++)
{ sum += x ; }
for(x = 9 ; x <= 15 ; x+=2)
{ product*= x ;}
return sum/product;
}
double secondFraction( )
{
double sum=0,product=1 ; int x ;
for(x = 7 ; x <= 19 ; x+=3)
{product*= x ;}
for(x = 10 ; x <= 40 ; x+=5)
{ sum += x ; }
return product / sum ;
}
int main ()
{
cout << firstFraction( ) - secondFraction( );
}
int main ()
{int A,B;
cin >> A>>B;
cout << sumrangge( A, B) ;
}
#include <iostream>
using namespace std;
int main()
{
int a[5];
set(a, 5);
print(a, 5);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int x = 10, y = 20;
swap(x, y);
cout << "x= " << x << " y= " << y << endl;
return 0;
}
//x= 10 y= 20 Output
///
#include <iostream>
int z = x;
x = y;
y = z;
int main()
{
int x = 10, y = 20;
swap(x, y);
cout << "x= " << x << " y= " << y << endl;
return 0;
//x= 20 y= 10 output
As you can see, the result of calling the swap() function is different depending on
………………………………………………………………….
#include <iostream>
x += 1;
y += 2;
int main()
fun(k, r);
cout << "K= " << k << " R= " << r << endl;
return 0;
//K= 51 R= 12
LINEAR SEARCH
Write a program to fill an array with size 20 by positive random.
numbers between 5 and 100.Then read a key number from the
user and search for this key number using linear search.
int main ()
{
int size=20;
int arr[size];
int key;
fillarray(arr,size);
disarray(arr,size);
cin>> key;
linearsearch (arr,size,key);
if (linearsearch (arr,size,key) != -1) {
cout << "Number " << key << " found at index " <<
linearsearch (arr,size,key)<< endl;
} else {
cout << "Number " << key << " not found in the array." << endl;
}
}
b. Write a function to fill an array with random numbers.
Write another function to search for a number in array
using linear search.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
const int size = 20;
int arr[size];
int key;
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
using namespace std;
int main()
{
int arr[] = { 90,10,40,70,5 };
int n = sizeof(arr) / sizeof(arr[0]);//5*4=20 => 20/4 =>5
int num;
cout << "Enter an Integer :";
cin >> num;
return 0;
}
Binary Search
#include<iostream>
using namespace std;
int binarySearch(int arr[], int l, int h, int element)
{
while (l <= h) {
int m = (l + h) / 2;
if (arr[m] == element)
return m;
else
l = m + 1;
}
return -1;
}
/*
int binarySearchRec(int arr[], int l, int h, int element)
{
if (h >= l) {
int mid = (l + h) / 2;
if (arr[mid] == element)
return mid;
int main()
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int num;
cout << "Enter an Integer :";
cin >> num;
Recursive
function calculates the sum of the first n natural numbers.
#include <iostream>
using namespace std;
int sum(int n)
{
if (n == 1)
return 1;
else
return n + sum(n - 1);
}
int main()
{
cout << sum(5) << endl;
return 0;
}
numbers to sum.
#include <iostream>
if (x == y)
return x;
else
int main()
return 0;
—--------------------------------------------
*****
****
***
**
#include <iostream>
void f(int n)
if (n < 0)
return;
else
{
cout << "*";
f(n - 1);
int main()
f(5);
return 0;
}
recursive function that calculates the factorial of a
number
#include <iostream>
int fact(int n)
if (n == 0 || n == 1)
return 1;
else
int main()
{
cout << fact(5) << endl;
return 0;
Using loops
int main(void)
int n, k, factorial;
cin >> n;
factorial = 1;
factorial = factorial * k;
}
cout << "Factorial " << n << " = " << factorial << "\n";
return 0;
of zeros
user
#include <iostream>
int countZeros(int n) {
if (n == 0) return 1; // Base case for input 0
int main() {
int num;
value
#include <iostream>
int m = (l + h) / 2;
if (arr[m] == element)
int main() {
int arr[] = {2, 3, 4, 10, 40};
int num;
if (result == -1)
cout << "The number (" << num << ") was not found." << endl;
else
cout << "The number (" << arr[result] << ") was found at index (" <<
return 0;
}
check if
level : Palindrome
madam: Palindrome
#include <iostream>
#include <string>
using namespace std;
palindrome
return true;
if (str[start] != str[end])
return false;
int main() {
string str;
else
return 0;
}
Write a program to solve Hanoii tower
void main(void)
{
int n ;
cin >> n ;
Hanoii('A','B','C',n);
getch();
}
Pointer
A) Write a program to read ten numbers from a
each number.
global variable.
pointer.
3- #include <iostream>
using namespace std;
int i;
int *ptr;
ptr=arr;
for (i=0;i<=9;i++)
int main ()
int arr[10];
int i;
cout<< arr[i];
square(arr);
{
cout<< arr[i];
2-
#include<iostream>
for(int i=0;i<10;i++)
*(p+i)=*(p+i) * *(p+i);
}
}
int main()
int a[10];
cout<<"enter 10 number";
for(int i=0;i<10;i++)
cin>>a[i];
square(a);
for(int i=0;i<10;i++)
cout<<a[i]<<endl;
}
return 0;
3-
#include<iostream>
int square(int n)
int res=n*n;
return res;
int main()
int a[10],i;
cout<<"enter 10 number";
for(i=0;i<10;i++)
cin>>a[i];
for(i=0;i<10;i++)
cout<<square(a[i])<<endl;
return 0;
4-
int a[10];
void square()
{
for(int i=0;i<10;i++)
a[i]=a[i]*a[i];
int main()
cout<<"enter 10 number";
for(int i=0;i<10;i++)
cin>>a[i];
square();
for(int i=0;i<10;i++)
{
cout<<a[i]<<endl;
three separate
user-inputted values.
and a+c.
appropriately handles
#include<iostream>
for(int i=0;i<n;i++)
cin>>*(p+i);
}
for(int i=0;i<n;i++)
*(p3+i)=*(p1+i) + *(p2+i);
for(int i=0;i<n;i++)
cout<<*(p+i)<<endl;
}
int main()
{int a[10],b[10],c[10],
res1[10],res2[10],res3[10];
read(a,10);
read(b,10);
read(c,10);
add(a,b,res1,10);
add(b,c,res2,10);
add(a,c,res3,10);
cout<<"a+b"<<endl;
print(res1,10);
cout<<"b+c"<<endl;
print(res2,10);
cout<<"a+c"<<endl;
print(res3,10);
return 0;
for(int i=size;i>=0;i--)
{
cout<<*(p+i)<<endl;
int * array;
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>*(array+i);
reverse(array,n);
Sorting
Merge sort
#include <iostream>
using namespace std;
int main() {
int arraySize = 7;
int A[7] = {38, 27, 43, 3, 9, 82, 10};
int low = 0;
int high = arraySize - 1;
return 0;
}
Quick sort
#include <iostream>
using namespace std;
// Partition function divides the array and returns the pivot index
int Partition(int A[], int low, int high) {
int pivot = A[low]; // Choose the first element as the pivot
int leftwall = low; // The "wall" separates elements smaller than
pivot
// Put the pivot in its correct position (middle of the smaller and larger
elements)
swap(A[low], A[leftwall]);
return 0;
}
Bubble Sort
#include <iostream>
using namespace std;
int main() {
int a[10];
int k;
bubbleSort(a, n);
return 0;
}
Insertion Sort
#include <iostream>
using namespace std;
// Shift elements of A[0..i-1] that are greater than key to the right
while (j >= 0 && A[j] > key) {
A[j + 1] = A[j]; // Move element one step to the right
j = j - 1;
}
int main() {
int A[] = {8, 3, 5, 2};
int n = sizeof(A) / sizeof(A[0]);
insertionSort(A, n);
SelectionSort
#include <iostream>
using namespace std;
int main() {
int A[] = {8, 3, 5, 2};
int n = sizeof(A) / sizeof(A[0]);
selectionSort(A, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << A[i] << " ";
}
return 0;
}
Files
//Write a program to store squares of numbers
from 1 to 10
//in a file
void main(void)
{
int k;
ofstream ofp;
ofp.open("mosalah.txt");
for(k = 1 ; k <= 10 ; k++)
ofp << k << "\t" << k*k << "\n";
ofp.close();
}
void main(void)
{
ifstream ifp;
ifp.open("mydata.txt");
if(ifp.is_open() == true) cout << "Input file is opened \n";
else { cout << "Error, input file could not be
opened \n"; return; }
int a , b;
while(ifp >> a)
{
ifp >> b;
cout << a << "\t" << b << "\n";
}
getch();
}
void main(void)
{
int k;
ofstream toto;
toto.open("salah.txt");
for(int k = 6 ; k <= 40 ; k=k+2)
toto << k << "\t" << k*k << "\n";
toto.close();
}
*/
/*
void main(void)
{
ifstream ifp;
ifp.open("salah.txt");
if(ifp.is_open() == true) cout << "Input file is opened \n";
else { cout << "Error, input file could not be opened \n"; getch();
return; }
int a , b;
while(ifp >> a)
{
ifp >> b;
cout << a << "\t" << b << "\n";
}
getch();
}
int k , n , m;
for(k = 1 ; k <= 9 ; k++)
{
fscanf(pf2,"%d %d ",&n,&m);
printf("%d \t %d \n",n,m);
}
fclose(pf2);
}
*/
int main() {
srand(time(NULL));
int nums[99];
return 0;
}
If repeated
void main(void)
{
srand(time(NULL));
// srand(3);
ofstream ofp;
ofp.open("random_numbers.txt");
int num ;
do{
num = rand() % 100;
cout << num << "\t";
ofp << num << "\n";
}while(num != 0);
ofp.close();
getch();
}
int main() {
ifstream inputFile("random_numbers.txt");
ofstream outputFile("unrepeated_random_numbers.txt");
if (!inputFile.is_open()) {
cout << "Error opening input file.\n";
return 1;
}
int temp;
int arr[1000]; // Assuming max 1000 numbers
int count = 0;
bool isRepeated;
inputFile.close();
outputFile.close();
return 0;
}
Stackusing ARRAY
class stack_using_array
{
private:
int top , size , *arr ;
public:
stack_using_array(void)
{
arr = new int[10];
size = 10;
top = 0;
}
stack_using_array(int ss)
{
arr = new int[ss];
size = ss;
top = 0;
}
void push(int value)
{
if(top >= size)
{
cout << "Stack is full, so size will be double\n";
double_size();
}
arr[top] = value ;
top++;
}
int pop(void)
{
if(top == 0)
{
cout << "Stack is empty\n";
return -1;
}
top--;
return arr[top];
}
int read_top_data(void)
{
if(top == 0) return -1;
else return arr[top-1];
}
bool is_full(void)
{
return top == size;
}
bool is_empty(void)
{
return top == 0 ;
}
int get_max_size(void)
{
return size ;
}
int get_nuumber_of_item(void)
{
return top;
}
void double_size(void)
{
int *temparr;
temparr = new int[size*2];
for(int k = 0 ; k < top ; k++)
temparr[k] = arr[k];
delete arr;
size = size * 2;
arr = temparr;
}
void delete_stack_data()
{
top = 0;
}
~stack_using_array(void)
{
delete arr;
}
};
public:
// Constructor لتحديد الحجم
queue_using_array(int ss)
{
size = ss;
arr = new int[size];
front = rear = counter = 0;
}
// إدخال عناصر
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
// إخراج عنصر
cout << "Dequeued: " << q.dequeue() << endl;
cout << "\nFinal size of queue: " << q.get_max_size() << endl;
return 0;
}
Stack using LL
//Stack by linked list
struct stackNode
{
int data;
stackNode *next;
};
class LinkedListStack
{
private:
stackNode *top;
public:
LinkedListStack()
{
top = NULL;
}
bool isEmpty(void)
{
return top == NULL;
}
void push(int dd)
{
stackNode *temp;
temp = new stackNode ;
temp->data = dd;
temp->next = top;
top = temp;
}
int pop(void)
{
stackNode *temp;
int dd;
dd = top->data;
temp = top;
top = top->next ;
delete temp;
return dd;
}
int getNumberOfElements(void)
{
int counter;
stackNode *temp;
counter = 0;
for(temp = top ; temp != NULL ; temp = temp->next)
counter++;
return counter;
}
void printStack(void)
{
stackNode *temp;
cout << "Data in stack: ";
for(temp = top ; temp != NULL ; temp = temp->next)
cout << temp->data << "\t";
cout << "\n";
}
void deleteStack(void)
{
stackNode *temp;
while( top != NULL )
{
temp = top;
top = top->next;
delete temp;
}
}
};
class LinkedListQueue
{
private:
queueNode *front, *rear;
public:
LinkedListQueue()
{
front = rear = NULL;
}
void enqueue(int dd)
{
queueNode *temp;
temp = new queueNode;
temp->data = dd;
temp->next = NULL;
if(rear == NULL) front = rear = temp;
else
{
rear->next = temp;
rear = temp;
}
}
int dequeue(void)
{
queueNode *temp;
int dd;
dd = front->data;
temp = front;
front = front->next;
delete temp;
if(front == NULL) rear = NULL;
return dd;
}
bool isEmpty(void)
{
return front == NULL;
}
void printQueue(void)
{
queueNode *temp;
cout << "All data in queue : ";
for(temp = front ; temp != NULL ; temp = temp->next)
cout << temp->data << "\t";
cout << "\n";
}
int getNumberOfElements(void)
{
int counter;
queueNode *temp;
counter = 0;
for(temp = front ; temp != NULL ; temp = temp->next)
counter++;
return counter;
}
};
رر