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

Compile and Execute C++ Online

cpp

#include <iostream>

int main()
{
   
   int num = ((rand() & 10) + 1);
   
   std::cout << num;
   
   return 0;
}

mergeSort

cpp

#include <iostream>

using namespace std;

int n,v[100];

void citire (){
  int i;
  cin>>n;                   
  for(i=1;i<=n;i++)
    cin>>v[i];
 }

void afisare (){
  int i;
  for(i=1;i<=n;i++)
  cout<<v[i]<<" ";
}

void ordonare (int p, int u ){
  int aux;
  if(v[p]>v[u]){ 
    aux=v[p];
    v[p]=v[u];       
    v[u]=aux;
  }
}

void interclasare (int p, int u , int m ){
  int i , k=0, j,a[100];
  i=p;
  j=m+1;
  while (i<=m&&j<=u)
    if(v[i]<v[j]) {
       k++;
       a[k]=v[i];
       i++;
    }
    else {
      k++;
      a[k]=v[j];
      j++;
    }
    if(i<=m)
      for (j=i;j<=m;j++) {
        k++;
        a[k]=v[j];
    }
    else for(i=j;i<=u;i++) {
      k++;
      a[k]=v[i];
    }
  for(i=1;i<=k;i++) v[p+i-1]=a[i] ;
}

void divide (int p , int u ) {
  int m ;
  if(u<=p+1)     
  ordonare (p,u);
  else {
    m=(p+u)/2;
    divide(p,m);
    divide(m+1,u);
    interclasare(p,u,m);
    }
}

int main () { 
  citire ();
  divide (1,n);
  afisare();
  return 1;
}

Saa Yangu

cpp

#include <iostream>
#include <ctime>

using namespace std;

int main() {
   // current date/time based on current system
   time_t now = time(0);
   
   // convert now to string form
   char* dt = ctime(&now);

   cout << "The local date and time is: " << dt << endl;

   // convert now to tm struct for UTC
   tm *gmtm = gmtime(&now);
   dt = asctime(gmtm);
   cout << "The UTC date and time is:"<< dt << endl;
}

MP5.3_Runge-Kutta

cpp

//Machine Problem No. 5.3 - 4th-order Runge-Kutta Method
//code written by RCQuilala - MSCE (S)

#include <iostream>
#include <cmath>

using namespace std;

double fy(double t, double y, double z)
{
    double p=sin(t)+cos(y)+sin(z);
    return p;
}

double fz(double t, double z)
{
    double p=cos(t)+sin(z);
    return p;
}

int main()
{
    cout<<"Solve the system of ordinary differential equations"<<endl<<endl;
    cout<<"     dy/dt = sin t + cos y + sin z,   y(0) = 0"<<endl;
    cout<<"     dz/dt = cos t + sin z,           z(0) = 0"<<endl<<endl;
    cout<<"     t ϵ [0, 20] with 100 intervals,"<<endl<<endl;
    cout<<"using a Fourth-order Runge-Kutta Method."<<endl<<endl;
    cout<<" t           y           z"<<endl<<endl;

    double h=0.2, k1, k2, k3, k4, l1, l2, l3, l4;
    int i=0, n=100;
    double t[n+1], y[n+1], z[n+1];

    t[0]=0, y[0]=0, z[0]=0;
    cout<<t[0]<<"       "<<y[0]<<"      "<<z[0]<<endl;
    
    for(i=1;i<n+1;i++)
    {
        t[i]=(i)*h;
        
        k1=h*fy(t[i],y[i-1],z[i-1]);
        l1=h*fz(t[i],z[i-1]);
        k2=h*fy(t[i]+h/2,y[i-1]+k1/2,z[i-1]+l1/2);
        l2=h*fz(t[i]+h/2,z[i-1]+l1/2);
        k3=h*fy(t[i]+h/2,y[i-1]+k2/2,z[i-1]+l2/2);
        l3=h*fz(t[i]+h/2,z[i-1]+l2/2);
        k4=h*fy(t[i]+h,y[i-1]+k3,z[i-1]+l3);
        l4=h*fz(t[i]+h,z[i-1]+l3);
        
        y[i]=y[i-1]+(k1+2*k2+2*k3+k4)/6;
        z[i]=z[i-1]+(l1+2*l2+2*l3+l4)/6;
        
        cout<<t[i]<<"      "<<y[i]<<"       "<<z[i]<<endl;
    }
    
    return 0;
}

Compile and Execute C++ Online

cpp

#include <iostream>

extern "C" {
int toInt(float a)
{
    int *b = (int *)&a;
    return *b;
}
}


int main()
{
    
    float x = 65.315f;
    
    int a = (int)x;
    int b = (int&)x;
    
    int  c = reinterpret_cast<int &>(x);
    int &d = reinterpret_cast<int &>(x);
    
    
    std::cout << x << '\n' << a << '\n'
    << b << '\n' << c << '\n' << d << std::endl;
    
    
    d = 50;
    
    std::cout << d << '\n' << x << '\n'
    << b << '\n' << c << std::endl;
    
    
    const float z = 10.11f;
    
    int  e = reinterpret_cast<const int &>(z);
    const int &f = reinterpret_cast<const int &>(z);
    
    std::cout << e << '\n' << f << std::endl;
    
    
    int g = toInt(x);
    int h = toInt(z);
    
    std::cout << g << '\n' << h << std::endl;
    
    return 0;
    
}

MP5.2_Predictor-Corrector Scheme

cpp

