Sunday 9 December 2018

CS304 - Object Oriented Programming Fall 2018 Assignment No. 01 Solution

Leave a Comment


Problem Statement:
We have the following part of class diagram showing aggregation relationship:

CS304 - Object Oriented Programming  Fall 2018 Assignment No. 01 Solution

You are required to implement above class diagram (complete program) in C++ with all data members, constructors, member functions and concept (aggregation) given in the class diagram/table 


Class Name
Functions

Grocery Home
-Default constructor



Fruit Store
-Default constructor

Grocery Store
-Default constructor


Dairy Store
-Default constructor


Apple
-Default constructor


Grapes
-Default constructor


Guava
-Default constructor


Tetra Milk
-Default constructor


Whitener
-Default constructor


Cream
-Default constructor


Cooking Oil
-Default constructor


Rice
-Default constructor


Flour
-Default constructor




Solution or Download.cpp

#include <iostream>
using namespace std;
class Apple{
 public:
 Apple(){
  cout<<"Apple Constructor called\n";
 }
};
class Grapes{
 public:
 Grapes(){
  cout<<"Grapes Constructor called\n";
 }
};
class Guava{
 public:
 Guava(){
  cout<<"Guava Constructor called\n";
 }
};
class FruitStore{
 Apple a;
 Grapes g;
 Guava gv;
 public:
  FruitStore(){
   cout<<"\nFruit Store Constructor called\n\n";
   
  }
};
class CookingOil{
 public:
 CookingOil(){
  cout<<"Cooking Oil Constructor called\n";
 }
}; 
class Rice{
 public:
 Rice(){
  cout<<"Rice Constructor called\n";
 }
};
class Flour{
 public:
 Flour(){
  cout<<"Flour Constructor called\n";
 }
};
class GroceryStore{
 CookingOil c;
 Rice r;
 Flour f;
 public:
 GroceryStore(){
 cout<<"\nGrocery Store Constructor called\n\n";
 
 }
};
class TetraMilk{
 public:
 TetraMilk(){
  cout<<"Tetra Milk Constructor called\n";
 }
};
class Whitener{
 public:
 Whitener(){
  cout<<"Whitener Constructor called\n";
 }
};
class Cream{
 public:
 Cream(){
  cout<<"Cream Constructor called\n";
 }
};
class DairyStore{
 TetraMilk t;
 Whitener w;
 Cream c;
 public:
 DairyStore(){
  cout<<"\nDairy Store Constructor called\n\n";
  
 }
};
class GroceryHome{
 FruitStore f;
 GroceryStore g;
 DairyStore d;
 public:
  GroceryHome(){
   cout<<"Grocery Home Constructor called\n\n";
  }
};

main(){
 GroceryHome g;
 
}
Read More

CS301 Assignment No 01 Solution Fall 2018

Leave a Comment

Binary Search tree is the most efficient data structure for solving different problems. Construction of BST (Binary Search Tree) depends on the order in which data is inserted into the tree. For building a Binary Search Tree, data should be inserted into a tree in a way that nodes with smaller data values appears on left side and larger node values appears on right side of the root node.
Write a menu based system program in C++ that will allow you to:
Enter Employee data in BST buildBST()
Post order traversal of all the Employee data postOrder()
Show data in ascending order asscendingOrder()


Take the following attributes of an Employee from the user:
empId, empNname, empSalary
You have to implement the above scenario using BST on the basis of Employee Id. i.e. if the Id of the Employee is lesser than the root node then enter the next Employee data in the left child otherwise in right child.
Note:
  • BST will implement using Linked List.
  • Program will not allow entering duplicate employee id.
  • Design the program in a way that the user will be able to enter maximum 10 records.
  • Take empId, empName, empSalary from the user. At least 4 students record should be already entered (hard coded records).
You will use following already entered Employees data (hard coded records).
Emp Id Name EmpSalary
32 Raza 3000
56 Sajjad 25000
93 Rabia 19230
5 Sehar 24000
10 Ali 22200

Solution Guidelines:
  • You will use buildBST() method to build Binary Search Tree from the above given data.
  • Use the ascendingOredr() method to show the output in ascending order. With respect to empId.
  • Use postOrder() method to traverse the records in post-order.

Sample output 1:

