Monday, April 27, 2020

Power of Recursion

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;


int factorialCalc(int n);
// returns factorial value
double powerCalc(int base, int pow);
// returns power value
int digitMatch(int first, int second, int third);
// The function has three integer parameters first , second and
// third that are positive and have the same number of digits.
// It returns the number of positions where their digits
// match.
int digitsOpposite(int x, int y);
// It returns the number of positions where one number has an even
// digit and the other has an odd one.
bool startsWith(int x, int y) ;
// It determines whether the second number starts with the
// first number.
int digitDifferences(int x, int y);
// It prints the number formed from digits obtained as (positive)
// differences between corresponding digits in the parameters
int lucky7(int x);
// It calculatesan answer by turning the first 7 (from the left)
// in the parameter to 77.
int interlaceDigits(int x, int y);
// returns an integer that begins with the first digit of the
// first parameter, then the first digit
// of the second parameter, then the second digits of
// the parameters, and so on until all digits are used.
int gce(int x, int y) ;
// returns the greatest common ending to the two number
int biggerDigits(int a, int b) ;
// returns an integer whose digit in each position is the bigger of
// the two digits in that position in the input parameters.
int doubleDigit(int n) ;
//makes each digit of an input parameter repeat twice.




int main() {
srand(time(NULL));

cout<<factorialCalc(6)<<endl;
cout <<powerCalc(3,4)<<endl;
cout << digitMatch(168, 567, 767) << " matches !" << endl;
cout << digitMatch(143, 243, 343) << " matches !"<< endl;
cout << startsWith(7, 747) << endl; // prints true
cout << startsWith(74, 74) << endl; // prints true
cout << startsWith(747, 74) << endl; // prints false
cout << digitDifferences(162, 538) << endl;

cout << lucky7(747) << endl; // prints 7747
cout << lucky7(7) << endl; // prints 77
cout << lucky7(1234) << endl; // prints 1234
cout << lucky7(172737) << endl; // prints 1772737

cout << interlaceDigits(1, 2) << endl; // prints 12
cout << interlaceDigits(117, 302) << endl; // prints 131072
cout << interlaceDigits(1357, 2468) << endl; // prints 123456780

cout << gce(123, 123) << endl; // prints 123
cout << gce(123, 223) << endl; // prints 23
cout << gce(117, 119) << endl; // prints 0
cout << gce(1357, 13657) << endl; // prints 57

cout << biggerDigits(567, 765) << endl; // prints 767
cout << biggerDigits(123456, 444444) << endl; // prints 444456
cout << biggerDigits(999, 111) << endl; // prints 999

cout << doubleDigit(9) << endl; // prints 99
cout << doubleDigit(81) << endl; // prints 8811
cout << doubleDigit(243) << endl; // prints 224433
cout << doubleDigit(244) << endl; // prints 224444

}




int factorialCalc(int i){
if (i==1) return 1;
if (1<i)
{
i = i * (factorialCalc(i-1));
}
return i;
}


double powerCalc(int base, int pow){
if (pow>1)
{
base = base * powerCalc(base, pow-1);
}
return base;
}

int digitMatch(int first, int second, int third) {
if (first <= 0)
return 0;

if (first % 10 == second % 10 && first % 10 == third % 10)
return 1 + digitMatch(first / 10, second / 10, third / 10);

return digitMatch(first / 10, second / 10, third / 10);
}

int digitsOpposite(int x, int y)
{
if (x <= 0) return 0;
if (x % 2 == y % 2) return digitsOpposite(x/ 10, y / 10);
return digitsOpposite(x/ 10, y / 10) + 1;
}

bool startsWith(int x, int y)
{
if (x == y)
return true;
if (x > y)
return false;
return startsWith(x, y/10);
}


int digitDifferences(int x, int y)
{
if (x == 0) return 0;
int diff = x % 10 - y % 10;
if (diff < 0) diff = -diff;
return 10 * digitDifferences(x/10,y/10) + diff;
}


int lucky7(int x) {
if (x <= 0) return 0;
int ans = lucky7(x / 10);
if ((ans == x / 10) && (x % 10 == 7)) return 100 * ans + 77;
return 10 * ans + x % 10;
}


int interlaceDigits(int x, int y) {
if (x == 0) return 0;
return 100 * interlaceDigits(x / 10, y / 10) + 10 * (x % 10) + y % 10;
}


int gce(int x, int y) {
if (x == 0) return 0;
if (x % 10 != y % 10) return 0;
return 10 * gce(x / 10, y / 10) + x % 10;
}

int biggerDigits(int a, int b) {
if (a == 0 && b == 0) return 0;
if (a % 10 > b % 10) return 10 * biggerDigits(a/10, b/10) + a % 10;
else return 10 * biggerDigits(a/10, b/10) + b % 10;
}


int doubleDigit(int n) {
if (n < 10) return n * 11;
return 100 * doubleDigit(n / 10) + doubleDigit(n % 10);
}

