Snippets Collections
// Author : Khadiza Sultana
// Date : 1/4/2025
#include <iostream>
#include <vector>
using namespace std;

int search(vector<int>& nums, int target) {
    int st = 0, end = nums.size() - 1;
    while (st <= end) {
        int mid = st + (end - st) / 2;
        if (nums[mid] == target) {
            return mid;
        }
        if (nums[st] <= nums[mid]) { // Left half is sorted
            if (nums[st] <= target && target <= nums[mid]) {
                end = mid - 1;
            } else {
                st = mid + 1;
            }
        } else { // Right half is sorted
            if (nums[mid] <= target && target <= nums[end]) {
                st = mid + 1;
            } else {
                end = mid - 1;
            }
        }
    }
    return -1;
}

int main() {
    vector<int> nums = {4, 5, 6, 7, 0, 1, 2};
    int target = 0;

    int result = search(nums, target);

    if (result != -1) {
        cout << "Target " << target << " found at index: " << result << endl;
    } else {
        cout << "Target " << target << " not found in the array." << endl;
    }

    return 0;
}
// Author : Khadiza Sultana
// Date : 1/4/2025
#include<iostream>
#include<vector>
using namespace std;

int binarySearch(vector<int>&arr, int tar) { // practical implementation
    int n = arr.size();
    int st = 0, end = n-1;
    while(st <= end) {
        int mid = st + (end-st)/2; // not using (st+end)/2 to avoid integer overflow
        if (tar > arr[mid]) {
            st = mid+1;
        }
        else if (tar < arr[mid]) {
            end = mid-1;
        }
        else {
            return mid;
        }
    }
    return -1;
}

int binarySearch2(vector<int>&arr, int tar, int st, int end) { // recursive implementation
    if (st > end) { // base case
        return -1;
    }
    int mid = st + (end-st)/2;
    if (tar > arr[mid]) {
        binarySearch2(arr, tar, mid+1, end);
    }
    else if (tar < arr[mid]) {
        binarySearch2(arr, tar, st, mid-1);
    }
    else {
        return mid;
    }
}

int main() {
    vector<int>arr1 = {3, 5, 7, 12, 15, 18}; // even no of elements
    int tar1 = 3;
    vector<int>arr2 = {4, 6, 10, 11, 12, 18, 19}; // odd no of elements
    int tar2 = 19;

    cout << "Index at which tar1 is found(even no of elements) : " << binarySearch(arr1, tar1) << endl;
    cout << "Index at which tar2 is found(odd no of elements) : " << binarySearch(arr2, tar2) << endl;
    
    cout << "Using Recusive function index at which tar1 is found : " << binarySearch2(arr1, tar1, 0, 5) << endl;
    cout << "Using Recusive function index at which tar1 is found : " << binarySearch2(arr2, tar2, 0, 6) << endl;

    return 0;
}
// Author : Khadiza Sultana
#include<iostream>
#include<vector>
using namespace std;

int main() {
    int a = 10;
    int* ptr = &a;
    int** parPtr = &ptr;
    cout << ptr << endl;
    cout << parPtr << endl;
    cout << *(&a) << endl;
    cout << *(ptr) << endl;
    cout << *(parPtr) << endl;
    cout << **(parPtr) << endl;
    return 0;
}
#include <stdio.h>

int main() {
 int a = 10;
 int *p =&a; 
 int b = *p;
 printf("a= %d\n",a);
 printf("adress of a= %d\n",&a);
 printf("value of p= %d\n",p);
printf("address of p= %d\n",&p);
printf("(deref) show the value of stored memory address in p= %d\n",*p);
printf("summary: p= &a ; *p = a | b = %d",b);
}
star

Fri Jan 03 2025 20:18:56 GMT+0000 (Coordinated Universal Time)

#loop #c++ #vector #pointers #binarysearch #array
star

Fri Jan 03 2025 18:59:47 GMT+0000 (Coordinated Universal Time)

#loop #c++ #vector #pointers #binarysearch
star

Fri Jan 03 2025 10:56:21 GMT+0000 (Coordinated Universal Time)

#loop #c++ #vector #pointers
star

Tue Sep 12 2023 15:51:54 GMT+0000 (Coordinated Universal Time) https://www.w3schools.com/c/tryc.php?filename

#c #pointers

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension