bharath.paturi
BAN USER53 is not equal to 9*3 + 6 * 4, it is 51
- bharath.paturi March 30, 2014void reverseString(string [], int);
void sortString(string [], int);
int _tmain(int argc, _TCHAR* argv[])
{
string strArray[] = {"zebra", "monkey", "cat", "donkey"};
int len = sizeof(strArray)/sizeof(string);
reverseString(strArray, len);
sortString(strArray, len);
reverseString(strArray, len);
for (int i = 0; i < len; i++)
{
cout << strArray[i] << endl;
}
return 0;
}
//Sorting strings in an array
void sortString(string arr[], int len)
{
string temp;
for (int i = 0; i < len; i++)
{
for(int j = i + 1; j < len; j++)
{
if (arr[i].compare(arr[j]) > 0)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
//Reversing the strings in an array.
void reverseString(string arr[], int len)
{
/*for (int i = 0; i < sizeof(arr); i++)
{
for (int j = strlen((*(arr + i))) - 1; j >= 0; j--)
{
cout << *(*(arr + i) + j) << endl;
}
cout << endl << endl;
} */
char *revStr = 0;
char *origStr = 0;
int k;
for (int i = 0; i < len; i++)
{
k = 0;
revStr = new char [arr[i].size() + 1];
origStr = new char [arr[i].size()+ 1];
strcpy(revStr, arr[i].c_str());
strcpy(origStr, arr[i].c_str());
for( int j = (int)strlen(origStr) - 1; j >= 0; j--)
{
*(revStr + k) = *(origStr + j);
k++;
}
// cout << revStr << endl;
arr[i] = revStr;
delete [] revStr;
delete [] origStr;
}
}
int rectArea = 0;
int arr[5] = {10, 20, 30, 50, 40};
for (int i = 0; i < 5; i++)
{
if ( ((i + 1) * arr[i]) > rectArea)
rectArea = (i + 1) * arr[i];
}
cout << "largest rectangle is " << rectArea << endl;
// this solves in O(n)
1 - 1/2 * 1/2 * 1/2 * 1/2 => 15/16
- bharath.paturi May 23, 2011class dbList
{
public:
int item;
dbList *left;
dbList *right;
public:
dbList(int val)
{
item = val;
left = NULL;
right = NULL;
}
};
class treeNode
{
public:
int item;
treeNode *left;
treeNode *right;
public:
treeNode(int value)
{
item = value;
left = NULL;
right = NULL;
}
};
void constructBTree(treeNode& , int);
void inorderPrint(treeNode *);
void addList(int);
dbList *node = NULL;
dbList *startList = NULL;
dbList *prevList = NULL;
int _tmain(int argc, _TCHAR* argv[])
{
//construct a Binary Tree;
treeNode *root = new treeNode(100);
treeNode *strtRoot = root;
constructBTree(*strtRoot, 50);
strtRoot = root;
constructBTree(*strtRoot, 75);
strtRoot = root;
constructBTree(*strtRoot, 25);
strtRoot = root;
constructBTree(*strtRoot, 175);
//convert tree in to a doubly linked list.
//Parse through tree and add double linked list nodes.
inorderPrint(root);
for (dbList *hdr = node; hdr != NULL; hdr = hdr->left)
cout << hdr->item << " ";
return 0;
}
void addList(int val)
{
if (node != NULL)
{
if (node == startList)
{
node->right = new dbList(val);
prevList = node;
node = node->right;
node->left = prevList;
}
else
{
prevList = node;
node->right = new dbList(val);
node = node->right;
node->left = prevList;
}
}
else
{
node = new dbList(val);
startList = node;
}
}
void constructBTree(treeNode& root, int val)
{
/*if (root == NULL)
return;*/
if (root.item >= val && root.left == NULL)
root.left = new treeNode(val);
else if (root.item >= val)
constructBTree(*(root.left), val);
else if (root.item < val && root.right == NULL)
root.right = new treeNode(val);
else if (root.item < val)
constructBTree(*(root.left), val);
}
void inorderPrint( treeNode *root )
{
// Print all the items in the tree to which root points.
// The items in the left subtree are printed first, followed
// by the item in the root node, followed by the items in
// the right subtree.
if ( root != NULL )
{ // (Otherwise, there's nothing to print.)
inorderPrint( root->left ); // Print items in left subtree.
//cout << root->item << " "; // Print the root item. // 25, 50, 75, 100, 175
addList(root->item);
inorderPrint( root->right ); // Print items in right subtree.
}
} // end inorderPrint()
Reppaulajheineman, Consultant at Bank of America
Hello, I am from Las Vegas, NV.Completed a Diploma course in Advanced Technological Developments and Staff Management. I have ...
- bharath.paturi April 09, 2014