1. (a) Write C++ programs to find the followings:
(i) Area of Circle
(ii) Factorial of a given number
A.1.
(i)Area of Circle
#include <iostream>
void main()
{
float radius, area;
cout << "Enter the radius of circle : ";
cin >> radius;
area = 3.14 * radius * radius;
cout << "Area of circle with radius " << radius << " is " << area;
}
OUTPUT:--
Enter the radius of circle : 5
Area of circle with radius 5 is 78.5
(ii) Factorial of a given number
(b) Write a C++ program which create a Vehicle class and derive Car and
Bike classes from Vehicle class. All the classes in your program should
have proper constructors and methods to display vehicle details. Also
use appropriate access specifies in your program.
Ans (b)
#ifndef VEHICLE_H
#define VEHICLE_H
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
//Vehicle Class
class Vehicle {
protected:
Vehicle myVehicle[9];
string make; //make
string model; // model
string color; // color
int year; // year
int mileage; // miles on car
string type; //Type of vehicle
public:
//Constructor that will set information for a new car
void New_vehicle (string a, string b, string c, int d, int e)
{make = a; model = b; color = c; year = d; mileage = e;}
Vehicle(); //Default constructor
Vehicle(string, string, string, int, int, string);
//mutator and accessor functions
void setMake(string);
void setModel(string);
void setColor(string);
void setYear(int);
void setMileage(int);
void setType(string);
string getMake();
string getModel();
string getColor();
int getYear();
int getMileage();
string getType();
//Check mileage to see if valid
void valid_mileage(int);
//virtual function
virtual void details() {
}
};
//Sets to default values
Vehicle::Vehicle() {
make = " ";
model = " ";
color = " ";
year = 0;
mileage = 0;
type = " ";
}
Vehicle::Vehicle(string make, string model, string color, int year, int mileage, string type) {
Vehicle::make = make;
Vehicle::model = model;
Vehicle::color = color;
Vehicle::year = year;
valid_mileage(mileage);
Vehicle::type = type;
}
void Vehicle::setMake(string make) {
Vehicle::make = make;
}
void Vehicle::setModel(string model) {
Vehicle::model = model;
}
void Vehicle::setColor(string color) {
Vehicle::color = color;
}
void Vehicle::setYear(int year) {
Vehicle::year = year;
}
void Vehicle::setMileage(int mileage) {
valid_mileage(mileage);
}
void Vehicle::setType(string type) {
Vehicle::type = type;
}
string Vehicle::getMake() {
return make;
}
string Vehicle::getModel() {
return model;
}
string Vehicle::getColor() {
return color;
}
int Vehicle::getYear() {
return year;
}
int Vehicle::getMileage() {
return mileage;
}
string Vehicle::getType() {
return type;
}
void Vehicle::valid_mileage(int mileage) {
if (mileage>=0)
Vehicle::mileage=mileage;
else {
Vehicle::mileage=0;
cout << "WARNING! You have entered invalid mileage!\n";
}
Vehicle& getVehicle(int n) {
return myVehicle[n];
}
};
2. (a) Write a C++ program for matrix multiplication. Multiplication function
should notify if the order of the matrix is invalid, using exception.
Ans 2 (a)
contact details in it.
Ans (b)
#include <iostream>
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
char address[50];
int mob_no;
} s[10];
int main()
{
cout << "Enter information of students: " << endl;
// storing information
for(int i = 0; i < 10; ++i)
{
s[i].roll = i+1;
cout << "For roll number" << s[i].roll << "," << endl;
cout << "Enter name: ";
cin >> s[i].name;
cout << "Enter marks: ";
cin >> s[i].marks;
cout << "Enter address: ";
cin >> s[i].address;
cout << "Enter mobile number: ";
cin >> s[i].mob_no;
cout << endl;
}
cout << "Displaying Information: " << endl;
// Displaying information
for(int i = 0; i < 10; ++i)
{
cout << "\nRoll number: " << i+1 << endl;
cout << "Name: " << s[i].name << endl;
cout << "Marks: " << s[i].marks << endl;
cout << "Address: " << s[i].address<< endl;
cout << "Mobile Number : " << s[i].mob_no<< endl;
}
return 0;
}
(i) Area of Circle
(ii) Factorial of a given number
A.1.
(i)Area of Circle
#include <iostream>
void main()
{
float radius, area;
cout << "Enter the radius of circle : ";
cin >> radius;
area = 3.14 * radius * radius;
cout << "Area of circle with radius " << radius << " is " << area;
}
OUTPUT:--
Enter the radius of circle : 5
Area of circle with radius 5 is 78.5
(ii) Factorial of a given number
#include <iostream>
void main()
{
unsigned int n;
unsigned long long factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
for(int i = 1; i <=n; ++i)
{
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}
OUTPUT:--
Enter a positive integer: 12
Factorial of 12 = 479001600
(b) Write a C++ program which create a Vehicle class and derive Car and
Bike classes from Vehicle class. All the classes in your program should
have proper constructors and methods to display vehicle details. Also
use appropriate access specifies in your program.
Ans (b)
#ifndef VEHICLE_H
#define VEHICLE_H
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
//Vehicle Class
class Vehicle {
protected:
Vehicle myVehicle[9];
string make; //make
string model; // model
string color; // color
int year; // year
int mileage; // miles on car
string type; //Type of vehicle
public:
//Constructor that will set information for a new car
void New_vehicle (string a, string b, string c, int d, int e)
{make = a; model = b; color = c; year = d; mileage = e;}
Vehicle(); //Default constructor
Vehicle(string, string, string, int, int, string);
//mutator and accessor functions
void setMake(string);
void setModel(string);
void setColor(string);
void setYear(int);
void setMileage(int);
void setType(string);
string getMake();
string getModel();
string getColor();
int getYear();
int getMileage();
string getType();
//Check mileage to see if valid
void valid_mileage(int);
//virtual function
virtual void details() {
}
};
//Sets to default values
Vehicle::Vehicle() {
make = " ";
model = " ";
color = " ";
year = 0;
mileage = 0;
type = " ";
}
Vehicle::Vehicle(string make, string model, string color, int year, int mileage, string type) {
Vehicle::make = make;
Vehicle::model = model;
Vehicle::color = color;
Vehicle::year = year;
valid_mileage(mileage);
Vehicle::type = type;
}
void Vehicle::setMake(string make) {
Vehicle::make = make;
}
void Vehicle::setModel(string model) {
Vehicle::model = model;
}
void Vehicle::setColor(string color) {
Vehicle::color = color;
}
void Vehicle::setYear(int year) {
Vehicle::year = year;
}
void Vehicle::setMileage(int mileage) {
valid_mileage(mileage);
}
void Vehicle::setType(string type) {
Vehicle::type = type;
}
string Vehicle::getMake() {
return make;
}
string Vehicle::getModel() {
return model;
}
string Vehicle::getColor() {
return color;
}
int Vehicle::getYear() {
return year;
}
int Vehicle::getMileage() {
return mileage;
}
string Vehicle::getType() {
return type;
}
void Vehicle::valid_mileage(int mileage) {
if (mileage>=0)
Vehicle::mileage=mileage;
else {
Vehicle::mileage=0;
cout << "WARNING! You have entered invalid mileage!\n";
}
Vehicle& getVehicle(int n) {
return myVehicle[n];
}
};
2. (a) Write a C++ program for matrix multiplication. Multiplication function
should notify if the order of the matrix is invalid, using exception.
Ans 2 (a)
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<exception.h>
#include<conio.h>
#include<process.h>
#include<exception.h>
void matsum()
{
int m, n, i, j, first[10][10], second[10][10], sum[10][10];
try { cout << “Enter the number of rows and columns of matrix “;
cin >> m >> n;
cout << “Enter the elements of first matrix\n”;
if (m>10 || n>10) //exit(0);
throw 1;
}
catch(int)
{ cout<<“subscript invalid”;
}
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
cin >> first[i][j];
cout << “Enter the elements of second matrix\n”;
for ( i = 0 ; i < m ;i++ )
for ( j = 0 ; j < n ; j++ )
cin >> second[i][j];
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
sum[i][j] = first[i][j] + second[i][j];
cout << “Sum of entered matrices:-\n”;
for ( i = 0 ; i < m ; i++ )
{
for ( j = 0 ; j < n ; j++ )
cout << sum[i][j] << “\t”;
cout << endl;
}
}
{
int m, n, i, j, first[10][10], second[10][10], sum[10][10];
try { cout << “Enter the number of rows and columns of matrix “;
cin >> m >> n;
cout << “Enter the elements of first matrix\n”;
if (m>10 || n>10) //exit(0);
throw 1;
}
catch(int)
{ cout<<“subscript invalid”;
}
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
cin >> first[i][j];
cout << “Enter the elements of second matrix\n”;
for ( i = 0 ; i < m ;i++ )
for ( j = 0 ; j < n ; j++ )
cin >> second[i][j];
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
sum[i][j] = first[i][j] + second[i][j];
cout << “Sum of entered matrices:-\n”;
for ( i = 0 ; i < m ; i++ )
{
for ( j = 0 ; j < n ; j++ )
cout << sum[i][j] << “\t”;
cout << endl;
}
}
void main()
{
matsum();
getch();
{
matsum();
getch();
}
(b) Write C++ program to create a file and store students address andcontact details in it.
Ans (b)
#include <iostream>
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
char address[50];
int mob_no;
} s[10];
int main()
{
cout << "Enter information of students: " << endl;
// storing information
for(int i = 0; i < 10; ++i)
{
s[i].roll = i+1;
cout << "For roll number" << s[i].roll << "," << endl;
cout << "Enter name: ";
cin >> s[i].name;
cout << "Enter marks: ";
cin >> s[i].marks;
cout << "Enter address: ";
cin >> s[i].address;
cout << "Enter mobile number: ";
cin >> s[i].mob_no;
cout << endl;
}
cout << "Displaying Information: " << endl;
// Displaying information
for(int i = 0; i < 10; ++i)
{
cout << "\nRoll number: " << i+1 << endl;
cout << "Name: " << s[i].name << endl;
cout << "Marks: " << s[i].marks << endl;
cout << "Address: " << s[i].address<< endl;
cout << "Mobile Number : " << s[i].mob_no<< endl;
}
return 0;
}
Output
Enter information of students:
For roll number1,
Enter name: Tom
Enter marks: 98
Enter Address : Sigra
Enter Mobile Number : 8576463866
For roll number2,
Enter name: Jerry
Enter marks: 89
Enter Address : BHU
Enter Mobile Number : 8576463887
Displaying Information:
Roll number: 1
Name: Tom
Marks: 98
Address : Sigra
Mobile No : 8576463866
No comments:
Post a Comment