Category: OOMP Codes

Matrix Multiplication – Using Multicore Programming (C++)

/* Write a program for Matrix Multiplication using multicore programming. */ #include<iostream> #include<pthread.h> using namespace std; int MAT1[10][10],MAT2[10][10],MAT3[10][10]; int r1=3,c1=3,r2=3,c2=3; void *multi(void *p) { int i=0; i= (int &)p; for(int j=0;j<3;j++) { //MAT3[i][j]=0; for(int k=0;k<3;k++) { MAT3[i][j]=MAT3[i][j]+(MAT1[i][k]*MAT2[k][j]); } } } void read_mat(int MAT[10][10], int r, int c) { cout<<"Enter the elements in matrix...: "<<endl; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { cout<<"Enter the elements for row and column here: "<<endl; cin>>MAT[i][j]; } } } void print_mat(int MAT[10][10], int r, int c) { cout<<"\n"; for(int i=0;i<r;i++) { cout<<"\n"; for(int j=0;j<c;j++) { cout<<MAT[i][j]<<"\t"; } cout<<"\n"; } } main() { pthread_t tid[3]; read_mat(MAT1,r1,c1); read_mat(MAT2,r2,c2); ... Read more

Passing command line arguement to thread functions (C++)

Write object oriented program using C++ for passing command line arguement to thread functions and using command line arguement to determine number of threads to be created. Use multicore programming.

Multicore Programming – Producer Consumer problem solved by threads (C++)

#include <iostream> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> using namespace std; pthread_mutex_t status; sem_t empty,full; int buffer[50],x,item; void initialization() { sem_init(&empty,1,0); sem_init(&full,1,50); pthread_mutex_init(&status,NULL); x=0; } void* producer(void *para) { item=rand()%100; sem_wait(&full); //Producer waits if buffer is full pthread_mutex_lock(&status); //Lock the status, so that only one thread can access buffer at a time buffer[x]=item; //producer enters the no. in the buffer cout<<"\nProducer has producer : "<<buffer[x]; x++; pthread_mutex_unlock(&status); //Unlocks the buffer sem_post(&empty); } void* consumer(void *para) { sem_wait(&empty);//if buffer is empty,consumer waits pthread_mutex_lock(&status);//Lock the status, so that only one thread can access buffer at a time x--; item=buffer[x];//Consumer consumes an element ... Read more

Multicore Programming – using posix_spawn() to create processes (C++)

/* Using multicore programming implement posix_spawn() function to create a process. */ /*Execution: oct@CCOMPL08-10:~$ g++ templ.cpp -o mat.o oct@CCOMPL08-10:~$ g++ multicore.cpp -lpthread oct@CCOMPL08-10:~$ ./a.out */ #include<iostream> #include<spawn.h> #include<sys/types.h> #include<sys/wait.h> using namespace std; int main(int argc, char *argv[], char *env[]){ pid_t pid, pid1; int status, status1; status = posix_spawn(&pid, "/bin/ls", NULL, NULL, argv, env); cout<<"------------------------ \n"; if(status == 0) cout<<"Process created successfully... \n"; cout<<"------------------------ \n"; wait(&pid); status1 = posix_spawn(&pid1, "mat.o", NULL, NULL, argv, env); cout<<"------------------------ \n"; if(status1 == 0) cout<<"Process created successfully... \n"; cout<<"------------------------ \n"; wait(&pid1); return 0; } View Article Page Download

Standard Template Library – Lists (C++)

Google standard template library to use list container and using C++ implement following member functions of list class: empty, insert,merge,reverse,sort

Bubble Sort – using Function Template (C++)

<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>/* Implement C++/Java/Python program for bubble sort using function template */ #include<iostream> using namespace std; template <typename X> void show(int n,X arr[]); template <typename T> void bubblesort(int n,T arr[]) { T temp; cout<<"\n Before sorting elements are:"; show(n,arr); for(int i=0;i<n-1;i++) { for(int j=0;j<n-i-1;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } cout<<"after sorting: \n"; show(n,arr); } template <class X> void show(int n,X arr[]) { cout<<endl; for(int k=0;k<n;k++) { cout<<arr[k]<<"\t"; } } int main() { int iarr[100],ch,n; float farr[100]; do { cout<<"\n \t BUBBLE SORT"; cout<<"\n ———————————"; cout<<"\n 1.Integer elements\n ... Read more

Exception Handling (C++)

  /* Create a c++ class named Television that has data members to hold the model no., screen size and price. Member functions include overloaded insertion and extraction operators. If more than 4 digits are entered call the model if screen size is smaller than 12 and greater than 70 inches or if price is negative or over $5000 then provide the . Write a main function that instantiates a television object which allows users to enter data and display data members. If an exception is caught then replace all data members with 0. */ #include<iostream> using namespace std; class ... Read more

File Operations, Derived Class & Virtual Class illustration (C++)

Design a C++ base class consisting of the data members. The derived class consists of the data members. Construct a virtual base class. The program should have the facilities. i) Build a master table ii) List a table iii) Insert a new entry iv) Delete old entry v) Edit an entry vi) Search for a record

Matrix Operations using Function Template (C++)

Write a program in C++ using function template to read two matrices of different data types such as integers and floating point values and perform simple arithmetic operations on these matrices separately and display it.

String Operations using Operator Overloading (C++)

String operations include: equality, copy, reverse, display, substrings and palindrome check using operator overloading.