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

ACPP_block-swap-algorithm-for-array-rotation

#include <iostream>

// https://www.geeksforgeeks.org/block-swap-algorithm-for-array-rotation/

void swap(int* arr, int fi, int si, int d)
{
    for (int i = 0; i < d; i++)    
    {
        int t = arr[fi + i];
        arr[fi + i] = arr[si + i];
        arr[si + i] = t;
    }
}

void leftRotate(int* arr, int d, int n)
{
    int i, j;
    
    if (d == 0 || d == n) return;
    
    i = d;
    j = n - d;
    
    while (i != j)
    {
        if (i < j) // A is shorter
        {
            swap(arr, d - i, d + j - i, i);
            j = j - i;
        }
        else // B is shorter
        {
            swap(arr, d - i, d, j);
            i = i - j;
        }
    }
    
    // Finallu, block swap A and B
    swap(arr, d - i, d, i);
}

void printArray(int* arr, int n)
{
    for (int i = 0; i < n; i++)    
    {
        std::cout << arr[i] << " ";
    }
}

int main()
{
   int arr[] = {1, 2, 3, 4, 5, 6, 7}; 
   
   printArray(arr, 7);
   leftRotate(arr, 2, 7); 
   
   std::cout << std::endl;
   printArray(arr, 7);  
   
   return 0;
}

Testsss

#include <iostream>

using namespace std;

int main()
{
   cout << "Hello World" << endl; bfb
   
   
   return 0;
}

Compile and Execute C++11 Online

#include <iostream>

int main() {
    std::cout << sizeof(void*) << std::endl;
    return 0;
}

Compile and Execute C++11 Online

#include <iostream>

using namespace std;

int main()
{
    int a;
    
    cout << sizeof(a) << endl;
}

Algorithms

#include <iostream>

using namespace std;

int main()
{
   cout << "Hello World customized" << endl; 
   
   return 0;
}

no_bound_check

#include <iostream>
int main() {
    char before[7]="BEFORE";
    char chain[6]="CHAIN";
    char after[6]="AFTER";
    
    chain[8]='*';
    chain[-4]='#';

    std::cout << "before : " << before << std::endl;
    std::cout << "chain : " << chain << std::endl;
    std::cout << "after : " << after << std::endl;
}

Arduino CV car

/**
   Motor shield: 4, 7 - направление; 5, 6 - скорость
*/

#define SPEED_1 5 // Пин контроля скрости 1-го мотора
#define DIR_1 4 // Пин направления 1-го мотора

#define SPEED_2 6 // Пин контроля скрости 2-го мотора
#define DIR_2 7 // Пин направления 2-го мотора

#define BAUDHRATE 115200

// +---------------------+
// | Motor control block |
// +---------------------+

void bkw()
{
  digitalWrite(DIR_1, HIGH);
  analogWrite(SPEED_1, 120);

  digitalWrite(DIR_2, HIGH);
  analogWrite(SPEED_2, 120);
}
void fwd()
{
  digitalWrite(DIR_1, LOW);
  analogWrite(SPEED_1, 120);

  digitalWrite(DIR_2, LOW);
  analogWrite(SPEED_2, 120);
}
void left()
{
  digitalWrite(DIR_2, HIGH);
  analogWrite(SPEED_2, 100);

  digitalWrite(DIR_1, LOW);
  analogWrite(SPEED_1, 50);

}
void right()
{
  digitalWrite(DIR_2, LOW);
  analogWrite(SPEED_2, 50);

  digitalWrite(DIR_1, HIGH);
  analogWrite(SPEED_1, 100);
}
void stop()
{
  analogWrite(SPEED_2, 0);
  analogWrite(SPEED_1, 0);
}
void right_fwd() {
  digitalWrite(DIR_2, HIGH);
  analogWrite(SPEED_2, 20);
  digitalWrite(DIR_1, LOW);
  analogWrite(SPEED_1, 200);
}
void left_fwd() {
  digitalWrite(DIR_2, LOW);
  analogWrite(SPEED_2, 255);
  digitalWrite(DIR_1, HIGH);
  analogWrite(SPEED_1, 20);
}

