Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

reinterpret_cast

#include <stdio.h>

typedef  unsigned char     uint8;
typedef  unsigned int      uint16;

int main()
{
    uint8 data = 5;
    const uint8* dataPtr = &data;
    const uint16* dataPtr16 = reinterpret_cast<const uint16*>(dataPtr);
    uint16 result = *dataPtr16;
    
    uint16 realDataIndex = 10, result1 = 0;
    
    const uint8* sendData = reinterpret_cast<const uint8*>(dataPtr) + 1;
    
    const uint16* dataPtr1 = reinterpret_cast<const uint16*>(data);
    *reinterpret_cast<uint8*>(&result1) = *(reinterpret_cast<const uint8*>(&dataPtr1));
    
    printf("%d %d, %d %d %d\n", dataPtr, dataPtr16, *dataPtr, *dataPtr16, result);
    printf("%d, %d\n", *sendData, result1);
}

map at

#include <iostream>

typedef unsigned long int uintptr_t;

template<typename _IntType, typename _PointerType> _IntType castInt(_PointerType ptr)
{
    typedef _IntType ToType;
    typedef _PointerType FromType;
    if (sizeof(ToType) < sizeof(FromType)) {
        uintptr_t intFromMemAddr = reinterpret_cast<uintptr_t>(ptr);
        ToType value = intFromMemAddr;
        return value;
    }
    else {
        return reinterpret_cast<uintptr_t>(ptr);
    }
}


int main()
{
    std::cout << sizeof (uintptr_t);
    return 0;
}

Càlcul resistències paral·leles 2 resistències

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
   cout << "Càlcul de resistència total" << endl; 
   
   double r1 = 7;
   double r2 = 3;
   double part1 = 1/r1 + 1/r2;
   
   cout << "R1= "         << r1    << endl;
   cout << "R2= "         << r2    << endl;
   cout << "1/r1 + 1/r2=" << part1 << endl; 
   
   double resultat = 1/part1;
   
   cout << "Resistència total (paral·lela)= "<< resultat << endl;
   return 0;
}

Compile and Execute C++11 Online

#include <stdio.h>

main()
{
  float x,y,z;
  
  printf("Enter value of x:\n");
  scanf("%f",&x);
  printf("\nEnter value of y:\n");
   scanf("%f",&y);
   z = x * y;
   printf("The product of %f and %f is %f.",x,y,z);
   return 0;
}

ACPP_efficiently-implement-k-queues-single-array

#include <iostream>

// https://www.geeksforgeeks.org/efficiently-implement-k-queues-single-array/

int main()
{
    // Let us create 3 queue in an array of size 10 
    int k = 3, n = 10; 
    kQueues ks(k, n); 
  
    // Let us put some items in queue number 2 
    ks.enqueue(15, 2); 
    ks.enqueue(45, 2); 
  
    // Let us put some items in queue number 1 
    ks.enqueue(17, 1); 
    ks.enqueue(49, 1); 
    ks.enqueue(39, 1); 
  
    // Let us put some items in queue number 0 
    ks.enqueue(11, 0); 
    ks.enqueue(9, 0); 
    ks.enqueue(7, 0); 
  
    cout << "Dequeued element from queue 2 is " << ks.dequeue(2) << endl; 
    cout << "Dequeued element from queue 1 is " << ks.dequeue(1) << endl; 
    cout << "Dequeued element from queue 0 is " << ks.dequeue(0) << endl; 
    
    return 0;
}

ACPP_implement-two-stacks-in-an-array

#include <iostream>

// https://www.geeksforgeeks.org/implement-two-stacks-in-an-array/
class twoStacks
{
    int* arr;
    int spA;
    int spB;
    
    int STACK_SIZE;
    int STACKA_BEG;
    int STACKB_BEG;
    
    public:
    
    twoStacks(int size) : STACK_SIZE(size)
    {
        spA = -1;
        spB = size;
        STACKA_BEG = 0;
        STACKB_BEG = size - 1;
        
        arr = (int*) malloc(sizeof(int)*size);
        
        for (int id = 0; id < size; id++)
        {
            arr[id] = -1;
        }
    }
    
    bool isNotFull() { return (spB - spA) > 0; }
    bool isEmptyA() { return spA < STACKA_BEG; }
    bool isEmptyB() { return spB > STACKB_BEG; }
    
    void push1(int value)
    {
        if (isNotFull())
        {
            arr[++spA] = value;
        }
    }
    
    void push2(int value)
    {
        if (isNotFull())
        {
            arr[--spB] = value;
        }
    }
    
    int pop1()
    {
        if (!isEmptyA())
        {
            int top = arr[spA--];
            arr[spA] = -1;
            
            return top;
        }
        return -1;
    }
    
    int pop2()
    {
        if (!isEmptyB())
        {
            int top = arr[spB++];
            arr[spB++] = -1;
            
            return top;
        }
        return -1;
    }
};

int main()
{
    twoStacks ts(5); 
    ts.push1(5); 
    ts.push2(10); 
    ts.push2(15); 
    ts.push1(11); 
    ts.push2(7); 
    std::cout << "Popped element from stack1 is " << ts.pop1(); 
    ts.push2(40); 
    std::cout << "\nPopped element from stack2 is " << ts.pop2();  
   
    return 0;
}

ACPP_queue-using-stacks

#include <iostream>
#include <stack>

// https://www.geeksforgeeks.org/queue-using-stacks/

class QueueA // enqueue heavy implementation
{
    std::stack<int> stA;
    std::stack<int> stB;
    
    public:
    
