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

Multiply Woot

#include <iostream>

using namespace std;

// Multiplication woot!
int main()
{
   cout << "Enter a number";
   int woot{0};
   cin >> woot;
   
   return 0;
}

cache sim

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include<time.h>

using namespace std;


struct cache_line {
    
  bool valid;
  uint32_t tag;
  ushort offset;
  uint index;
  

};

struct add_t{
  uint32_t addrs;
  unsigned int ref_num;
  uint32_t next_addrs;
  ushort next_addrs_num;
  /*
  //simulation types are:
  1. sequential
  2. conflict miss
  3. capacity miss
  4. random access
  */
  ushort sim_type;
  
    
    
};

struct cache_t{
  
  cache_line *lines; 
    
};


cache_t build_cache(int size){
    
    cache_t newCache;
    cache_line line;
    
    newCache.lines = (cache_line *) malloc (sizeof(line) * 256);
    
    for(int i = 0; i < 256; i++){
        
        line.valid = 0;
        line.tag = 0;
        line.index = 0;
        line.offset = 0;
        
        newCache.lines[i] = line;
        
    }
    
    
    return newCache;
    

    
}

struct sim_param{
  
  int hit;
  int miss;
  int evict;
    
};


void cache_sim(uint32_t address, cache_t & cache, sim_param & param){
    
    
    uint32_t tag;
    uint32_t index;
    uint32_t offset;
    
    cache_line tmpLine;
    
    
    
    //tag is the 20 bits MSB of the address
    tag = address & 0xFFFFF000;
    tag = tag >> 12;
    //index is 8 bit from bit 5 ot bit 11 of the address
    index = address & 0x00000FF0;
    index = index >> 4;
    //offset is the 4 bit LSB of the address
    offset = address & 0x0000000F;
    
    
    tmpLine = cache.lines[index];
    //case of there is no block in this line
    if(tmpLine.valid == false){
        cout<<"was not here"<<endl;
    }
    //case of cache hit
    else if(tmpLine.valid == true && tmpLine.tag == tag){
        cout<<"it was in the cache"<<endl;
    }
    //case of cache conflict
    else if(tmpLine.valid == true && tmpLine.tag != tag){
        cout<<"cache miss here"<<endl;
    }
    
    
    
    
    
}

void addressGen(const int ran, add_t & sim_add){
    
    uint32_t result;
    uint32_t addrs;
    ushort num;
    uint32_t next_add;
    ushort next_add_num;
    ushort sim_case;
    uint32_t index;
    uint32_t new_index;
    uint32_t add_range_st = 0;
    uint32_t max_address = 0xFFFFFFFF;
    uint32_t new_address;
    
    
    
  /*  

  uint32_t addrs;
  unsigned int ref_num;
  uint32_t next_addrs;
  ushort next_addrs_num;
  */
  /*
  //simulation types are:
  1. sequential
  2. conflict miss
  3. capacity miss
  4. random access
  */
  //ushort sim_type;
    
    srand(time(0));
    addrs = rand();
    sim_add.addrs = addrs;
    switch(ran){
        
        //sequential access
        case 1:
            
            srand(addrs);
            num = (rand() % (99));
            sim_add.ref_num = num;
            next_add = addrs + 1;
            sim_add.next_addrs = next_add;
            next_add_num = (rand() % (99));
            sim_add.next_addrs_num = next_add_num;
            sim_add.sim_type = 1;
            
            
            
            break;
        
        //capacity miss    
        case 2:
    
            index = addrs & 0x00000FF0;
            index = index >> 4;
            new_index = 255 - index;
            new_index = new_index << 4;
            add_range_st = addrs & 0xFFFFF00F;
            add_range_st = add_range_st | new_index;
            srand(addrs);
            num = 0;
            sim_add.ref_num = num;
            uint32_t range;
            range = (max_address + 1 - add_range_st);
            next_add = (rand() % (range));
            sim_add.next_addrs = next_add;
            next_add_num = (rand() % (255));
            sim_add.next_addrs_num = next_add_num;
            sim_add.sim_type = 2;
            
            
            
            break;
        
        //cache conflict
        case 3:
            
            
            index = addrs & 0x00000FF0;
            
            srand(addrs);
            
            
            num = 0;
            sim_add.ref_num = num;
            new_address = rand();
            next_add = new_address & 0xFFFFF00F;
            next_add = next_add | index;
            sim_add.next_addrs = next_add;
            next_add_num = (rand() % (99));
            
            sim_add.next_addrs_num = next_add_num;
            sim_add.sim_type = 3;
            
            break;
            
        //random access
        case 4:
            
            
            num = 0;
            sim_add.ref_num = num;
            srand(addrs);
            next_add = rand();
            sim_add.next_addrs = next_add;
            next_add_num = (rand() % (99));
            sim_add.next_addrs_num = next_add_num;
            sim_add.sim_type = 4;
            
            break;
        
        default:
            
            cout << "it is default \n";
        
    }
    
    
    
    return ;
    
    
}