Solution:     Download.cpp
#include<iostream>
#include<conio.h>
#include<string>
#include<cstring>

using namespace std;

struct Node
{
    int empId;
    char* empName;
    double empSalary;

    Node* left;
    Node* right;
};
Node* buildBST(Node* root, int i,int id, char* name, double salary)
{

    if(root== NULL)
    {
        Node* newNode = new Node();
        newNode->empId = id;
        newNode->empName = name;
        newNode->empSalary = salary;
        newNode->left = newNode->right = NULL;
        return newNode;
    }
    if(root->empId == id)  // if the element already exists dont need to enter it again...
    {
        return root;
    }
    else if(id <= root->empId)
    {
        root->left = buildBST(root->left, i, id, name, salary);
    }
    else
    {
        root->right = buildBST(root->right, i, id, name, salary);
    }
}
void ascendingOrder(Node* root)
{
    if(root==NULL)
    {
        return;
    }
    ascendingOrder(root->left);
    cout<<endl<<root->empId<<"\t"<<root->empName<<"\t"<<root->empSalary<<endl;
    ascendingOrder(root->right);
}
void postOrder(Node* root)
{
    if(root==NULL)
    {
        return;
    }
    postOrder(root->left);
    postOrder(root->right);
    cout<<root->empId<<"\t"<<root->empName<<"\t"<<root->empSalary<<endl;
}

main()
{

    int id[]= {32,56,93,5,10};
    char* name[]= {"Raza","Sajid","Rabia","Sehar","Ali"};
    double salary[]= {3000, 25000, 19230, 24000, 22200};
    int c;
    int x = (sizeof(id)/sizeof(*id)); // getting the elements of the array, in that case '5'
    Node* root = NULL;
    char string[20]; //  for new entry a temporary array for getting name....


// loop for entering of the given table in the tree...
    for(int i=0; i<x; i++)
    {
        root = buildBST(root, i, id[i], name[i], salary[i]);
    }


// conditions...
s:
    cout<<endl<<"Press 1 to Enter Data \n";
    cout<<"Press 2 to see the Records in Ascending Order \n";
    cout<<"Press 3 to see the Records in Post Order \n";
    cout<<"Press 4 to Exit\n ";
    cin>>c;
    switch(c)
    {
    case 1:
    {
r:
        cout<<endl<<"Enter Employee ID : ";
        cin>>id[x];
        cout<<endl<<"Enter Employee Salary : ";
        cin>>salary[x];
        cout<<endl<<"Enter Employee Name : ";
        cin>>string;
// checking redundancy (duplication) for ID...
        for(int i=0; i<x; i++)
        {
            if(id[x] == id[i])
            {
                cout<<endl<<"Record Already Exists...";
                goto r;
            }
        }
// assigning name a dynamic memory....
        name[x] = new char[strlen(string)+1];
        strcpy(name[x], string);

// again update the tree...
        for(int i=0; i<(x+1); i++)
        {
            root = buildBST(root, i, id[i], name[i], salary[i]);
        }
        break;
    }
    case 2:
        cout<<"Ascending Order: \n";
        ascendingOrder(root);
        break;
    case 3:
        cout<<"Post Order: \n";
        postOrder(root);
        break;
    case 4:
        break;
    default:
        cout<<"\nWrong Input.";
        break;
    }
    char ch;
y:
    cout<<endl<<"DO YOU WANT TO DO SOMETHING ELSE...\nYES = y \t NO = n";
    ch = getch();
    if(ch=='y')
    {
        goto s;
    }
    else if(ch=='n')
    {
        cout<<"\nBye!";
    }
    else
    {
        cout<<endl<<"\nWrong input.";
        goto y;
    }
}

Read More

Sunday 18 November 2018

Time Class and Its Implementation in C++

Leave a Comment

Time.h

#ifndef TIME_H
#define TIME_H

class Time
{
    public:
        Time();
        Time(int,int,int);
        virtual ~Time();
        Time(const Time& other);
        int Gethour() { return hour; }
        void Sethour(int val) { hour = val; }
        int Getminutes() { return minutes; }
        void Setminutes(int val) { minutes = val; }
        int Getseconds() { return seconds; }
        void Setseconds(int val) { seconds = val; }
    protected:
    private:
        int hour;
        int minutes;
        int seconds;
};
#endif // TIME_H