    QueueA() {}
    
    void enQueue(int value)
    {
        if (stA.empty())
        {
            stA.push(value);
        }
        else
        {
            while (!stA.empty())
            {
                int top = stA.top();
                
                stB.push(top);
                stA.pop();
            }
            
            stA.push(value);
            
            while (!stB.empty())
            {
                int top = stB.top();
                
                stA.push(top);
                stB.pop();
            }
        }
    }
    
    int deQueue()
    {
        int top = stA.top();
        
        stA.pop();
        
        return top;
    }
};

class QueueB
{
    std::stack<int> stA;
    std::stack<int> stB;
    
    public:
    
    QueueB() {}
    
    void enQueue(int value)
    {
        stA.push(value);
    }
    
    int deQueue()
    {
        if (!stB.empty())
        {
            int top = stB.top();
            
            stB.pop();
            
            return top;
        }
        else
        {
            while (!stA.empty())
            {
                int top = stA.top();
                
                stB.push(top);
                stA.pop();
            }
            
            int top = stB.top();
            
            stB.pop();
            
            return top;
        }
    }
};

int main()
{
    QueueA qa; 
    qa.enQueue(1); 
    qa.enQueue(2); 
    qa.enQueue(3); 
  
    std::cout << qa.deQueue() << '\n'; 
    std::cout << qa.deQueue() << '\n'; 
    std::cout << qa.deQueue() << '\n';
    
    /* ***************************** */
    std::cout << std::endl << std::endl;
    
    QueueB qb; 
    qb.enQueue(1); 
    qb.enQueue(2); 
    qb.enQueue(3); 
  
    std::cout << qb.deQueue() << '\n'; 
    std::cout << qb.deQueue() << '\n'; 
    std::cout << qb.deQueue() << '\n';  
   
    return 0;
}

ACPP_delete-a-linked-list-node-at-a-given-position

#include <iostream>

// https://www.geeksforgeeks.org/delete-a-linked-list-node-at-a-given-position/

struct Node
{
    int data;
    struct Node* next;
};

Node* create_node(int value)
{
    Node* node = (Node*) malloc(sizeof(struct Node));
    
    if (node)
    {
        node->data = value;
        node->next = NULL;
    }
    
    return node;
}

void push(Node** head, int value)
{
    if ((*head) == NULL)
    {
        (*head) = create_node(value);
        return;
    }
    
    Node* curr = (*head);
    
    while (curr->next)
        curr = curr->next;
        
    curr->next = create_node(value);
}

void deleteNode(Node** head, int ind)
{
    if (ind == 1)
    {
        Node* node = (*head);
        
        (*head) = (*head)->next;
        free(node);
        
        return;
    }
    
    Node* curr = (*head);
    int cind = 1;
    
    while (true)
    {
        if (cind == ind - 1)
        {
            Node* node = curr->next;
            
            curr->next = curr->next->next;
            free(node);
            
            break;
        }
        
        curr = curr->next;
        cind = cind + 1;
    }
}

void printList(Node* head)
{
    Node* curr = head;
    
    while (true)
    {
        if (curr == NULL) break;
        
        std::cout << curr->data << " ";
        curr = curr->next;
    }
}

int main()
{
    /* Start with the empty list */
    struct Node* head = NULL; 
  
    push(&head, 7); 
    push(&head, 1); 
    push(&head, 3); 
    push(&head, 2); 
    push(&head, 8); 
  
    puts("Created Linked List: "); 
    printList(head); 
    
    deleteNode(&head, 4); 
    puts("\nLinked List after Deletion at position 4: "); 
    printList(head);
    
    deleteNode(&head, 3); 
    puts("\nLinked List after Deletion at position 3: "); 
    printList(head);  
    
    deleteNode(&head, 7); 
    puts("\nLinked List after Deletion at position 7: "); 
    printList(head);  
    
    deleteNode(&head, 8); 
    puts("\nLinked List after Deletion at position 8: "); 
    printList(head);  
   
    return 0;
}

Pi FANJAC

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
   cout << "Hola" << endl; 
   cout.precision(10);
   
   int    Npart = 1000000000;
   double Base  = 1.0/Npart;
   
   cout << "No. parts " << Npart<<endl;
   cout << "Base      " << Base << endl;
   
   double suma = 0;
   for( int i=0; i< Npart; ++i){
        double x = Base * i;
        double y=sqrt(1-x*x);
        double area = Base*y;
        suma = suma + area;
        //cout << "Rectangle "<< i <<"\t x= "<< x <<"\t y= "<< y<<"\t area= "<<area<<endl;
   }
   cout <<endl<< "Pi:" << suma * 4 <<endl;
   
   return 0;
}

ACPP_c-program-cyclically-rotate-array-one

#include <iostream>

// https://www.geeksforgeeks.org/c-program-cyclically-rotate-array-one/

void rotate(int* arr, int n)
{
    int endElement = arr[n - 1];
    
    for (int id = n - 1; id >= 1; id--)
    {
        arr[id] = arr[id - 1];
    }
    
    arr[0] = endElement;
}

int main()
{
    int arr[] = {1, 2, 3, 4, 5}, i; 
    int n = sizeof(arr) /  
            sizeof(arr[0]); 
  
    std::cout << "Given array is \n"; 
    for (i = 0; i < n; i++) 
        std::cout << arr[i] << " "; 
  
    rotate(arr, n); 
  
    std::cout << "\nRotated array is\n"; 
    for (i = 0; i < n; i++) 
        std::cout << arr[i] << " "; 
   
    return 0;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.