Archive

Posts Tagged ‘Cpp’

C++ Program to implement Toll Tax Problem by using CLASS Access Specifiers and SWITCH CASE – Q12

January 12, 2010 Leave a comment

Q12. Program to implement Toll Tax Problem by using CLASS Access Specifiers and SWITCH CASE:

Calculate the toll tax for cars passing by a toll bridge @ Rs 5 per car (use the concept static data members). Calculate and Print the following details:
– Total no of cars passed with paying the tax
– Total amount of tax paid
– Total no of cars passed without paying tax.

… from College notes (BCA/MCA assignments):

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

class TollBridge{
	private:
		static int car_yes, car_no;
		static int amt;
	public:
		static void GetCar();
		static void PutCar();
	};

int TollBridge::car_yes = 0;
int TollBridge::car_no = 0;
int TollBridge::amt = 0;

void TollBridge :: GetCar(){
	char ch;
	cout<<"\n Total Cars Passed: "<<car_yes+car_no;
	cout<<"\n This Car:- ";
	cout<<"\n Paid Tax (y/n) ?: ";
	cin>>ch;
	if(ch == 'y'){
		car_yes++;
		amt += 5;
		}
	else
		car_no++;
	}

void TollBridge :: PutCar(){
	cout<<"\n Total Car Passed: "<<car_yes+car_no<<endl;
	cout<<"\n\t Car Passed by giving Tax: "<<car_yes;
	cout<<"\n\t\t Amount Recieved: "<<amt<<endl;
	cout<<"\n Car Passed by not giving Tax: "<<car_no;
	}

void main(){
	int ch;
	char choice;
	while(1){
		clrscr();
		cout<<"\n TOLL TAX PLAZA";
		cout<<"\n ~~~~~~~~~~~~~~";
		cout<<"\n 1 -> Entry of CARS.";
		cout<<"\n 2 -> Report of CARS.";
		cout<<"\n 3 -> Exit.";
		cout<<"\n Enter your choice: ";
		cin>>ch;
		switch(ch){
			case 1:
				while(1){
					TollBridge::GetCar();
					cout<<"\n Do you want to continue 
  (y/n) ?: ";
					cin>>choice;
					if(choice =='n') break;
					}
				break;
			case 2:
				TollBridge::PutCar();
				getch();
				break;
			default:
				exit(1);
			} // end of switch.
		} // end of while.
	} // end of main.

 

Output:

TOLL TAX PLAZA
1 -> Entry of CARS.
2 -> Report of CARS.
3 -> Exit.
Enter your choice: 1

Total Cars Passed: 0
This Car:-
Paid Tax (y/n) ?: y

Do you want to continue (y/n) ?: y

Total Cars Passed: 1
This Car:-
Paid Tax (y/n) ?: y

Do you want to continue (y/n) ?: y

Total Cars Passed: 2
This Car:-
Paid Tax (y/n) ?: y

Do you want to continue (y/n) ?: y

Total Cars Passed: 3
This Car:-
Paid Tax (y/n) ?: n

Do you want to continue (y/n) ?: y

Total Cars Passed: 4
This Car:-
Paid Tax (y/n) ?: n

Do you want to continue (y/n) ?: y

Do you want to continue (y/n) ?: n

TOLL TAX PLAZA
1 -> Entry of CARS.
2 -> Report of CARS.
3 -> Exit.
Enter your choice: 2

Total Car Passed: 5
Car Passed by giving Tax: 3
Amount Recieved: 15
Car Passed by not giving Tax: 2


C++ Program for CLASS Array Addition & Swapping by using Friend function – Q11

January 11, 2010 Leave a comment

Q11. Program for CLASS Array Addition & Swapping by using Friend function:

Use the concept of Friend function for swapping the private array data members of two classes and getting the sum of two arrays within third class array member.

… from College notes (BCA/MCA assignments):

#include <iostream.h>
#include <conio.h>

class B;
class A{
	private:
		int arr[10];
		int sum;
	public:
		void getdata();
		friend void Swap(A &, B &);
		friend void Sum(A *, B *);
		void display() const;
	};

class B{
	private:
		int arr[10];
		int sum;
	public:
		void getdata();
		friend void Swap(A &, B &);
		friend void Sum(A *, B *);
		void display() const;
	};

void A :: getdata(){
	cout<<"\n Enter 10 Numbers in Array A:- \n";
	for(int i=0; i<10; i++)
		cin>>arr[i];
	}

void B :: getdata(){
	cout<<"\n Enter 10 Numbers in Array B:- \n";
	for(int i=0; i<10; i++)
		cin>>arr[i];
	}

