Posts

Showing posts with the label cc

Hackerrank Utopian Tree Solution

Image
This is the solution to the program in Hackerrank. This program can be found in the Algorithm Domain. Basically the Utopian Tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter.   Screenshot The Code: #include<stdio.h> int main() { int t, n, a; scanf("%d", &t); while(t--) { a=1; scanf("%d", &n); for(int i=1; i<=n; i++) { if(i%2) a<<=1; else a++; } printf("%d\n", a); } return 0; } Found bugs, report them !

[ FUN CODE ] Couple Or Friend Finder

Image
This is a fun code which is based on love quotient. The love quotient of the names is calculated. If the difference of the love qoutients of the two names is 0, 4, 16, or 32, the pair is declared a COUPLE or else, they are declared as FRIEND. The bellow code is implemented in C++ To calculate the love qoutient of a name individually, take the product of the letters, considering A as 1, Z as 26 and so on. Then perform modulus 36 with that number. For example, DAVE can be calculated as: D * A * V * E = (4 * 1 * 22 * 5) % 36 = 440 % 36 = 8 Screenshot The Code #include<iostream> #include<string.h> #include<cmath> using namespace std; string upper(string x) { for(int i=0;i<x.size();i++) { x[i]=toupper(x[i]); } return x; } void call(string x, string y) { int yc=1,xc=1; for(int i=0;i<x.size();i++) { xc*=((int)x[i]-64); } for(int i=0;i<y.size();i++) { yc*=((int)y[i]-64); } xc=xc%36; yc=yc%36; int dif=std::abs(xc-yc); if(dif==0 || dif=...

Implementation of Vector in c++

Image
Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container. Screenshot :  Code : #include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { int t; vector<int> a; for(int i=0;i<6;i++) { int t; cin>>t; a.push_back(t); } if(a.empty()) { cout<<"Vector is empty"<<endl; return 0; } sort(a.begin(),a.end()); for(int i=0;i<6;i++) { cout<<a[i]<<" "; } cout<<"\nThe size of vector is "<<a.size(); return 0; }

Simple Factorial program for larger numbers

Image
It is a totally different approach, we are just playing with double. As we know double has very large value 4.9e-324 to 1.8e+308. Then we convert the double into an integer array. Screenshot: OUTPUT The Code: #include<iostream> #include<stdlib.h> #include<math.h> using namespace std; /*Classic factorial function to find the factorial of a number using recursion method*/ double factorial(int n) { if(n==1) return 1; else return (factorial(n-1)*n); } int main() { int n; cout<<"factorial of: "; cin>>n; double r = factorial(n); double s = r; int i=0; /*to find the number of digits of the answer*/ for(i=0; r>=1; i++) { r /= 10; } cout<<"number of digits: "<<i<<endl; /*creating and dynamically allocated array*/ int *a = new int[370]; double t,u; int k; int l=0; /*type casting digit by digit of the answer and symultaneously storing into the array*/ for(int j=i-1; j>0; j--) { ...

Merge sort using C

Image
                     This program illustrates the merge sort using c program. Merge sort uses merge algorithm and partition algorithm to sort the given array of unsorted elements. Partition algorithm separates each element, and merge algorithm combines every thing and shows up the sorted format of the array in increasing or decreasing format as wished, here it is in increasing format. Have a look at the sample output and code. Sample Output: Program code: #include<stdio.h> #include<conio.h> void merge(int [],int ,int ,int ); void partition(int [],int ,int ); int main() { int arr[30]; int i,size; printf("\n\t*********** Merge sort *********\n\n"); printf("Enter total no. of elements : "); scanf("%d",&size); printf("Enter elements to be sorted:"); for(i=0; i<size; i++) { scanf("%d",&arr[i]); } partition(arr,0,size-1); printf("\n*********************************\n"); printf(...

Merging two files into one file

Image
Some time we need to merge two or more file written by many ones. Then we can simply use such a lite and small program to merge many files into one. Screenshots: The output of the program View of the file one View the file two View the merged file here The code: #include<iostream> #include<fstream> #include<string.h> using namespace std; int main(int argc, char *argv[]) { if ( argc != 4 ) cout<<"give 3 file as command line argument"<<endl; else { ifstream file1; /*object created for file1*/ file1.open(argv[1]); /*Name of the file1 passed through command line argument*/ string line1; /*String is created to store all the data of the file1*/ char c1 = file1.eof(); /*EOF file1 is stored in a character variable*/ getline(file1,line1,c1); /*scanning and storing the data into the string*/ cout<<"The first file is opening here:\n\n"; cout<<line1<<endl; /*Simil...

File Handling in Cpp [Read & Write]

Image
This is a program which will perform the File Handling operations in Cpp program. In this a txt file, data.txt is created in which the input is stored and then read and displayed. Screenshots [Demo] The Terminal window in which the data is entered and then fetched The file data.txt, generated by the program ! The Code #include<iostream> #include<fstream> using namespace std; int main() { fstream fp; char st[50]; fp.open("data.txt",ios::out|ios::binary); for (int i=0;i<4;i++) { cout<<"Enter THe String: "; gets(st); fp<<st<<endl; } fp.close(); fp.open("data.txt"); cout<<"Displaying the File: "; while (!fp.eof()) { fp>>st; cout<<st<<" "; } fp.close(); return 0; } Got problems, you can comment it !