Saturday, December 7, 2019

Sieve of Eratosthenes: , ancient algorithm for finding all prime numbers up to any given limit


Thursday, November 28, 2019

Structs 2

#include <iostream>
using namespace std;

const double pi = 3.14159; 

struct circle 
{
 double radius, diameter;
};

circle getInfo();
void showinfo(const circle &c);

int main() 
{
circle one; 
one=getInfo(); //this is important: where would the getter function will return the data 
showinfo(one);
cout << "\n";
cout << "\n";
}




circle getInfo() //this is function which gets info for a struct and return that struct //AKA getter function 
{
circle tempDataCircle;
cout << "Please enter the diameter = ";
cin >> tempDataCircle.diameter;
tempDataCircle.radius=tempDataCircle.diameter/2.0;
return tempDataCircle;
}

void showinfo(const circle &c)
{
double area = (pi * c.radius * c.radius );
cout << "Diameter is: "<<c.diameter;
cout << "\n";
cout << "Radius is: "<<c.radius;
cout << "\n";
cout << "Area is: "<<area;
}

Structs 1

The following code would introduce to structs.



#include <iostream>
using namespace std;

struct student //this is a struct named student
{
 int id;
 string name;
 int age;
 int grade;
};

void showinfo(int id, string name, int age, int grade) //passing all the struct variables
{
cout <<"Id: "<<id<<endl;
cout <<"Name: "<<name<<endl;
cout <<"Age: "<<age<<endl;
cout <<"Grade: "<<grade<<endl;
}

void showinfo2(student n)//passing the whole struct
{
cout <<"Id: "<<n.id<<endl;
cout <<"Name: "<<n.name<<endl;
cout <<"Age: "<<n.age<<endl;
cout <<"Grade: "<<n.grade<<endl;
}

// Using value parameter for structure can
// slow down a program, waste space
// Using a reference parameter will speed up
// program, but function may change data in
// structure
// Using a const reference parameter allows
// read-only access to reference parameter,
// does not waste space, speed

void showinfo3(const student &n)//passing the whole struct
{
cout <<"Id: "<<n.id<<endl;
cout <<"Name: "<<n.name<<endl;
cout <<"Age: "<<n.age<<endl;
cout <<"Grade: "<<n.grade<<endl;
}



int main()
{
//
student student1;
cout << "Enter student's name: ";
cin >> student1.name;
cout << "\n";
cout << "Enter student's age: ";
cin >> student1.age;
cout << "\n";
cout << "Enter student's id: ";
cin >> student1.id;
cout << "\n";
cout << "Enter student's grade: ";
cin >> student1.grade;
cout << "\n";
cout << "\n";
cout << "Here's student1's info:\n";
showinfo(student1.id,student1.name,student1.age,student1.grade);
cout << "\n";
cout << "\n";
student student2={470175,"Pazi",9,10}; //this is how a structure is initialized in a single line
cout << "Here's student2's info:\n";
showinfo(student2.id,student2.name,student2.age,student2.grade);
cout << "\n";
cout << "\n";
student student3={470165,"Nazi",9,10};
showinfo2(student3);
cout << "\n";
cout << "\n";
showinfo3(student3);
}

Wednesday, November 27, 2019

Partitioning An Array in C++

What is meant partitioning:
The array would have smaller and larger values separated by a specific value.

example:
if the array is:  5 | 4 | 1| 7 | 3  and we want it to partition at value 5;
larger value is 7, goes after 5;
smaller values are 1, 4 and 3; goes before 5

The final output after partitioning would be:  4 | 1 | 3| 5 | 7
What is not taken into account in here, is, we are NOT doing any kind of sorting over here. We are just setting up a boundary of data where the rest of values gets separated.



Partitioning an array: Pseudo
  1. Take a carbon Copy of the array 
  2. Make the main array become all value = partition value 
  3. Iterate an compare with smaller number and larger numbers 
  4. Smaller numbers gets to go in the front indexes of  the array
  5. Larger number gets to go in the back indexes of the array
  6. While iterating, we don't worry about value being the same as partition
  7. Rest of the array remains as the partition value.
Task: Write a function that would take three parameters:  a double array, an int representing the array size, indx representing the partition value index

Here's the C++ code:

void Partition(double data[],int n, int indx)
{
cout <<"\n=======================================================\n";
double tempData[n]; //a carbon copy of the input array
for (int i=0; i<n; i = i+1)
{
tempData[i]=data[i];
}
for (int i=0; i<n; i = i+1) {cout<< tempData[i]<<"|"; } //print array
cout<<"\n";

int boundary=data[indx];
cout << "Partition is at : "<<boundary<<endl;

for (int i=0; i<n; i = i+1) {data[i]=boundary;} //main array is all now.

 for (int i=0,j=0,k=n-1; i<=n-1 ; i++)  //i to iterate, j to count from front, k to count from back
    {
if (tempData[i] < boundary) //if the value is less than the boundary 
{
cout <<"\n";
data[j] = tempData[i] ;  //add it to the front
j++;
for (int m=0; m<5; m = m+1) {cout<< data[m]<<"|"; }
cout<<"\n";
}
else if (tempData[i] > boundary) //if the value is more than the boundary 
{
data[k] = tempData[i] ; //add it to the back
k--;
for (int m=0; m<5; m = m+1) {cout<< data[m]<<"|"; }
cout<<"\n";
}
//if any iterate value is matching the boundary, we do nth now, the iterated value would remain 0
}

cout<< "\n\n";
for (int i=0; i<5; i = i+1) {cout<< data[i]<<"|"; } //print array
cout <<"\n=======================================================\n";
return;
}

Friday, November 15, 2019

Arrays Playground

Arrays are set of variables that can hold multiple variables.

How do we declare Arrays:

string arr[3]; 

Here, the array type is string and name is "arr", and it will hold 3 elements, meaning the size of the array.

The size can be omitted by doing this :  string arr[]If the size of the array determined to unknown. But if it is known, it's better to declare the size.

 How do we initialize Arrays:

< under -progress > This post is under progress, if you are a learner and want to contribute your progress in this blog, contact me! ashik.shovon03@gmail.com < under -progress >


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>

using namespace std;

void printArray(int array[],int size);
void printEvenArray(int array[],int size);
void printEvenArray2(int array[],int size);
void printOddArray(int array[],int size);
int sumOfArray(int array[],int size);
double avgOfArray(int array[],int size) ;
int searchLinear(int arr[],int size,int search);
int findMinimum(int arr[],int size);

int main()
{
char arr[]={'a','b'};
cout << arr <<endl;

int const size =5;
int arr2[size]={0,2,4,5};
int arr3[6]={50,22,74,51,62,102};

printArray(arr2,size); //0 | 2 | 4 | 5 | 0
cout << "\n";
printEvenArray(arr2,size);  //0 | 4 | 0 |
cout << "\n";
printEvenArray2(arr2,size); //0 | 4 | 0 |
cout << "\n";
printOddArray(arr2,size);     //2 | 5 |
cout << "\n";
cout<< sumOfArray(arr2,size); //11
cout << "\n";
cout<< avgOfArray(arr2,size); //2.2
cout << "\n";
cout<< searchLinear(arr2,size,5);//3
cout << "\n";
cout<< searchLinear(arr2,size,4);//2
cout << "\n";
cout<< findMinimum(arr3,6); //22
cout << "\n";
cout<< findMinimum(arr2,size); //0
cout << "\n";
return 0;
}


void printArray(int array[],int size) // to print the whole array
{
for (int i=0; i<size; i++) //the classic iterator.
  if (i==size-1) // if i reaches the last element
    cout <<array[i]<<" "; //so that the last " | " doesn't show up
  else
    cout <<array[i]<<" | "; // so that " | " shows up untill it reaches the last element 
}

void printEvenArray(int array[],int size) // to print the even indexes in the array (for loop)
{
for (int i=0; i<size; i = i+2)
  cout <<array[i]<<" | ";
}

void printEvenArray2(int array[],int size) // to print the even indexes in the array (do + if)
{
int i=0; 
do {
     if  (i%2==0){
                  cout <<array[i]<<" | ";
                  }
     i = i+1;
    }while (i<size);

}

void printOddArray(int array[],int size) // to print the odd indexes in the array
{
for (int i=1; i<size; i = i+2)
  cout <<array[i]<<" | ";
}

int sumOfArray(int array[],int size) // to return sum of the elements of an array 
{
int sum=0;
for (int i=0; i<size; i = i+1)
 { sum = sum+array[i]; }
return sum;
}

double avgOfArray(int array[],int size)  // to return average of the elements of an array 
{
/*
int sum=0;
for (int i=0; i<size; i = i+1)
 { sum = sum+array[i]; }
return sum/size;
*/
// all those things can be simplified into this:
return (sumOfArray(array,size)*1.0)/size; //multiplying by 1.0 to get a double value 

}


int searchLinear(int arr[],int size,int search) //iterates through the elements and searchs for an element
{
for (int i=0; i<size; i = i+1)
    {
            if (arr[i]==search)
                        {
                          return  i;               // if it exists in any index the function returns the index 
                        }
    }
return -1; //// if it doesn't exist, the function returns -1
}

int findMinimum(int arr[],int size) //iterates through the elements and searchs for a minimum value
{
int min=arr[0]; //assumes that the first one is the minimum
for (int i=1; i<size;i++)
    {
        if (arr[i]<min) // iterate an compares with min variable 
            min = arr[i]; // lowest value gets to enter into min variable
    }
return min; // lowest value is returned
}

 

Copyright @ 2017 Codename: CPlusPlus.

Designed by Templateiy