void Swap(A &ob1, B &ob2){
	int temp;
	for(int i=0; i<10; i++){
		temp = ob1.arr[i];
		ob1.arr[i] = ob2.arr[i];
		ob2.arr[i] = temp;
		}
	}

void Sum(A *ob1, B *ob2){
	int sum = 0;
	for(int i=0; i<10; i++)
		sum += ob1->arr[i];
	ob1->sum = sum;
	cout<<"\n Sum of Array A : "<<sum;

	sum = 0;
	for(i=0; i<10; i++)
		sum += ob2->arr[i];
	ob2->sum = sum;
	cout<<"\n Sum of Array B : "<<sum;
	}

void A :: display() const{
	cout<<"\n Elements of Array A are: ";
	for(int i=0; i<10; i++)
		cout<<" "<<arr[i];
	}

void B :: display() const{
	cout<<"\n Elements of Array B are: ";
	for(int i=0; i<10; i++)
		cout<<" "<<arr[i];
	}

void main(){
	A obj1;
	B obj2;
	clrscr();

	cout<<"\n Friend Function programme:-"<<endl;
	obj1.getdata();
	obj2.getdata();

	cout<<"\n Data Before Swapping:-"<<endl;
	obj1.display();
	obj2.display();

	cout<<"\n Data After Swapping:-"<<endl;
	Swap(obj1, obj2);
	obj1.display();
	obj2.display();

	cout<<"\n Addition of Both Class's Arrays:-"<<endl;
	Sum(&obj1, &obj2);
	getch();
	}

 

Output:
Friend Function program:-

Enter 10 Numbers in Array A:-
22 33 4 2 56 64 78 45 3 90

Enter 10 Numbers in Array B:-
67 543 5 67 89 90 12 0 12 70

Data Before Swapping:-

Elements of Array A are: 22 33 4 2 56 64 78 45 3 90
Elements of Array B are: 67 543 5 67 89 90 12 0 12 70

Data After Swapping:-

Elements of Array A are: 67 543 5 67 89 90 12 0 12 70
Elements of Array B are: 22 33 4 2 56 64 78 45 3 90

Addition of Both Class’s Arrays:-

Sum of Array A : 955
Sum of Array B : 397


Categories: Cpp Tags: ,

C++ Program to work with Purchase Order Problem by using Pointers and CLASS with PUBLIC & PRIVATE Access Specifiers – Q10

January 10, 2010 2 comments

Q10. Program to work with Purchase Order Problem:

Solve the following purchase order problem:
– Item list entry
– Bill generation
– Modify Item quantity purchased
– Remove Item from the list

… from College notes (BCA/MCA assignments):

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

class CBills{
	private:
		char name[20];
		int qty;
		float price, net;
	public:
		void Getinfo();
		void Modify(CBills *, int );
		void Remove(CBills *, int &);
		void Display() const;
		};

void CBills :: Getinfo(){
	cout<<"\n Enter Item Name: ";
	cin>>name;
	cout<<"\n Enter Item Price: ";
	cin>>price;
	cout<<"\n Enter Item Quantity: ";
	cin>>qty;
	net = price * qty;
	}

void CBills :: Modify(CBills *order, int no){
	char key[15];
	cout<<"\n Enter the Item Name to Modify: ";
	cin>>key;
	for(int i=0; i<=no; i++){
		if(strcmp(order[i].name, key) == 0)
			break;
		}
	cout<<"\n Enter Item Name: ";
	cin>>order[i].name;
	cout<<"\n Enter Item Price: ";
	cin>>order[i].price;
	cout<<"\n Enter Item Quantity: ";
	cin>>order[i].qty;
	order[i].net = order[i].price * order[i].qty;
	}

void CBills :: Remove(CBills *order, int &no){
	char key[15];
	cout<<"\n Enter the Item Name to Remove: ";
	cin>>key;
	for(int i=0; i<=no; i++){
		if(strcmp(order[i].name, key) == 0)
			break;
		}
	for(int j=i; j<=no; j++)
		order[j] = order[j+1];
	no--;
	}

void CBills :: Display() const{
	cout<<"\n\t Item Name:  "<<name;
	cout<<"\n\t Item Price: "<<price;
	cout<<"\n\t Item Quantity: "<<qty;
	cout<<"\n\t Net Price: "<<net;
	}

void main(){
	CBills bills[20], bl;
	int ch1, item_no = -1;
	char ch2;
	while(1){
		clrscr();
		cout<<"\n Inventory and Purchase Order Managament.";
		cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
cout<<"\n 1 -> Get Item Details.";
		cout<<"\n 2 -> Display Items.";
		cout<<"\n 3 -> Modify an Item.";
		cout<<"\n 4 -> Remove an Item.";
		cout<<"\n 5 -> Exit.";
		cout<<"\n Enter your choice: ";
		cin>>ch1;
		switch(ch1){
			case 1: // Getinfo.
				while(1){
					clrscr();
					item_no++;
					cout<<"\n Entry of Purchased Items.";
					cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~";
					cout<<"\n Item No. "<<item_no+1;
					bills[item_no].Getinfo();
					cout<<"\n Do you want to add more 
  (y/n): ";
					cin>>ch2;
					if(ch2 == 'n')
						break;
					}
				break;
			case 2: // Display.
				clrscr();
				cout<<"\n Purchase Order Details.";
				cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~";						for(int i=0; i<=item_no; i++){
					cout<<"\n\n Item No.: "<<i+1;
					bills[i].Display();
					}
				break;
			case 3: // Modify.
				bl.Modify(bills, item_no);
				cout<<"\n\n Record Modified.";
				break;
			case 4: // Remove.
				bl.Remove(bills, item_no);
				cout<<"\n\n Record Removed.";
				break;
			default:
				exit(1);
			} // end of switch.
		getch();
		} // end of while.
	} // end of main.

 

Output:

Inventory and Purchase Order Managament.
1 -> Get Item Details.
2 -> Display Items.
3 -> Modify an Item.
4 -> Remove an Item.
5 -> Exit.
Enter your choice: 1

Entry of Purchased Items.

Item No. 1
Enter Item Name: Magazine

Enter Item Price: 100

Enter Item Quantity: 5

Do you want to add more (y/n): y

Entry of Purchased Items.

Item No. 2
Enter Item Name: Register

Enter Item Price: 50

Enter Item Quantity: 10

Do you want to add more (y/n): y

Entry of Purchased Items.

Item No. 3
Enter Item Name: Pen

Enter Item Price: 10

Enter Item Quantity: 20

Do you want to add more (y/n): n

Purchase Order Details.
Item No.	Name		Price		Qty		Net Price
    1		Magazine	100		5		500
    2		Register	50		10		500
    3		Pen		10		20		200

Categories: Cpp Tags: , ,

C++ Program to maintain a Cricket Team by using CLASS with PUBLIC & PRIVATE Access Specifiers – Q9

January 9, 2010 Leave a comment

Q9. Program to maintain a Cricket Team:

Make a Class Cricket_Team with following attributes:
– Team Name
– Player Name
– Batting Average
Read the details various players from different team in random.
Now print the team wise list of players in the sorted order of their batting averages.

Use functions:
– To read the player detail
– To arrange the players team wise
– To sort the batting average
– To Print the details

… from College notes (BCA/MCA assignments):

#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <conio.h>

class CCricket{
	private:
		char country[20], player[20];
		int avg;
	public:
		void getTeams();
		void compTeams(CCricket *, const int);
		void dispTeams(const CCricket *, const int);
	};

void CCricket :: getTeams(){
	cout<<"\n Enter the Name of a Country: ";
	cin>>country;
	cout<<"\n Enter a Player Name: ";
	cin>>player;
	cout<<"\n Enter the Batting Average: ";
	cin>>avg;
	}

void CCricket :: compTeams(CCricket *Ock, const int t_pls){
	int i, j;
	CCricket Otemp;
	// Sorting By Players Name.
	for(i=0; i<=t_pls; i++){
		for(j=i+1; j<=t_pls; j++){
			if(Ock[i].avg < Ock[j].avg){
				Otemp = Ock[i];
				Ock[i] = Ock[j];
				Ock[j] = Otemp;
				}
			}
		}
	// Sorting By Country Name.
	for(i=0; i<=t_pls; i++){
		for(j=i+1; j<=t_pls; j++){
			if(strcmp(Ock[i].country, Ock[j].country) > 0){
				Otemp = Ock[i];
				Ock[i] = Ock[j];
				Ock[j] = Otemp;
				}
			}
		}
	}

void CCricket :: dispTeams(const CCricket *Ock, const int t_pls){
	int i, j;
	char t_c_name[10];
	// Display Players.
	cout<<"\n\n Players Sorted According to their Country and 
  Average:- \n";
	cout<<"\n COUNTRY \t TEAM \t AVERAGE"<<endl;
	for(i=1; i<=t_pls; i++){
		if(strcmp(t_c_name, Ock[i].country) != 0){
			cout<<"\n "<<Ock[i].country;
			strcpy(t_c_name, Ock[i].country);
			}
		cout<<"\n\t\t"<<Ock[i].player<<"  -  "
    <<setw(5)<<Ock[i].avg<<endl;
		}
	}