//Machine Problem No. 5.2 - Adams-Bashforth-Moulton Predictor-Corrector Scheme
//code written by RCQuilala - MSCE (S)

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

double f(double y, double t)
{
    double p=-20*y+exp(-t);
    return p;
}

int main()
{
    cout<<"Solve for y(2) from the differential equation"<<endl<<endl;
    cout<<"     y' = -20y + e^(-bt),   y(0) = 0"<<endl<<endl;
    cout<<"using a second-order Adams-Bashforth and third-order"<<endl;
    cout<<"Adams-Moulton Predictor-Corrector Scheme."<<endl<<endl;

    double h;
    cout<<"Enter the step size, h    :   "; cin>>h; cout<<h<<endl<<endl;
    cout<<"t            y(t)"<<endl<<endl;
    int i, n=2/h;
    double t[n+1], y[n+1], y_p[n+1];

    t[0]=0, y[0]=0;
    
    for(i=1;i<n+1;i++)
    {
        t[i]=i*h;
        
        if(i==1)
        {
            y_p[i]=y[i-1]+h*f(y[i-1],t[i-1]);
            //1st-order Adams-Bashforth
            y[i]=y[i-1]+0.5*h*(f(y_p[i],t[i])+f(y[i-1],t[i-1]));
            //2nd-order Adams-Moulton
        }
        else
        {
            y_p[i]=y[i-1]+0.5*h*(3*f(y[i-1],t[i-1])-f(y[i-2],t[i-2]));
            //2nd-order Adams-Bashforth
            y[i]=y[i-1]+(h/12)*(5*f(y_p[i],t[i])+8*f(y[i-1],t[i-1])-f(y[i-2],t[i-2]));
            //3rd-order Adams-Moulton
        }
    }
    
    for(i=0;i<n+1;i++)
    {
        cout<<t[i]<<"      "<<y[i]<<endl;
    }
    
    cout<<endl<<"y(2) = "<<y[n]<<endl;

    return 0;
}

Compile and Execute C++ Online

cpp

#include <iostream>
#include <cstring>
using namespace std;

typedef struct{
    int a;
    int arr[10];
} Test;

int main () {
    string str = "12d";
    cout << sizeof(string) << endl;
    cout << sizeof(Test) << endl;
    return 0;
}

Joss

cpp

#include <iostream>
#include <iomanip>
using namespace std;

#include "ItemToPurchace.h"
using namespace std;
int main() {
    int i;
    const int NUM_ITEMS = 2;
    string name;
    int price;
    int quantity;
    double total;
    ItemToPurchase item1;
    ItemToPurchase item2;
    for (i=0;i < NUM_ITEMS; ++i) {
        cout << "Item" << i + 1 <<endl;
        cout << "Enter the item name:" << endl;
        getline(cin, name);
        
        cout << "Enter the item price:" << endl;
        cin >> price;
        cout << "Enter the item quanity:" << endl;
        cin >> quanity;
        cout << endl;
        if(i==0) {
            item1.SetName(name);
            item1.SetPrice(price);
            item1.SetQuanity(quanity);
            
        }
        else if (i==1) {
            item2.SetName(name);
            item2.SetPrice(price);
            item2.SetQuanity(quanity);
        }
        cin.ignore();
     }
     total = (item1.GetPrice()* item1.GetQuanity()) + (item2.GetPrice()* item2.GetQuanity())
     cout << "TOTAL Cost" << endl;
     item1.Print();
     item2.Print();
     cout << endl;
     cout << "Total: $" << total << endl;
     return 0;
     
     
     
}

Shopping-H

cpp

#ifdef SHOPPING_CART_H
#define SHOPPING_CART_H
#include <string>

using namespace std;
#include "ItemtoPurchase.h"
class shoppingCart{
    public:
    shoppingCart()
    shoppingCart(stringname,stringdate);
    stringGeCustomerName()const;
    "Customer Date";
void AddItem(ItemtoPurchaseItem);
void RemoveItem(string name);

void ModifyItem(ItemtoPurchaseItem);
int GetNumItemsInCart();
double GetCostofCart();

void PrintTotal();
void PrintDescription();

    private:
    string CustomerName;
    string CurrentDate;
    vector <ItemtoPurchase> CartItems;
};
#endif

ItemToPurchase.cpp

cpp

#include <iostream>
using namespace std;
#include "ItemToPurchase.h"
ItemToPurchase::ItemToPurchase(){
    itemName = "none";
    itemDescription = "none";
    itemPrice = 0;
    itemQuantity = 0;
    return;
}
ItemToPurchase::ItemToPurchase(stringName,stringDescription,intPrice,intQuantity){
    itemName=name;
    itemDescription=description;
    itemPrice= Prices;
    itemQuantity = quantities;
    return;
}
void ItemToPurchase::SetName(stringName){
    itemName=name;
    return;
}
void ItemPurchase::SetName(stringDescription){
    itemDescription= Description;
    return;
}
void ItemToPurchase::SetName(intPrice){
    itemPrice= Prices
    return;
}
void ItemToPurchase::SetName(intQuantity){
    itemQuantity= quantities;
    return;
}
string ItemToPurchase::GetName()const{
    return itemName;
}
string ItemToPurchase::GetDescription()const{
    return itemDescription;
}
string ItemToPurchase::GetPrice()const{
    return itemPrice;
}
string ItemToPurchase::GetQuantity()const{
    return itemQuantity;
}
void ItemToPurchase::Print(){
    cout << itemName <<';' << itemDescription << endl;
}

Advertisements
Loading...

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