Chat application in C using named PIPES

Image
This is a simple set of two C Programs which can act as a chat application. Basically there are two programs which need to run simultaneously. One is the client program and other the server program. This core concept used behind this is FIFO, named pipes. Screenshots [Demo] Client Side Chat Terminal running the client output file Server Side Chat Terminal running the server output file The Code --> Receiver(Server) Side code //client_chat #include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<unistd.h> #include<string.h> #include<linux/stat.h> #define FIFO_FILE "MYFIFO" int main() { FILE *fp; char a[40]; char readbuf[80]; start: if((fp=fopen(FIFO_FILE,"r+"))==NULL) { perror("fopen"); exit(1); } printf("Me : "); gets(a); if(a[strlen(a)-1]=='.') { fputs(a,fp); fclose(fp); ...

How to calculate the time required to execute a particular program

Image
The following program shows how to calculate the time taken to execute a program. The program is implemented using c. Here <time.h> header file is included. Output :  Code : #include<stdio.h> #include<time.h> int main() { int i; clock_t begin,end; begin=clock(); for(i=0;i<5;i++) { printf("Hello world\n"); } end=clock(); printf("Time taken : %lf\n",(double)(end-begin)/CLOCKS_PER_SEC); return 0; }

Constructor overloading in C++

Image
                          Well hello guys, I am back again with a key concept of C++ programming language. This program is all about constructor overloading concept. Even if it is a very simple concept, one should have knowledge on it. Have a look at the concept explained below. Concept:                            Constructor is a special feature in C++ whose task is to initialize objects of its class. It is called constructor because it constructs the values of data members of the class. Constructors carry the same name of the class. When we initialize any object of the class, then we can pass values through the constructor during initialization only. Constructor overloading is that thing when in one class we use constructors with different no. of parameters. i.e. the when creating objects of the class, the number of arguments it will have, accor...

Hackerrank Sherlock and array solution

Image
The below program is the solution of Sherlock and array in hackerrank. Screenshot[Demo] :  Code : #include<iostream> using namespace std; int main() { int t; cin>>t; while(t--) { int x,l=0,r=0,flag=0; cin>>x; int a[x]; for(int i=0;i<x;i++) { cin>>a[i]; r+=a[i]; } for(int i=0;i<x;i++) { l=l+a[i-1]; r=r-a[i]; cout<<l<<" "<<r<<" "<<i<<endl; if(l==r) { flag=1; break; } } if(flag==1) cout<<"YES"<<endl; else cout<<"NO"<<endl; } }

Find the factorial using PIPES in C

Image
This is a simple program in C which uses the pipes to find the factorial of a number. Initially the Child get the number from the user and performs the factorial operation. After performing the factorial operation it writes and gets closed. Now the parent extracts the stored data and displays it in the console. Screenshots[Demo] The Code: #include<stdio.h> #include<stdlib.h> #include<sys/wait.h> #include<unistd.h> int main() { int pfds[2]; int n,f=1,i,n1; pipe(pfds); if(!fork()) { printf("Child: Writing to the PIpe \n"); printf("Child: Enter the NUmber:"); scanf("%d",&n); for(i=1;i<=n;i++) f=f*i; write(pfds[1],(char *)&f,sizeof(f)); close(pfds[1]); printf("Child: Exiting\n"); } else { wait(0); printf("Parent: reaading from pipe\n"); read(pfds[0],&n1,sizeof(int)); printf("Parent : Read %d\n",n1); sleep(1); close(pfds[0]); } return 0; } Got...

Matrix and Vector Multiplication In C using OpenMP Model

Image
This is a program which will multiply a vector and a matrix and display it. Basically a 1D Dynamic array and another 2D Dynamic array gets created and values are automatically assigned to it using the rand().  Here, OpenMP model is used for parallel computation of the task. Screenshot The Code: #include<stdio.h> #include<malloc.h> #include<omp.h> #include<time.h> int main() { int **a,n,i,j,*b,*c,k; clock_t st,en; double t; printf("Enter the N: "); scanf("%d",&n); // For the A Matrix a=(int**)malloc(sizeof(int*)*n); for(i=0;i<n;i++) a[i]=(int*)malloc(sizeof(int)*n); for(i=0;i<n;i++) { for(j=0;j<n;j++) { a[i][j]=rand()/100000000; } } //For the B Matrix b=(int*)malloc(sizeof(int)*n); for(i=0;i<n;i++) b[i]=rand()/100000000; //For The C Matrix c=(int*)malloc(sizeof(int)*n); for(i=0;i<n;i++) c[i]=0; printf("The A MAtrix :\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { ...

Hackerrank Anagram solution