Time.cpp

#include "Time.h"
Time::Time()
{
    this.Sethour(0);
    this.Setminutes(0);
    this.Setseconds(0);
}
Time::Time(int hour,int minut,int sec)
{
    this.Sethour(hour);
    this.Setminutes(minut);
    this.Setseconds(sec);
}
Time::~Time()
{
    //dtor
}
Time::Time(const Time& other)
{
    //copy ctor
}


main.cpp

#include <iostream>
#include "Time.h"
using namespace std;
int main()
{
    Time myTime=new Time();
   
    cout << "Hello world!" << endl;
    return 0;
}
Read More

Sunday 5 August 2018

DS Malik PROGRAMMING EXERCISES -- Chapter 3 -- Question 1 -- Solution

Leave a Comment
Question :


1. Consider the following incomplete C++ program:
#include <iostream>
int main()
{
...
}
a. Write a statement that includes the header files fstream, string, and
iomanip in this program.
b. Write statements that declare inFile to be an ifstream variable and
outFile to be an ofstream variable.
c. The program will read data from the file inData.txt and write output
to the file
outData.txt. Write statements to open both of these files,
associate
inFile with inData.txt, and associate outFile with
outData.txt.
d. Suppose that the file inData.txt contains the following data:
10.20 5.35
15.6
Randy Gill 31
18500 3.5
A
The numbers in the first line represent the length and width, respectively, of
a rectangle. The number in the second line represents the radius of a circle.
The third line contains the first name, last name, and the age of a person. The
first number in the fourth line is the savings account balance at the beginning
of the month, and the second number is the interest rate per year. (Assume
that p ¼3.1416.) The fifth line contains an uppercase letter between
A and
Y (inclusive). Write statements so that after the program executes, the con-
tents of the file
outData.txt are as shown below. If necessary, declare
additional variables. Your statements should be general enough so that if the
content of the input file changes and the program is run again (without
editing and recompiling), it outputs the appropriate results.
Rectangle:
Length = 10.20, width = 5.35, area = 54.57, parameter = 31.10
Circle:
Radius = 15.60, area = 764.54, circumfer
ence = 98.02
Name: Randy Gill, age: 31
Beginning balance = $18500.00, interest rate = 3.50
Balance at the end of the month =
$18553.96
The character that comes after A in the ASCII set is B
e. Write statements that close the input and output files.
f. Write a C++ program that tests the statements in parts a through e.

Explanation:
Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
       ifstream inFile;
       ofstream outFile;

       inFile.open("inData.txt");
       outFile.open("outData.txt");
       double length;                       //variable declarations are correct
       double width;
       double radius;
       string name;
       int age;
       double savings;
       double interest;
       string letter;

       inFile >> length >> width;
       outFile << length << width; //just add an endl after these so it look similar to the file given

       inFile >> radius;
       outFile << radius;

       inFile >> name >> age;
       outFile << name << age;   //The file reads "Randy Gill", note that ifstream
                                                  //files take whitespaces as the end of reading, which means you're reading
                                                  //Gill into age

       inFile >> savings >> interest;
       outFile << savings << interest;

       inFile >> letter;
       outFile << letter << endl;
       //simple enough, use cout statements to display
       //contents of your vars
       inFile.close();
       outFile.close();

       system("PAUSE");

       return 0;

}

Output:


Related Articles:


DS Malik Fifth Edition Complete Solution Manual 

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution



Note :: All credit and all rights belong to DS Malik

and their respective partners. I do not own this material, nor do i claim to do so. This material is only for educational purpose.

Read More

DS Malik Chapter 3 Question 18 Solution

Leave a Comment
Question :


Suppose that infile is an ifstream variable and it is associated with the file that contains the following data: 27306 savings 7503.35. Write the C++ statement(s) that reads and stores the first input in the int variable acctNumber, the second input in the string variable accountType, and the third input in the double variable balance.



Explanation:
Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int acctNumber;
    string accountType;
    double balance;

    ifstream infile("employee.dat");

    if(infile.is_open())
    {
        infile >> acctNumber;
        infile >> accountType;
        infile >> balance;

        infile.close();
    }

    cout << "acctNumber = " << acctNumber << endl
         << "accountType = " << accountType << endl
         << "balance = " << balance << endl
         << endl;

    return 0;
}

