Add files via upload

This commit is contained in:
Christian Bender
2017-12-23 18:30:49 +01:00
committed by GitHub
parent 220c0fbaf8
commit 4b7d334c6e
31 changed files with 2290 additions and 0 deletions

17
Others/Buzz_number.cpp Normal file
View File

@@ -0,0 +1,17 @@
//A buzz number is a number that is either divisble by 7 or has last digit as 7.
#include <iostream>
using namespace std;
int main()
{
int n,t;
cin >> t;
while(t--)
{
cin >> n;
if((n%7==0)||(n%10==7))
cout << n << " is a buzz number" << endl;
else
cout << n << " is not a buzz number" << endl;
}
return 0;
}

View File

@@ -0,0 +1,20 @@
#include<iostream>
using namespace std;
int main()
{
int number;
cin>>number;
int remainder,binary=0,var=1;
do{
remainder=number%2;
number=number/2;
binary=binary+(remainder*var);
var=var*10;
}
while(number>0);
cout<<binary;
}

View File

@@ -0,0 +1,51 @@
#include <iostream>
using namespace std;
void main(void)
{
int valueToConvert = 0; //Holds user input
int hexArray[8]; //Contains hex values backwards
int i = 0; //counter
int lValue = 0; //Last Value of Hex result
cout << "Enter a Decimal Value" << endl; //Displays request to stdout
cin >> valueToConvert; //Stores value into valueToConvert via user input
while (valueToConvert > 0) //Dec to Hex Algorithm
{
lValue = valueToConvert % 16; //Gets remainder
valueToConvert = valueToConvert / 16;
hexArray[i] = lValue; //Stores converted values into an array
i++;
}
cout << "Hex Value: ";
while (i > 0)
{
//Displays Hex Letters to stdout
switch (hexArray[i - 1]) {
case 10:
cout << "A";
break;
case 11:
cout << "B";
break;
case 12:
cout << "C";
break;
case 13:
cout << "D";
break;
case 14:
cout << "E";
break;
case 15:
cout << "F";
break;
default:
cout << hexArray[i - 1]; //if not an int 10 - 15, displays int value
}
i--;
}
cout << endl;
}

View File

@@ -0,0 +1,23 @@
//This program aims at calculating the GCD of n numbers by division method
#include <iostream>
using namepsace std;
int main()
{
cout <<"Enter value of n:"<<endl;
cin >> n;
int a[n];
int i,j,gcd;
cout << "Enter the n numbers:" << endl;
for(i=0;i<n;i++)
cin >> a[i];
j=1; //to access all elements of the array starting from 1
gcd=a[0];
while(j<n)
{
if(a[j]%gcd==0) //value of gcd is as needed so far
j++; //so we check for next element
else
gcd=a[j]%gcd; //calculating GCD by division method
}
cout << "GCD of entered n numbers:" << gcd;
}

26
Others/Happy_number.cpp Normal file
View File

@@ -0,0 +1,26 @@
/* A happy number is a number whose sum of digits is calculated until the sum is a single digit,
and this sum turns out to be 1 */
#include <iostream>
using namespace std;
int main()
{
int n,k,s=0,d;
cout << "Enter a number:";
cin >> n;
s=0;k=n;
while(k>9)
{
while(k!=0)
{
d=k%10;
s+=d;
k/=10;
}
k=s;
s=0;
}
if(k==1)
cout << n << " is a happy number" << endl;
else
cout << n << " is not a happy number" << endl;
}

View File

@@ -0,0 +1,65 @@
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
using namespace std;
char stack[100];
int top=0;
void push(char ch)
{
stack[top++]=ch;
}
char pop()
{
return stack[--top];
}
bool check(char x, char y)
{
if ((x=='(' && y==')') || (x=='{' && y=='}') || (x=='[' && y==']') || (x=='<' && y=='>'))
{
return true;
}
else
{
return false;
}
}
int main()
{
char exp[100];
cout<<"Enter The Expression : ";
gets(exp);
for (int i = 0; i < strlen(exp); i++)
{
if (exp[i]=='(' || exp[i]=='{' || exp[i]=='[' || exp[i]=='<')
{
push(exp[i]);
}
else if (exp[i]==')' || exp[i]=='}' || exp[i]==']' || exp[i]=='>')
{
if(!check(pop(), exp[i]))
{
cout<<"\nWrong Expression";
exit(0);
}
}
}
if(top==0)
{
cout<<"Correct Expression";
}
else
{
cout<<"\nWrong Expression";
}
return 0;
}

28
Others/Sparse matrix.cpp Normal file
View File

@@ -0,0 +1,28 @@
/*A sparse matrix is a matrix which has number of zeroes greater than (m*n)/2,
where m and n are the dimensions of the matrix.*/
#include <iostream>
int main()
{
int m,n,i,j,c=0;
cout << "Enter dimensions of matrix:";
cin >> m >> n;
int a[m][n];
cout << "Enter matrix elements:";
for(i=0;<m;i++)
{
for(j=0;j<n;j++)
cin >> a[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]==0)
c++; //Counting number of zeroes
}
}
if(c>((m*n)/2)) //Checking for sparse matrix
cout << "Sparse matrix";
else
cout << "Not a sparse matrix";
}

View File