void main(){
	int i=0;
	char ch;
	CCricket Ock[30], Otemp;

	while(1){
		clrscr();
		Ock[i].getTeams();
		i++;
		cout<<"\n Do you want to Enter next Entry (y/n) ? : ";
		cin>>ch;
		if(ch == 'n')
			break;
		} // End of while Loop.

	cout<<"\n\n Total Players Entered: "<<i<<endl;

	// Sort Teams.
	Otemp.compTeams(Ock, i);

	// Display Teams.
	Otemp.dispTeams(Ock, i);

	getch();
	}

 

Output:

Enter the Name of a Country: India

Enter a Player Name: Sachin

Enter the Batting Average: 70

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: Australia

Enter a Player Name: Ponting

Enter the Batting Average: 50

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: Bang

Enter a Player Name: Zahid

Enter the Batting Average: 30

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: India

Enter a Player Name: Rahul

Enter the Batting Average: 45

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: Australia

Enter a Player Name: Gilcrist

Enter the Batting Average: 75

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: India

Enter a Player Name: Harbhjan

Enter the Batting Average: 50

Do you want to Enter next Entry (y/n) ? : n

Total Players Entered: 6

Players Sorted According to their Country and Average:-

COUNTRY TEAM

Australia
Gilcrist – 75

Ponting – 50

Bang
Zahid – 30

India
Harbhjan – 50

Rahul – 45

Sachin – 70


Categories: Cpp Tags: ,

C++ Program to implement Function Overloading – Q8

January 8, 2010 Leave a comment

Q8. Program to implement Function Overloading:

Implement Function Overloading for following requirements
– Getting Sum of two number
– Concatenating two strings
– Adding two matrices

… from College notes (BCA/MCA assignments):

#include <iostream.h>
#include <conio.h>

const int MAX = 3;

int   Sum (int, int);
void  Sum (char *, char *);
void  Sum (int [][MAX], int [][MAX]);

void main(){
	int a, b;
	char x[10], y[10];
	int mat1[MAX][MAX], mat2[MAX][MAX], tot[MAX][MAX];
	clrscr();

	cout<<"\n Integers:- "<<endl;
	cout<<"\n Enter value of a: ";
	cin>>a;

	cout<<"\n Enter value of b: ";
	cin>>b;

	cout<<"\n Sum of Integers "<<a<<" & "<<b<<" is: "<<Sum(a, b)<<endl;

	cout<<"\n Strings:- "<<endl;
	cout<<"\n Enter value of x: ";
	cin>>x;

	cout<<"\n Enter value of y: ";
	cin>>y;

	cout<<"\n Sum of Strings "<<x<<" & "<<y<<" is: ";
	Sum(x, y);

	cout<<"\n Integer Matrices:- "<<endl;
	cout<<"\n Enter values in Matrix mat1: ";
	for(int i=0; i<MAX; i++)
		for(int j=0; j<MAX; j++)
			cin>>mat1[i][j];

	cout<<"\n Enter value in Matrix mat2: ";
	for(i=0; i<MAX; i++)
		for(j=0; j<MAX; j++)
			cin>>mat2[i][j];

	cout<<"\n Sum of Matrices is:-"<<endl;
	Sum(mat1, mat2);
	getch();
	}

int Sum(int a, int b){
	return (a+b);
	}

void Sum(char *a, char *b){
	int i = 0, j = 0;
	char c[20];
	while(*(a+i) != '\0'){
		*(c+i) = *(a+i);
		i++;
		}
	while(*(b+j) != '\0'){
		*(c+i) = *(b+j);
		i++;
		j++;
		}
	*(c+i) = '\0';
	cout<<c;
	}

void Sum(int m1[MAX][MAX], int m2[MAX][MAX]){
	int tot[MAX][MAX];
	for(int i=0; i<MAX; i++){
		for(int j=0; j<MAX; j++){
			tot[i][j] = m1[i][j] + m2[i][j];
			cout<<" "<<tot[i][j];
			}
		cout<<endl;
		}
}

 

Output:

Integers:-

Enter value of a: 45

Enter value of b: 12

Sum of Integers 45 & 12 is: 57

Strings:-

Enter value of x: Manoj

Enter value of y: Pandey

Sum of Strings Manoj & Pandey is: ManojPandey

Integer Matrices:-

Enter values in Matrix mat1: 1 2 3 4 5 6 7 8 9

Enter values in Matrix mat2: 9 8 7 6 5 4 3 2 1

Sum of Matrices is :-

10 10 10
10 10 10
10 10 10


Categories: Cpp Tags: ,