// +-------------------------+
// | Motor control block END |
// +-------------------------+

void setup()
{
  //Setup motor shield
  for (int i = 4; i < 8; i++)
  {
    pinMode(i, OUTPUT);
  }

  pinMode(13, OUTPUT);

  Serial.begin(BAUDHRATE);
  //Serial.println("Ready");
}

void loop()
{
  String direction = Serial.readString();

  //Serial.println("CMD: " + direction);

  if (direction == "fwd") {
    fwd();
  } else if (direction == "bkw") {
    bkw();
  } else if (direction == "left") {
    left();
  } else if (direction == "right") {
    right();
  } else if (direction == "stop") {
    stop();
  } else if (direction == "left_fwd") {
    left_fwd();
  } else if (direction == "right_fwd") {
    right_fwd();
  }
}

Compile and Execute C++11 Online

#include <iostream>
#include <cstring>
#define N 15
using namespace std;


struct Persona{
    int id;
    char nombre[20];
    char apellido[20];
};

int main() {
    
    // Puntero a array de N variables de tipo Persona
    Persona *arr = (Persona *) malloc(sizeof(Persona)*N);
    // Puntero doble (array de punteros)
    Persona **arr2 = (Persona **) malloc(sizeof(Persona*)*N);
    
    strcpy(arr[5].nombre, "Juan");
    
    arr2[5] = arr+5;
    strcpy(arr2[5]->nombre, "Juana");
    
    
    for(unsigned short i=0; i<N; ++i){
        (arr+i)->id = 0xF23 + i;
        cout << "(" << (arr+i) << ")";
        cout << "(#"<<(arr+i)->id <<")";
        cout << " Nombre: "<< (arr+i)->nombre << endl;
    }
    
    
    arr2[10] = (Persona *) malloc(sizeof(Persona));
    
    Persona *ptrp10 = arr2[10];
    ptrp10->id = 123456; 
    
    for(unsigned short i=0; i<N; ++i){
        cout << endl << i << " (#"<<(arr2+i) <<") --> " << arr2[i];
    }
    
    free(arr2);
    cout << endl << endl;
    cout << "El borrado de punteros doble no implica que se borre el puntero simple.....";
    cout << endl << ptrp10->id;
    
    return 0;
}

Compile and Execute C++11 Online

 #include <bits/stdc++.h>
    using namespace std;
    #define PB push_back
    #define MP make_pair
    #define F first
    #define S second
    typedef long long ll;
    typedef vector<int> vi;
    typedef pair<int,int>pi;
     
    int main() 
      {
      	ios_base::sync_with_stdio(0);
      	cin.tie(NULL);
      	long x,y,n,i;
      	cin>>n;
      long	ans=0;
      long	px=0,py=0;bool flag=0;
      	for(i=0;i<n;i++)
      	{
      	    cin>>x>>y;
      	
     long 	dif=min(x,y)-max(px,py)+1;
      	if(dif)
      	ans+=dif;
      	
      	if(x==px && y==py)
      	  --ans;
      	 px=x;
      	 py=y;
      	 
      	}
      	cout<<ans;
      	
    
      
      	
     
     
     
    	return 0;
      }

Compile and Execute C++11 Online

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

long long fact(long long x)
{
    long long answer = 1;
    for(long long i=1; i<=x; i++)
        answer *= i;
    
    return answer;
}

int main()
{
    long long x, n;
    cin >> x >> n;
    double ex = 0;
    cout.precision(10);
    cout << fixed;
    
    for(long long i=0; i<=n; i++)
    {
        ex += pow(x,i)/fact(i);
        cout << i << " " << ex << endl;
    }
   
   return 0;
}

Advertisements
Loading...

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