Image
The below program is the solution of Anagram in implementation sub-domain. You can refer the question from  HERE. Output : Code : #include<stdio.h> int main() { int t,i,x,c,flag; char s[10001]; int *a=(int*)malloc(sizeof(int)*26); int *b=(int*)malloc(sizeof(int)*26); memset(a,0,sizeof(int)*26); memset(b,0,sizeof(int)*26); scanf("%d",&t); while(t--) { scanf("%s",&s); flag=0;c=0; if(strlen(s)%2==1) { printf("-1\n"); continue; } for(i=0;i<26;i++) { a[i]=b[i]=0; } for(i=0;i<strlen(s)/2;i++) a[s[i]-'a']++; for(i=strlen(s)/2;i<strlen(s);i++) b[s[i]-'a']++; for(i=0;i<26;i++) { c+=abs(a[i]-b[i]); } printf("%d\n",c/2); } }

Hackerrank Angry Professor Solution

Image
The below program is the solution of Angry Professor in Implementation sub-domain. You can get the question  HERE . Output : Code : #include<iostream> using namespace std; int main() { int t,x,y,c=0,m; cin>>t; while(t--) { cin>>x>>y; while(x--) { cin>>m; if(m<=0) c++; } if(c>=y) cout<<"NO"<<endl; else cout<<"YES"<<endl; c=0; } return 0; }

Addition of Two Matrix By OpenMP Model

Image
This is a program in which the user gives the Number of Elements as input, then from that input 3 dynamic arrays are created in which first two are assigned with random values. Using the OpenMP model, the addition is distributed among different threads and then the addition of the above matrix is done and finally stored in the third Matrix. Screenshots [DEMO] The Code #include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<omp.h> int main() { int *a,*b,*c,i,N; printf("Enter The No of Elements for both arrays: "); scanf("%d",&N); a=(int*)malloc(sizeof(int)*N); b=(int*)malloc(sizeof(int)*N); c=(int*)malloc(sizeof(int)*N); for(i=0;i<N;i++) { //scanf("%d",&a[i]); a[i]=rand()/100000; } for(i=0;i<N;i++) { b[i]=rand()/100000; //scanf("%d",&b[i]); } printf("A[%d]:\t",N); for(i=0;i<N;i++) { printf("%d ",a[i]); //a[i]=rand(); } printf("\n...

OpenMP, Parallel Programming in C [Intro]

Image
This is a post in which you will get the basic knowledge about the parallel programming concept. In this post we going to use the OpenMP model. This program is implemented in the C Language. Basically this programs demonstrates how the thread gets executed in different Cores of the processor. In this case, this program is executed in a Quad Core CPU, so it displays 4 threads from 0-3. Screenshots[Demo] Here the threads are executed randomly on each cores on the basics of their availability. This method by which threads get their processor can be known in detail by googling about ready queue. The Code: #include<stdio.h> #include<omp.h> int main() { int x; #pragma omp parallel { x=omp_get_thread_num(); printf("Thread Is running at %dth processor\n",x); } return 0; } Got Suggestions? Comment them here !

Hackerrank Caeser Cipher Solution

Image
The below program is the solution of Caeser Cipher in implementation domain. You can get the program  HERE. Output :  Code : #include<iostream> using namespace std; int main() { int x,t,m; cin>>x; string s; cin>>s; cin>>t; while(t>26) { t=t-26; } for(int i=0;i<s.length();i++) { m=s[i]+t; if(islower(s[i])) { if(m>122) { m=m-122; m+=96; } } else if(isupper(s[i])) { if(m>90) { m=m-90; m+=64; } } //cout<<m; if(isalpha(s[i])) cout<<(char)m; else cout<<s[i]; } return 0; }

Printing ' Hello world ' without using any semicolon

Image
The below program shows how to print something without using semicolon. The program is implemented using c. Output : Code : #include<stdio.h> main() { if(printf("Hello world")) { } } Description : In C, printf() returns the number of character   successfully written on the output .

Non Restoring Division Algorithm Implementation in C

Image
This  is a Dynamic program for the implementation of the NON RESTORING Division Algorithm in C Language. Non Restoring Division uses Left Shift Operations, Twos' Compliment and Binary Addition. Screenshots[Demo] The Code #include<stdio.h> #include<malloc.h> int *a,*q,*m,*mc,*c,n,d; int powr(int x,int y) { int s=1,i; for(i=0;i<y;i++) s=s*x; return s; } void print(int arr[],int n) { int i; for(i=0;i<n;i++) printf("%d ",arr[i]); } void bin(int n, int arr[]){ int r, i = 0; do{ r = n % 2; n /= 2; arr[i] = r; i++; }while(n > 0); } void set(int array[], int x){ int i,tmp[20]={0}; for(i = x -1; i >=0; i--) tmp[x-1-i]=array[i]; for(i=0;i<x;i++) array[i]=tmp[i]; } int len(int x) { int i=0; while(powr(2,i)<=x) i++; return ++i; } void addBinary(int a1[], int a2[]) { int bi[2]={0},ca[20]={0}; int t=len(n),tmp=0; int *su=(int*)malloc(sizeof(int)*len(n)); while(t...