void run_sim(const int case_num, add_t & sim_info,cache_t & cache, sim_param & param){
    
    uint32_t add;
    uint32_t next_add;
    uint32_t index;
    uint32_t new_index;
    uint32_t add_range_st;
    uint32_t max_address = 0xFFFFFFFF;
    uint32_t new_address;
    unsigned int num;
    unsigned int next_num;
    unsigned int sim_ite;
    int i;
    
     /*  

  uint32_t addrs;
  unsigned int ref_num;
  uint32_t next_addrs;
  ushort next_addrs_num;
  */
  /*
  //simulation types are:
  1. sequential
  2. conflict miss
  3. capacity miss
  4. random access
  */
  //ushort sim_type;
    
    add = sim_info.addrs;
    num = sim_info.ref_num;
    next_num = sim_info.next_addrs_num;
    
    sim_ite = next_num + num;
    
    switch(case_num){
        
        case 1:
            for(i = 0; i < sim_ite; i++){
                
                if(sim_ite < num){
                    cache_sim(add, cache, param);
                }
                else{
                    add = add + 0x10;
                    cache_sim(add, cache, param);
                }
                
                
            }
            break;
        //capacity miss
        case 2:
            
            for(i = 0; i < sim_ite; i++){
                
                if(sim_ite < num){
                    cache_sim(add, cache, param);
                }
                else{
                    
                    index = add & 0x00000FF0;
                    index = index >> 4;
                    new_index = 255 - index;
                    new_index = new_index << 4;
                    add_range_st = add & 0xFFFFF00F;
                    add_range_st = add_range_st | new_index;
                    srand(add);
                   
                   
                    uint32_t range;
                    range = (max_address + 1 - add_range_st);
                    next_add = (rand() % (range));
                    cache_sim(next_add, cache, param);
                    
                    
                    
                }
            }
            
            break;
        //cache conflict
        case 3:
            for(i = 0; i < sim_ite; i++){
                
                if(sim_ite < num){
                    cache_sim(add, cache, param);
                }
                else{
                    
                    index = add & 0x00000FF0;
            
                    srand(add);
                    
                    
                   
                    
                    new_address = rand();
                    next_add = new_address & 0xFFFFF00F;
                    next_add = next_add | index;
                    cache_sim(next_add, cache, param);
                    
                   
                    
                }
            }
            
            
            break;
            
        case 4:
            
            for(i = 0; i < sim_ite; i++){
                
                if(sim_ite < num){
                    cache_sim(add, cache, param);
                }
                else{
                    srand(add);
                    next_add = rand();
                    cache_sim(next_add, cache, param);
                }
            }
            
            break;
            
        default:
            cout<<"it is default" << endl;
        
        
    }
}


