Nik
BAN USERKey Skills:
1. Positive Attitude
2. Eager to take new challenges
3. Optimistic
With above qualities I can work in any domain or technical field.
Technical:
C++ (main language): worked on OOPs concepts, Design Pattern, Templates, STL and Threading .
JAVA (beginner): worked on desktop application for creating client in JAVA swing/SWT using eclipse RAC framework.
OS:
UNIX: shell scripting, basic UNIX commands
Windows
Database: ORACLE
Tools worked on:
TOAD, Visual Studio, eclipse, Altova Spy
Hobbies: play guitar
Solution :
#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
vector<int> vecInt;
vector<int> vecGrpFourInt;
int multiple=1000;
int num=0;
void getFour(vector<int> vec)
{
//count combinations of four's
int size=0;
for(vector<int>::iterator it=vecInt.begin();it!=vecInt.end();it++)
{
//count to stop once 4 numbers picked
int count=0;
while(count<4)
{
//check if it is at 6th positon then once it is at 7th we should not proceed
if(vecInt.size()>=(size+4))
{
//create the number
num=num+multiple*(*it);
multiple=multiple/10;
++it;
++count;
}
else
{
count=4;
break;
}
}
//add the number created in separate vector so that we can use this in end after sorting to get max num
if(num>999)
{
vecGrpFourInt.push_back(num);
num=0;
multiple=1000;
}
// get back iterator 'it' to position it was at the begining of loop
if(vecInt.size()>=(size+4))
{
while(count>0)
{
--it;
--count;
}
}
++size;
}
vecGrpFourInt.resize(size);
}
int main()
{
//9164352435
vecInt.push_back(9);vecInt.push_back(1);vecInt.push_back(6);vecInt.push_back(4);vecInt.push_back(3);vecInt.push_back(5);vecInt.push_back(2);vecInt.push_back(4);vecInt.push_back(3);vecInt.push_back(5);
vecInt.resize(10);
getFour(vecInt);
sort (vecGrpFourInt.begin(), vecGrpFourInt.end());
int max=(vecGrpFourInt.back());
cout<<"greatest:"<<max<<endl;
vecInt.clear();
vecGrpFourInt.clear();
//9164916500
vecInt.push_back(9);vecInt.push_back(1);vecInt.push_back(6);vecInt.push_back(4);vecInt.push_back(9);vecInt.push_back(1);vecInt.push_back(6);vecInt.push_back(5);vecInt.push_back(0);vecInt.push_back(0);
vecInt.resize(10);
getFour(vecInt);
sort (vecGrpFourInt.begin(), vecGrpFourInt.end());
int max2=(vecGrpFourInt.back());
cout<<"greatest:"<<max2<<endl;
getchar();
return 0;
}
stack and heap are memory used for allocation by programs. A program has:
1. code
2. heap
3. stack
4. BSS
5. data (global and static data)
stack is used for storage of auto variables in program. When a program starts in main() all the auto variables are stored in stack using LIFO algo. Stack can grow down or UP depending on the system being used.
example code taken from other site :)
debug this program and see how memory is being allocated, this will give you a fare understanding of how stack works.
This program will give error, because in "fn()" we eat up the memory assigned to "i" and when fn() finishes that memory is freed. But we try to print "i' which has already been freed.
#include <iostream>
using namespace std;
void fn(void)
{
int * ptr;
ptr = (int *)&ptr;
++ptr;
int* mainfp = (int*)(*ptr);
--mainfp;
--mainfp;
*mainfp = 20;
return;
}
int main (void)
{
int i = 10;
fn();
cout<<”I =<<i<<endl;
return 0;
}
use binary tree to get the numbers arranged and get the count of numbers
#include<iostream>
#include<string>
#include <map>
using namespace std;
//tree node
struct TreeNode{
int data;
TreeNode *leftNode;
TreeNode *rightNode;
TreeNode(){}
TreeNode(int data){this->data=data;}
};
//binary tree
class BinaryTree{
map<int,int> sameNumbers;
TreeNode* rootNode;
//create binary tree
void createTree(TreeNode*& rootNode,int data);
void showTree(TreeNode*& root);
public:
BinaryTree(){rootNode=NULL;}
//starting point for creating tree, root is accessed from this func
void insert(int data)
{
map<int,int>::iterator it;
it=sameNumbers.find(data);
if(sameNumbers.size()>0)
{
if(it!=sameNumbers.end())
{
if(sameNumbers.find(data)->first==data)
{
sameNumbers.find(data)->second=sameNumbers.find(data)->second++;
}
}
else
{
sameNumbers.insert(pair<int,int>(data,1));
}
}
else
{
sameNumbers.insert(pair<int,int>(data,1));
}
createTree(rootNode,data);
}
//show tree
void BinaryTree::show()
{
showTree(rootNode);
}
void showMap();
};
void BinaryTree::createTree(TreeNode*& root,int data)
{
//check if root is NULL, this is called recursively for left or right side,for every new node
if(root==NULL)
{
root=new TreeNode(data);
root->leftNode=NULL;
root->rightNode=NULL;
return;
}
if(root->data<data)
{
createTree(root->leftNode,data);
}
else
{
createTree(root->rightNode,data);
}
}
void BinaryTree::showTree(TreeNode*& root)
{
//check if root is NULL, this is called recursively for left or right side,for every new node
if(root==NULL)
{
return;
}
showTree(root->leftNode);
cout<<root->data<<endl;
showTree(root->rightNode);
}
void BinaryTree::showMap()
{
cout<<endl;
int min=sameNumbers.begin()->second;
int key=sameNumbers.begin()->first;
for(map<int,int>::iterator it1=sameNumbers.begin();it1!=sameNumbers.end();++it1)
{
cout<<it1->first<<" "<<it1->second<<endl;
int key;
if(min>it1->second)
{
min=it1->second;
key=it1->first;
}
}
cout<<"number with minmum count"<<min<<" is"<<key;
}
int main()
{
BinaryTree tree;
tree.insert(2);
tree.insert(3);
tree.insert(4);
tree.insert(2);
tree.insert(3);
tree.insert(3);
tree.insert(4);
tree.insert(4);
tree.show();
tree.showMap();
getchar();
return 0;
}
}
- Nik March 29, 2012get the words from paragraph by searching for blank spaces and store the words in vector or binary tree.
If you use vector then run a loop through the vector search for same words.
If use binary tree for storing words, you can arrange words around the root..example
"RAM is a boy. Ram is a Great boy"
make ROOT= "RAM"
and arrange words in binary tree like this
if(root->data>newData)
{
//insert at left
}
else if(root->data<newData)
{
//insert at right
}
else if(root->data==data)
{
//same word found, store this in some global vector
}
Hence using Binary tree will save you resource of looping for every word in vector used for storing data.
code for creating tree modify this for storing paragraph and add check for EQUALS while creating tree.
#include<iostream>
#include<string>
using namespace std;
//tree node
struct TreeNode{
string data;
TreeNode *leftNode;
TreeNode *rightNode;
TreeNode(){}
TreeNode(string data){this->data=data;}
};
//binary tree
class BinaryTree{
TreeNode* rootNode;
//create binary tree
void createTree(TreeNode*& rootNode,string data);
void showTree(TreeNode*& root);
public:
BinaryTree(){rootNode=NULL;}
//starting point for creating tree, root is accessed from this func
void insert(string data)
{
createTree(rootNode,data);
}
//show tree
void BinaryTree::show()
{
showTree(rootNode);
}
};
void BinaryTree::createTree(TreeNode*& root,string data)
{
//check if root is NULL, this is called recursively for left or right side,for every new node
if(root==NULL)
{
root=new TreeNode(data);
root->leftNode=NULL;
root->rightNode=NULL;
return;
}
if(root->data[0]<data[0])
{
createTree(root->leftNode,data);
}
else
{
createTree(root->rightNode,data);
}
}
void BinaryTree::showTree(TreeNode*& root)
{
//check if root is NULL, this is called recursively for left or right side,for every new node
if(root==NULL)
{
return;
}
showTree(root->leftNode);
cout<<root->data<<endl;
showTree(root->rightNode);
}
int main()
{
BinaryTree tree;
tree.insert("cd");
tree.insert("6");
tree.insert("asd");
tree.insert("1");
tree.insert("B");
tree.insert("4");
tree.insert("5");
tree.insert("avs");
tree.insert("A");
tree.show();
getchar();
return 0;
}
You can create a Binary tree which will sort the array in the required format.
example below I am adding strings you can convert that to char and work. But algo should remain same.
//Tree Nodes
#include<iostream>
#include<string>
using namespace std;
//tree node
struct TreeNode{
string data;
TreeNode *leftNode;
TreeNode *rightNode;
TreeNode(){}
TreeNode(string data){this->data=data;}
};
//binary tree
class BinaryTree{
TreeNode* rootNode;
//create binary tree
void createTree(TreeNode*& rootNode,string data);
void showTree(TreeNode*& root);
public:
BinaryTree(){rootNode=NULL;}
//starting point for creating tree, root is accessed from this func
void insert(string data)
{
createTree(rootNode,data);
}
//show tree
void BinaryTree::show()
{
showTree(rootNode);
}
};
void BinaryTree::createTree(TreeNode*& root,string data)
{
//check if root is NULL, this is called recursively for left or right side,for every new node
if(root==NULL)
{
root=new TreeNode(data);
root->leftNode=NULL;
root->rightNode=NULL;
return;
}
if(root->data[0]<data[0])
{
createTree(root->leftNode,data);
}
else
{
createTree(root->rightNode,data);
}
}
void BinaryTree::showTree(TreeNode*& root)
{
//check if root is NULL, this is called recursively for left or right side,for every new node
if(root==NULL)
{
return;
}
showTree(root->leftNode);
cout<<root->data<<endl;
showTree(root->rightNode);
}
int main()
{
BinaryTree tree;
tree.insert("cd");
tree.insert("6");
tree.insert("asd");
tree.insert("1");
tree.insert("B");
tree.insert("4");
tree.insert("5");
tree.insert("avs");
tree.insert("A");
tree.show();
getchar();
return 0;
}
//tree implementation
//Tree Nodes
#include<iostream>
#include<string>
using namespace std;
//tree node
struct TreeNode{
string data;
TreeNode *leftNode;
TreeNode *rightNode;
TreeNode(){}
TreeNode(string data){this->data=data;}
};
//binary tree
class BinaryTree{
TreeNode* rootNode;
//create binary tree
void createTree(TreeNode*& rootNode,string data);
void showTree(TreeNode*& root);
public:
BinaryTree(){rootNode=NULL;}
//starting point for creating tree, root is accessed from this func
void insert(string data)
{
createTree(rootNode,data);
}
//show tree
void BinaryTree::show()
{
showTree(rootNode);
}
};
void BinaryTree::createTree(TreeNode*& root,string data)
{
//check if root is NULL, this is called recursively for left or right side,for every new node
if(root==NULL)
{
root=new TreeNode(data);
root->leftNode=NULL;
root->rightNode=NULL;
return;
}
if(root->data[0]<data[0])
{
createTree(root->leftNode,data);
}
else
{
createTree(root->rightNode,data);
}
}
void BinaryTree::showTree(TreeNode*& root)
{
//check if root is NULL, this is called recursively for left or right side,for every new node
if(root==NULL)
{
return;
}
showTree(root->leftNode);
cout<<root->data<<endl;
showTree(root->rightNode);
}
int main()
{
BinaryTree tree;
tree.insert("cd");
tree.insert("b");
tree.insert("c");
tree.insert("f");
tree.insert("z");
tree.insert("zaas");
tree.insert("avs");
tree.show();
getchar();
return 0;
}
Example of threaded read and write.
I have a code where thread pool periodically updates a value and 2 separate threads periodically read the current value.
build command: g++ -g -o readWrite readWrite.cpp -I/usr/local/include -lboost_thread -L/usr/local/lib
- Nik October 07, 2013