Tuesday, November 30, 2021

Coding Challange 2

 Question : arr[1, 2, 3, 4, 5, 6, 7] ko arr[4, 5, 6, 7, 1, 2, 3] banana hai .


#include<stdio.h>
void reverse(int* arr) {
    int frst = 0;
    int temp;
    int i;
    int j;
    while(frst != 3) {
        temp = arr[0];
        for(i = 1; i<=6; i++) {
            arr[i - 1] = arr[i];
        }
        arr[6] = temp;
        frst++;
    }
    for( j = 0; j<=6; j++) {
        printf("%d", arr[j]);
    }
}
int main() {
    int arr[7] = {1, 2, 3, 4, 5, 6, 7};
    reverse(arr);
    return 0;
}
//4567 123

Monday, November 29, 2021

StrCpy Implementation

 #include<stdio.h>
void fncpy(char* str2, char* str1) {
    int i = 0;
    while(*str1 != '\0') {
        str2[i] = *str1;
        i++;
        str1++;
    }
    *str1 = '\n'; //Isse lagana padega qki compiler khud ye copy nahi kareaga
    printf("Content from str2 to str1 is %s", str2);
}
int main() {
    char str1[5] = "atul";
    char str2[5];
    fncpy(str2, str1);
    return 0;
}

Strcmp implementation by self, while mein dhyan de do conditions par check kar raha hai

 #include<stdio.h>
int fnCmp(char* str1, char* str2) {
    while( (*str1 != '\0' && *str2 != '\0') && (*str1 == *str2)) {
        str1++;
        str2++;
    }
    if(*str1 == *str2)
       return 0;
    else
       return *str1 - *str2;
}
int main() {
    char str1[] = "abc";
    char str2[] = "abc";
    int x = fnCmp(str1, str2);
    if(x == 0) {
        printf("Strings Are Same");
    } else {
        printf("Strings Are not Same");
    }
    return 0;
}

Strings {pata nahi direct concatination bhi hota hai}

 #include<stdio.h>

int main() {
    
    char name[10] = {'s', 't', 'r', 'i', 'n', 'g'};
    printf("%s", name);
    char *str = "Hello World";
    printf("\n");
    while(*str != '\0') {
        printf("%c", *str);
        str = str + 1;
    }


    printf("\n");
    printf("%c", "Hello"[0]);


    printf("\n");
    printf("%c", *("Atul" + 0));


    printf("\n");


    printf("Ashish\
    Madarchod\
    Hai \n");


    printf("Ashish""Madarchod""Tha""Hai""Aur""Rehega");
}

Passing 2D Array to a function (2D Array Haua nahi hai normal array ki tarah he behave karta hai)

 #include<stdio.h>
void fun(int (*ptr)[4]) {
    for(int i = 0; i<=2; i++) {
        printf("Address of i block of arr %p\n", ptr + i);
        for(int j = 0; j<=3; j++) {
            printf("arr[%d][%d] = %d\n",i,j,*(*(ptr + i) + j) );
        }
    }
};
int main() {
    int arr[3][4] = {
      {11, 12, 13, 14},
      {15, 16, 17, 18},
      {19, 20, 21, 22}
    };
    
    int (*ptr)[4] = arr;
    
    fun(ptr);
    
    
    return 0;
}

2D Array

 What is 2D Array ?

A 2D array is actually a 1-D array in which each element is itself a 1-D array


#include<stdio.h>
int main() {
    int arr[3][4] = {
      {11, 12, 13, 14},
      {15, 16, 17, 18},
      {19, 20, 21, 22}
    };
    
    int (*ptr)[4] = arr;
    
    for(int i = 0; i<=2; i++) {
        printf("Address of i block of arr %p\n", ptr + i);
        for(int j = 0; j<=3; j++) {
            printf("arr[%d][%d] = %d\n",i,j,*(*(ptr + i) + j) );
        }
    }
    return 0;
}

Sunday, November 28, 2021

Insertion Sort (if pehle wala bada hai toh jagah change kar aise karte jaana hai condition for loop ke andar lagana hai)


#include<stdio.h>
int main() {
    int temp;        
    int arr[6] = {30, 45, 12, 51, 19, 8};
    for(int i = 1; i<=5; i++) {
          
             for(int j = i; j>0; j--) {
                 
                 if(arr[j-1] > arr[j]) {
                     temp = arr[j -1];
                     arr[j -1] = arr[j];
                     arr[j] = temp;
                 }
                 
             }
            
    }
    
    for(int k = 0; k<=5; k++) {
                printf("%d ", arr[k]);
            }
         return 0;
}

Priority queue deleting elements after ascending inputs Gaand Fadd

 using namespace std; #include<iostream> #define N 5 class pq { public:     int* arr;     int front;     int rear;     pq() {         ...