@@ -0,0 +1,60 @@
#include <iostream>
using namespace std;
Multiply(int A[][], int B[][], int n)
{
if (n==2)
{
int p1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
int p2= (a[1][0]+a[1][1])*b[0][0];
int p3= a[0][0]*(b[0][1]-b[1][1]);
int p4= a[1][1]*(b[1][0]-b[0][0]);
int p5= (a[0][0]+a[0][1])*b[1][1];
int p6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
int p7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
int c[n][n];
c[0][0]=p1+p4-p5+p7;
c[0][1]=p3+p5;
c[1][0]=p2+p4;
c[1][1]=p1-p2+p3+p6;
return c[][];
}
else
{
}
}
int main()
{
int p,q,r,s;
cout<<"Enter the dimensions of Matrices";
cin>>n;
int A[n][n],;
int B[n][n],;
cout<<"Enter the elements of Matrix A";
for (int i = 0; i < n; i++)
{
for (int j = 0; j <n ; j++)
{
cin>>A[i][j];
}
}
cout<<"Enter the elements of Matrix B";
for (int i = 0; i < n; i++)
{
for (int j = 0; j <n ; j++)
{
cin>>B[i][j];
}
}
Multiply(A, B, n);
return 0;
}

88
Others/Tower of Hanoi.cpp Normal file
View File

@@ -0,0 +1,88 @@
#include<iostream>
using namespace std;
struct tower
{
int values[10];
int top;
}F, U, T;
void show()
{
cout<<"\n\n\tF : ";
for(int i=0; i<F.top; i++)
{
cout<<F.values[i]<<"\t";
}
cout<<"\n\tU : ";
for(int i=0; i<U.top; i++)
{
cout<<U.values[i]<<"\t";
}
cout<<"\n\tT : ";
for(int i=0; i<T.top; i++)
{
cout<<T.values[i]<<"\t";
}
}
void mov(tower &From, tower &To)
{
--From.top;
To.values[To.top]=From.values[From.top];
++To.top;
}
void TH(int n, tower &From, tower &Using, tower &To)
{
if (n==1)
{
mov(From, To);
show();
}
else
{
TH(n-1, From, To, Using);
mov(From, To);
show();
TH(n-1, Using, From, To);
}
}
int main()
{
F.top=0;
U.top=0;
T.top=0;
int no;
cout << "\nEnter number of discs : " ;
cin >> no;
for (int i = no; i >0; i--)
{
F.values[F.top++]=i;
};
show();
TH(no, F, U, T);
return 0;
}

100
Others/fibonacci.cpp Normal file
View File

@@ -0,0 +1,100 @@
#include <iostream>
using namespace std;
/*
Efficient method for finding nth term in a fibonacci number sequence.
Uses Dvide and conquer approach
Enter Values from 1
Eg-
1st term = 0;
2nd term = 1;
3rd term = 1;
.
.
.
.
*/
int a[2][2] = {{1,1},{1,0}};//fibonacci matrix
int ans[2][2] = {{1,1},{1,0}};//final ans matrix
/*
Working Principal:
[F(k+1) F(k)] [1 1]^k
| | = | |
[F(k) F(k-1)] [1 0]
where F(k) is the kth term of the Fibonacci Sequence.
*/
void product(int b[][2],int k[][2])
{
/*
Function for computing product of the two matrices b and k
and storing them into the variable ans.
Implementation :
Simple matrix multiplication of two (2X2) matrices.
*/
int c[2][2];//temporary stores the answer
c[0][0] = b[0][0]*k[0][0]+b[0][1]*k[1][0];
c[0][1] = b[0][0]*k[0][1]+b[0][1]*k[1][1];
c[1][0] = b[1][0]*k[0][0]+b[1][1]*k[1][0];
c[1][1] = b[1][0]*k[0][1]+b[1][1]*k[1][1];
ans[0][0] = c[0][0];
ans[0][1] = c[0][1];
ans[1][0] = c[1][0];
ans[1][1] = c[1][1];
}
void power_rec(int n)
{
/*
Function for calculating A^n(exponent) in a recursive fashion
Implementation:
A^n = { A^(n/2)*A^(n/2) if n is even
{ A^((n-1)/2)*A^((n-1)/2)*A if n is odd
*/
if((n == 1)||(n==0))
return;
else
{
if((n%2) == 0)
{
power_rec(n/2);
product(ans,ans);
}
else
{
power_rec((n-1)/2);
product(ans,ans);
product(ans,a);
}
}
}
int main()
{
//Main Function
cout <<"Enter the value of n\n";
int n;
cin >>n;
if(n == 1)
{
cout<<"Ans: 0"<<endl;
}
else
{
power_rec(n-1);
cout <<"Ans :"<<ans[0][1]<<endl;
}
return 0;
}

View File

@@ -0,0 +1,60 @@
/*
* Sieve of Eratosthenes is an algorithm to find the primes
* that is between 2 to N (as defined in main).
*
* Time Complexity : O(N)
* Space Complexity : O(N)
*/
#include <iostream>
using namespace std;
#define MAX 10000000
int primes[MAX];
/*
* This is the function that finds the primes and eliminates
* the multiples.
*/
void sieve(int N)
{
primes[0] = 1;
primes[1] = 1;
for(int i=2;i<=N;i++)
{
if(primes[i] == 1) continue;
for(int j=i+i;j<=N;j+=i)
primes[j] = 1;
}
}
/*
* This function prints out the primes to STDOUT
*/
void print(int N)
{
for(int i=0;i<=N;i++)
if(primes[i] == 0)
cout << i << ' ';
cout << '\n';
}
/*
* NOTE: This function is important for the
* initialization of the array.
*/
void init()
{
for(int i=0;i<MAX;i++)
primes[i] = 0;
}
int main()
{
int N = 100;
init();
sieve(N);
print(N);
}