https://www.google.com/contributor/welcome/?utm_source=publisher&utm_medium=banner&utm_campaign=2376261057261409

Search This Blog

Search with our Site

Custom Search

Friday, December 4, 2015

CS301 Assignment No 02 Solution & Discussion Due Date Dec 03, 2015


Text Box: };Assignment Solution Change it according to your university requirement before submitting #include <stdlib.h> #include <iostream> using namespace std; template <class Object>
class TreeNode {
public:
TreeNode()        {
this->object = NULL;
this->left = this->right = NULL;
};
TreeNode( Object * object ) { this->object = object;
this->left = this->right = NULL;
};
Object * getInfo()
{
return this->object;
};
void setInfo(Object * object) {
this->object = object;

TreeNode * getLeft() {
return left;
};
void setLeft(TreeNode * left)
{
this->left = left;
};
TreeNode * getRight()
{
return right;
};
void setRight(TreeNode * right)
{
this->right = right;
};
int isLeaf( )
{
if( this->left == NULL && this->right == NULL )
return 1;
return 0;
};
private:
Object * object;

Text Box: int getNodeLevel(TreeNode<int>* root, int data,int level)TreeNode * left;
TreeNode * right;
};
void insert (TreeNode <int> * root, int * info)
{
TreeNode <int> * node = new TreeNode <int> (info);
TreeNode <int> * p, * q;
p = q = root;
while( *info != *(p->getInfo()) && q != NULL )
{
p = q;
if( *info < *(p->getInfo()) )
q = p->getLeft(); else
q = p->getRight();
}
if( *info == *( p->getInfo() ) )
{
cout << "attempt to insert duplicate: " << *info ;
delete node;
}
else if( *info < *(p->getInfo()) )
p->setLeft( node );
else
p->setRight( node );
} // end of insert

{
if (root == NULL)
return 0;
if (*(root->getInfo()) == data)
return level;
int downlevel = getNodeLevel(root->getLeft(), data, level+1);
if (downlevel != 0)
return downlevel;
downlevel = getNodeLevel(root->getRight(), data, level+1);
return downlevel;
}
void printGivenRow(TreeNode<int>* root, int level)
{
if(root == NULL)
return;
if(level == 1)
cout << *(root->getInfo())<<" ";
else if (level > 1)
{
printGivenRow(root->getLeft(), level-1);
printGivenRow(root->getRight(), level-1);
}
}

Text Box: {int height(TreeNode<int>* node)

if (node==NULL)
return 0;
else
{
/* compute the height of each subtree */
int lheight = height(node->getLeft());
int rheight = height(node->getRight());
/* use the larger one */ if (lheight > rheight) return(lheight+1); else return(rheight+1);
}
}
Text Box: {
}
}
void printRow(TreeNode<int>* root)
int h = height(root);
int i;
for(i=1; i<=h; i++){
cout<<"At Row "<<i-1<< " :"; printGivenRow(root, i);
cout<<endl;
void carInfo(TreeNode<int>* root)
{
int h = height(root);

int i;
for(i=1; i<=h; i++){
cout<<"Car ";
printGivenRow(root, i); cout<<endl;
}
}
/* Returns level of given data value */
int getLevel(TreeNode<int>* node, int data)
{ return getNodeLevel(node,data,1);
}
int main(int argc, char * argv[])
{
int x[] = {14, 15, 4, 9, 7, 18, 3, 5, 16, 20, 17};
TreeNode <int> * root = new TreeNode<int>();
root->setInfo( &x[0] );
for(int i = 1; x[i] > 0; i++ )
{
insert( root, &x[i] );
}
int i =0;
for (i = 0; i <=10; i++)
{
int data = x[i];

int level = getLevel(root, data);
if (level)
cout<<" Car "<< data <<" is at Row : "<< (getLevel(root, data)-1)<<endl;
}
cout<<endl; printRow(root);
}
Compiled Result







Fall 2015 CS2 01 Assignment 2 Solution



Text Box: }Page 1 of 2
#include<ctype.h> #include<iostream> #include<string.h> using namespace std;
void convertToUppercase (char *);
main() {

char firstString[50];
char secondString[50];
char uppercaseFirstString[50];
char uppercaseSecondString[50];
char concatString[100];
cout<<"Enter String 1 : ";
cin>>firstString;
cout<<"Enter String 2 : ";
cin>>secondString;
cout<<"\n\n";
cout<<"The Length Of String 1 is : "<<strlen(firstString);
cout<<"\n";
cout<<"The Length Of String 2 is : "<<strlen(secondString);
strcpy(uppercaseFirstString,firstString ); strcpy(uppercaseSecondString,secondString);
convertToUppercase(uppercaseFirstString); convertToUppercase(uppercaseSecondString);
cout<<"\n\n";
cout<<"String 1 in upper case : "<<uppercaseFirstString<<"\n";
cout<<"String 2 in upper case : "<<uppercaseSecondString<<"\n";
cout<<"\n\n";
if(strcmp(uppercaseFirstString,uppercaseSecondString)==0) {
cout<<"Both strings are the same \n";
} else {
cout<<"Both strings are different \n";

Page 2 of 2
strcpy(concatString,firstString); strcat(concatString,secondString); cout<<"\n\n";
cout<<"Both Strings after Concatenation : " <<concatString;
return 0;
}
void convertToUppercase (char *sptr)
{
while ( *sptr != '\0' )
{
if (islower(*sptr) )
*sptr = toupper ( *sptr );
++ sptr;
}
}