int main()
{

   cout << "Hello World" << endl; 
   uint32_t a = 4892950101;
   uint32_t tag;
   uint index;
   uint offset;
   uint32_t tmp = a;
   
   cache_t cache;
   sim_param *param;
   
   param = (sim_param *) malloc (sizeof(sim_param));
   cache = build_cache(256);
   
   //add_t * gen_ad;
   add_t * gen_ad;
   
   srand(time(0));
   
   int upper, lower;
   int number_of_iteration = 100;
   //int * sim_case = (int *) malloc(number_of_iteration * sizeof(int));
   int sim_case = 0;
   //gen_ad = (add_t *) malloc(number_of_iteration * sizeof(add_t));
   gen_ad = (add_t *) malloc(sizeof(add_t));
   upper = 4;
   lower = 1;
   
   
   for(int i = 0; i < number_of_iteration; i++){
       sim_case = (rand() % (upper - lower + 1)) + lower;
       addressGen(sim_case, *gen_ad);
       
       run_sim(sim_case, *gen_ad, cache, *param);
       
   }
   
   
   
   
   cout<<a<<endl;
   printf("0x%08x\n", a);
   
   
   tmp = a & 0xFFFFF000;
   
   tag = tmp >> 12;
   
   tmp = a & 0x00000FF0;
   
   index = tmp >> 4;
   
   tmp = a & 0x0000000F;
   
   offset = tmp;
   
   
   
   uint32_t tmp_2;
   tmp_2 = a % 256;
   
   cout << "this is tmp_2 " << tmp_2 <<endl;
   cout<<"this is index: "<<index<<endl;
   
   ushort my_ran;
   srand(time(0));
   
   //cout<<time(0)<<endl;
   my_ran = rand();
   
   cout<<"the first random: "<<my_ran<<"version 2 "<<my_ran*100<<endl;
   //srand(time(0));
   //cout<<time(0)<<endl;
   ushort num = (rand() % (999));
   //my_ran = rand();
   cout<<"the second random: "<<num<<endl;
   
   
   
   return 0;
}

c++11, lambda, capture local variable, by reference or value

#include <iostream>



#include <string>
#include <functional>
 
using namespace std;
 
std::function<void ()> getCallBack()
{
	// Local Variable
	int counter = 10;
 
	// Defining Lambda function and
	// Capturing Local variables by Reference
		
//frank fix	
//auto func = [&counter]  () mutable {
		    
	auto func = [&counter]  () mutable {
		std::cout<<"Inside Lambda :: counter = "<<counter<<std::endl;
 
		// Change the counter
		// Change will affect the outer variable because counter variable is
		// captured by Reference in Lambda function
		counter = 20;
 
		std::cout<<"Inside Lambda :: After changing :: counter = "<<counter<<std::endl;
 
		};
 
	return func;
}
 
int main(int argc, char **argv)
{
 
	std::function<void ()> func = getCallBack();
 
	//Call the Lambda function
	func();
 
	// Lambda function will access and modify the variable that has been captured it by reference
	// But that variable was actually a local variable on stack which was removed from stack when getCallbacK() returned
	// So, lambda function will basically corrupt the stack
 
	return 0;
}

33233

#include <iostream>

using namespace std;

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

Linked list operations

#include <iostream>

using namespace std;
struct node
{
    int data;
    struct node* next;
};
int getCount(struct node* head)
{
    int count=0;
    struct node *current = head;
    if(head == NULL)
        return 0;
    else
    {
        while(current!= NULL)
        {
            current = current->next;
            count++;
        }
    }
    return count;
}
void insert1(struct node** head,int data)
{
    struct node* new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = data;
    new_node->next = NULL;
    if(head==NULL)
    {
        *head = new_node;
    }
    else
    {
        new_node->next = *head;
        *head = new_node;
    }
}
void insert2(struct node** head,int data)
{
    struct node* new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = data;
    new_node->next = NULL;
    struct node *current = *head;
    if(head == NULL)
    {
        *head = new_node;
        return;
    }
    while(current->next!=NULL)
    {
        current = current->next;
    }
    current->next = new_node;
}
void insert3(struct node** head,int data,int pos)
{
    struct node* new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = data;
    new_node->next = NULL;
    struct node *current = *head;
    int count = getCount(*head);
    cout<<count<<endl;
    if(pos<1 || pos>count)
    {
        cout<<"Invalid position  "<<pos<<endl;
        return;
    }
    if(pos==1)
    {
        new_node->next = *head;
        *head = new_node;
        return;
    }
    for(int i=1;i<pos-1;i++)
    {
        current = current->next;
    }
    new_node->next = current->next;
    current->next = new_node;
}
void display(struct node* head)
{
    while(head!=NULL)
    {
        cout<<head->data<<" ";
        head = head->next;
    }
}
void delete1(struct node* head)
{
    struct node *temp = head;
    //struct node *current;
    if(head==NULL)
    {
        cout<<" LIST is Empty "<<endl;
        return;
    }
    //current = temp;
    head = head->next;
    delete temp;
    //*head = temp;
}
int main()
{
    struct node* head = NULL;
    insert1(&head,2);
    insert1(&head,3);
    insert2(&head,6);
    insert2(&head,5);
    insert3(&head,8,3);
    display(head);
    delete1(head);
    cout<<endl;
    display(head);
    insert1(&head,9);
    cout<<endl;
    display(head);
   return 0;
}

