Showing posts with label Competative Programming. #codercommunity #coderhe. Show all posts
Showing posts with label Competative Programming. #codercommunity #coderhe. Show all posts

Class Constructor and Destructor for this Class

 #include<iostream>

using namespace std;

class Student

{

string name;

public:

Student(){

    name = "Vaibhav Yadav";

    cout<<"Constructor of Student Class has been Called"<<endl;

    cout<<"Student Name is: "<<name<<endl;

}

~Student(){

    cout<<"---Destructor Of Student Class has been called---"<<endl;

    cout<<"---Memory for 'obj name' has been deallocated and memory has been released---"<<endl;

}

};


main()

{

Student stu;

return 0;

}


Matrix Addition and Mulitplication Is given here

 #include <stdio.h>

#define max 100

int main() {

    int arr1[max][max],arr2[max][max],arr3[max][max],arr4[max][max];

    int k,i,j,n;

    printf("Enter Row and Column of Matrix(both Should be equal to perform the Operation): ");

    scanf("%d",&n);

    printf("Enter Your Array 1 Element\n");

    for(i=0;i<n;i++)

    for(j=0;j<n;j++){

        printf("Enter %d Row %d Column: ",i,j);

        scanf("%d",&arr1[i][j]);

    }

    printf("Enter Your Array 2 Element\n");

    for(i=0;i<n;i++)

    for(j=0;j<n;j++){

        printf("Enter %d Row %d Column: ",i,j);

        scanf("%d",&arr2[i][j]);

    }

    printf("\nYour Matrix 1 is \n");

    for(i=0;i<n;i++){

        for(j=0;j<n;j++){

            printf("%d ",arr1[i][j]);

        }

        printf("\n");

    }

    printf("Your Matrix 2 is \n");

    for(i=0;i<n;i++){

        for(j=0;j<n;j++){

            printf("%d ",arr2[i][j]);

        }

        printf("\n");

    }

    for(i=0;i<n;i++){

        for(j=0;j<n;j++){

            arr3[i][j]+=arr1[i][j]+arr2[i][j];

        }

    }

    printf("\nYour Addition of Matrix is \n");

    for(i=0;i<n;i++){

        for(j=0;j<n;j++){

            printf("%d ",arr3[i][j]);

        }

        printf("\n");

    }

    for(i=0;i<n;i++){

        for(j=0;j<n;j++){

            arr4[i][j]=0;

            for(k=0;k<n;k++)

            arr4[i][j]+=arr1[i][k]*arr2[k][j];

        }

    }

    printf("\nYour mutiplication of Matrix is \n");

    for(i=0;i<n;i++){

        for(j=0;j<n;j++){

            printf("%d ",arr4[i][j]);

        }

        printf("\n");

    }

    


    return 0;

}

Array Implementation of Stack (Using predefined Value)

// program to implement array

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

struct stack{

      int top;

      int capacity;

      int *array;

};

struct stack *create(int size){

      struct stack *s = (struct stack *)malloc(sizeof(struct stack));

      if(!s){

            printf("Not enough space\n");

            return NULL;

      }

      s->capacity = size;

      s->top = -1;

      s->array = malloc(s->capacity*sizeof(int));

      if(!s->array){

            printf("Not enough space\n");

            return NULL;

      }

      return s;

}

int isempty(struct stack *s){

      return (s->top==-1);

}

int isfull(struct stack *s){

      return (s->top==s->capacity-1);

}

int size(struct stack *s){

      return (s->top+1);

}

void push(struct stack *s,int data){

      if(isfull(s)){

            printf("stack overflow\n");

            return;

      }

      else

      s->array[++s->top]=data;

}

int pop(struct stack *s){

      if(isempty(s)){

            printf("Stack is underflow\n");

            return -1;

      }

      return s->array[s->top--];

}

int peek(struct stack *s){

      if(isempty(s)){

            printf("stack is Empty\n");

            return -1;

      }

      else

      return (s->array[s->top]);

}

int main()

{

      int capacity=15,i;

      struct stack *stk = create(capacity);

      for(i=0;i<capacity;i++){

            push(stk,i);

      }

      printf("Top element in stack %d \n",peek(stk));

      printf("Deleted element in stack %d \n",pop(stk));

      printf("Top element in stack %d \n",peek(stk));

      printf("No of Element in Stack %d \n",size(stk));


    return 0;

}


Program to find the last index of an element of array using recursive function.

 // coded by Vaibhav Yadav


#include <iostream>

#define ll long long

ll index(ll a[],ll size,ll x);

using namespace std;

int main() {

    ll n,i,x;

    cout<<"Enter Your Number of Elements in array: ";

    cin>>n;

    ll a[n];

    for(i=0;i<n;i++){

        cout<<"Enter Your Element no "<<i+1<<": ";

        cin>>a[i];

    }

    cout<<"Enter the element you want to search for: ";

    cin>>x;

    if(index(a,n,x)>=0)

    cout<<"Element Is found at position "<<index(a,n,x)<<endl;

    else

    cout<<"Element is not found"<<endl;

    return 0;

}

ll index(ll a[], ll size, ll x){

    if(a[size-1]==x)

    return size-1;

    else

    index(a,size-1,x);

}

Round G 2022 - Kick Start 2022 Walktober Problem (C Code)

// Below is the Solution of the Round G 2022 -kickstart 2022 :) 

// Contact me if any question persist in your mind, I'm ready to help you :) 

// HAPPY CODING

#include <stdio.h>

int main(){

    int t,count=0;

    scanf("%d",&t);

    while(t--){

        int M,N,P,k=0,i,j;

        scanf("%d%d%d",&M,&N,&P);

        int max[N],pl[N],n[N];

        for(i=0;i<N;i++)

        max[i]=0;

        for(i=0;i<M;i++){

            for(j=0;j<N;j++){

                scanf("%d",&n[j]);

                if((i+1)!=P){

                if(max[j]<n[j])

                max[j]=n[j];

                }

                else{

                    pl[j]=n[j];

                }

            }

        }

        int ans=0;

        for(i=0;i<N;i++){

            if(max[i]>pl[i])

            ans+=(max[i]-pl[i]);

            else

            ans+=0;

        }

        printf("Case #%d: %d\n",++count,ans);

    }

    return 0;

}

Program to print the smallest word in an sentence (Coding Ninja Solutions)

#include <stdio.h>

#include <string.h> // Here I've to add this header file for strcat() function

int main() {

    char name[]="Vaibhav Yadav is good boy";

    char ccname[]=" "; // This is to add space at the end of sentence so that condition can read it.

    strcat(name,ccname); // this functions append the space to last character

    int i,j,k,min=50,count=0;

    for(i=0;name[i]!='\0';i++){

        if(name[i]!=' '){

        count++;

        }

        else{

            if(count<min)

            {

                min=count;

                k  = i;

                k-=count;

            }

                count=0;

        }

    }

    

    // printf("%d ",min);

    for(i=k;name[i]!=' ';i++)

    printf("%c",name[i]);

    k = 0;

    return 0;

}

Operator Overloading in C++

#include<iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i = 0){       re...