Output:


Related Articles:


DS Malik Fifth Edition Complete Solution Manual 

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution

Read More

Thursday 8 February 2018

Write a program that asks the user for an integer and then prints out all its factors in increasing order. Example input us 150, it should print 2 3 5 5

Leave a Comment
Question:

                Write a program that asks the user for an integer and then prints out all its factors in increasing                  order. Example input us 150, it should print 
                2
                3
                5
                5

Explanation:
Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include<iostream>
using namespace std;
void main() {
       int number=0;
       cin >> number;
       while (number > 1) {
              if (number % 2 == 0) {
                     number = number / 2;
                     cout << 2<<endl;
              }
              else if (number % 3 == 0) {
                     number = number / 3;
                     cout << 3 << endl;
              }

              else if (number % 4 == 0) {
                     number = number / 4;
                     cout << 4 << endl;
              }

              else if (number % 5 == 0) {
                     number = number / 5;
                     cout << 5 << endl;
              }
              else if (number % 6 == 0) {
                     number = number / 6;
                     cout << 6 << endl;
              }

              else if (number % 7 == 0) {
                     number = number / 7;
                     cout << 7 << endl;
              }
              else if (number % 8 == 0) {
                     number = number / 8;
                     cout << 8 << endl;
              }

              else if (number % 9 == 0) {
                     number = number / 9;
                     cout << 9 << endl;
              }
              else {
                     cout << number << endl;
                     number = number / number;
              }
       }

}


Output:
Write a program that asks the user for an integer and then prints out all its factors in increasing order. Example input us 150, it should print                   2                  3                  5                  5

Write a program that asks the user for an integer and then prints out all its factors in increasing order. Example input us 150, it should print                   2                  3                  5                  5




Related Articles:

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution

Read More

Sunday 10 December 2017

CS201 - Introduction to Programming -- Assignment No.2 - Fall 2017 - Solution

Leave a Comment
Objectives:
To enable students to write, compile and execute a program in DevC++. Moreover to familiarize students with  the concepts of:

  • If else structure
  • Loops
  • Passing array to a function
  • Multidimensional array declaration
  • Multidimensional array manipulation

Assignment:
Write a program that will generate random numbers and that numbers will be stored in a two-dimensional array. Then display the generated values on the screen and then also print the even numbers from the generated array elements.

Task 1:

First of all, you are required to generate random numbers from 1 to 500. For this, you are required to create a function named as “generateArray. The generateArray will load two- dimensional array with random numbers from 1to 500. The size of the two-dimensional array will be 7*7.

Task 2:

Defined a function named as “showArray(), which will display the elements of the two- dimensional array on the screen. See the sample output.

Task 3:
            The final task is to find the even numbers in this array and display it on the console. For this purpose create a function findEvenNumber().

CS201 - Introduction to Programming -- Assignment No.2 - Fall 2017 - Solution


Solution:

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

// functions definations
void generateArray(int inArr[7][7])
{
       for (int i = 0; i<7; i++)//rows
       {
              for (int j = 0; j<7; j++)//columns
              {
                     inArr[i][j] = rand() % 500 + 1; //Random Values Generation
              }
       }
}

void showArray(int inArr[7][7])
{
       for (int i = 0; i<7; i++)//rows
       {
              for (int j = 0; j<7; j++)//columns
              {
                     cout << inArr[i][j] << "\t"; // Output on Console
              }
              cout << "\n";
       }
}

void evenArray(int inArr[7][7])
{
       for (int i = 0; i<7; i++)
       {
              for (int j = 0; j<7; j++)
              {
                     if (inArr[i][j] % 2 == 0)
                     {
                           cout << inArr[i][j] << "\t"; // Output Even Values on console
                     }
              }
       }
}
// -- main body
main()
{
       int randarr[7][7];
       srand(time(NULL));// Null Time Randow Initialization
                                    // Array Generation
       generateArray(randarr);
       // Raw Array Output
       cout << "Array Element......\n\n";
       showArray(randarr);
       // Even Array output
       cout << "\n\nEven Array......\n\n";
       evenArray(randarr);
       // pause console screen
       cout << "\n\n";
       system("pause");
}


Read More