C++ Tutorial

#include <iostream>

using namespace std;

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

Compile and Execute C++11 Online

#include<iostream>
using namespace std;

struct list{
    int data;
  struct list* next;  
};
struct block{
    struct list* head;
    int size;
    struct block * next;
};
int blocksize=4;

struct block * emptyblock(){
    struct block * tempBlock;
    tempBlock = (struct block *)malloc(sizeof(struct block));
    tempBlock->head = NULL;
    tempBlock->next = NULL;
    tempBlock->size = 0;
    return tempBlock;
}
struct list* insertAtBegining(struct block* blockhead){
    struct list * temp =(struct list *)malloc(sizeof(struct list));
    temp->next =blockhead->head;
    cin >> temp->data;
    blockhead->head = temp;
    blockhead->size++;
    return blockhead->head;
}
int main()
{
    struct block * blockhead =NULL;
    blockhead = emptyblock();
    blockhead->head=insertAtBegining(blockhead);
    
    cout << "Hello World" << endl; 
   
   return 0;
}

C+11, std::transform, lambda, function class

#include <iostream>

#include <algorithm>
#include <vector>
#include <iterator>

using namespace std;

int op_increase (int i) { return ++i; }

class NeedIt
{
  public:
  bool operator() (int x)
  {
      return x>40;
  }
    
};

int main()
{
   cout << "Hello World" << endl; 
   
   vector<int> v = {10, 20, 30, 40, 50};
   
   std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, "|") );
   
   std::cout<<std::endl;
   
   //vector<int> v2(v.size());
   //std::transform(v.begin(), v.end(), v2.begin(), [](int x){return x*100;} );
   
   vector<int> v2;
   std::transform(v.begin(), v.end(), back_inserter(v2), [](int x){return x*2;} );

   std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, "|") );
    
    std::cout<<std::endl;

   vector<int> v3;
   std::transform(v.begin(), v.end(), back_inserter(v3), NeedIt() );
   //std::transform(v.begin(), v.end(), back_inserter(v3), [](int y){return (y>25);} );
   std::copy(v3.begin(), v3.end(), std::ostream_iterator<int>(std::cout, "|") );





std::cout<<std::endl;




    
std::vector<int> foo;
std::vector<int> bar;




  // set some values:
  for (int i=1; i<6; i++)
    foo.push_back (i*10);                         // foo: 10 20 30 40 50

  bar.resize(foo.size());                         // allocate space

  std::transform (foo.begin(), foo.end(), bar.begin(), op_increase);
                                                  // bar: 11 21 31 41 51

  // std::plus adds together its two arguments:
  std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>());
                                                  // foo: 21 41 61 81 101

  std::cout << "foo contains:";
  for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';
  
   return 0;
}

hello world

#include <iostream>

using namespace std;

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

typedef

#include <iostream>
#include <vector>
#include <sstream>

// Type: struct account
struct account {
     int account_number;
     char first_name[50];
     char last_name[50];
     float balance;
};

// if you don't want to write "struct"
// make an alias for type "struct account"
typedef struct account account_t;

// here we define a new type "account2"
typedef struct account2_t {
     int account_number;
     char first_name[50];
     char last_name[50];
     float balance;
};

// alias for an unnamed type (no forward declaration)
typedef struct {
     int account_number;
     char first_name[50];
     char last_name[50];
     float balance;
} account3_t;

int main()
{
    struct account my_account = {666, "Giovanni", "San Marzano", 0.3};
    std::cout << my_account.last_name << "\n";
    
    account_t my_other_account = {666, "Franz", "Feingeist", 0.3};
    std::cout << my_other_account.last_name << "\n";

    account2_t my_account2 = {666, "Sepp", "Wuxacher", 0.3};
    std::cout << my_account2.last_name << "\n";
    
    account3_t my_account3 = {666, "Federico", "Malbona", 0.3};
    std::cout << my_account3.last_name << "\n";
}

Advertisements
Loading...

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