CS201 (P) ProblemStatement
CS201 (P) ProblemStatement
Topics Covered:
Problem Statement:
You need to write a program in which the following concepts must be implemented.
Write a class Matrix and overload multiplication (*) operator for this class to multiply a matrix
by a scalar (2) and display the original and resultant matrix.
Write following functions in the class.
1. read(): To get input for number of rows and columns of matrix.
2.input(): To get elements of matrix from user.
3.display(): To display the elements of matrix.
Solution:
#include<iostream>
class Matrix
private:
int x, y;
public:
Matrix();//default constructor
void read();
void input();
void display();
{ x = 0;
y = 0;
{ x = a;
y = b;
cin >> y;
int i, j;
A[num1][num2] = 0;
int i, j;
int main ()
M1.display ();//Displaying M1
M2.display();//Displaying M2
return 0;
Lab # 14
Topics Covered:
Template Function
Nested Class
Problem Statement:
You need to write a program in which the following concepts must be implemented.
Write a template function name “Add” which accepts two arguments of the same type int, float,
double from the user then add those arguments, return their sum and display them on the screen.
Write a class named as firstClass, also write another class named as secondClass which should
be nested inside the firstClass. Define a function named displayMessage inside the secondClass,
this method should print the message “Inside the second class” on the screen. In the main
function create the object of the secondClass and invoke the displayMessage method using the
secondClass object.
Solution:
#include <iostream>
public:
return x + y;
int main() {
float floatOne = 12.34, floatTwo = 894.4; //float variables as argument to template function
Add()
double doubleOne = 1236.58, doubleTwo = 8945.685; //double variables as argument to
template function Add()
cout<< "Addition of Two floating point Number is = " << Add(floatOne,floatTwo) <<endl;
cout<< "Addition of Two double Number is = " << Add(doubleOne , doubleTwo) <<endl;
return 0;
Lab # 13
Week = 24 June – 28th June-2024
th
Topics Covered
Write a program in C++ to create a class name “String”. The String class should have a single
data member as a character array and a default constructor to initialize the character array with
default values.
The String class should overload the stream extraction >> and insertion operator <<.
The String class should also overload the “+” operator to concatenate the String.
Inside, the main function, create two objects of the String class. Take input from the user in those
objects as First Name and Last Name and output the Full Name by merging the two inputs using
the “+” operator. When taking the first and second input, the program should store the input in a
“Sample.txt” file and similarly store the output in the same file.
Sample Output:
Ali
Ahmed
AliAhmed
Solution:
#include <iostream> //header file containing info (like function prototypes etc.) about cout and
cin objects.
#include <stdlib.h> //a general purpose standard library in C.
#include <cstring> //header file containing info about string manipulation functions.
#include <fstream> //header file containing info about functions relating creating, reading and
writing files.
using namespace std;
ofstream outfile; //Since outfile is global, it can be used in all functions in this program.
class String {
private:
char txt [20]; //an array of 20 characters.
public:
String () { //a default constructor.
strcpy(txt,""); // In strcpy () is function, the data member txt is initialized to an
empty string.
}
friend istream &operator >> (istream &input, String &s); // insertion and
extraction operators are overloaded.
friend ostream &operator << (ostream &output, const String &s); //Since these are not
member functions of this class, so declared as friend to this class.
Lab # 12
Week = 17 June – 21st June-2024
th
Topics Covered:
Array of objects
Write a program in C++ to create two classes named “Rectangle” and “String” with the
following information:
For Rectangle class, create two data members length, breadth of type double, a default
constructor and a parameterized constructor. When a default constructor gets called, it should
print “Default constructor of Rectangle class” and similarly when parameterized constructor is
called, it should print “Parametrized constructor of Rectangle class”. Also, the body of the
parametrized constructor should initialize the data members with the passed values.
For String class, create a character pointer text and a default constructor. When a default
constructor gets called it should print “Default constructor of String class”. Also create a
destructor for the string class and it should print “Destructor of String class”.
In the main function, create an array of five objects for class “Rectangle” and initialize first two
array objects with random values. Similarly, in the main function dynamically create an array of
five objects for class “String” using new operator. In the end, use the delete operator to
deallocate the dynamically created objects for the String class.
Solution:
#include<iostream>
using namespace std;
class Rectangle {// declaring and defining class Rectangle
private:
double length; // Private members
double breadth;
public:
Rectangle(){ // default constructor of class Rectangle
cout << "Default Constructor of Rectangle class is called" << endl;
}
Rectangle(double l, double b){ //parameterize constructor of class Rectangle
length = l;
breadth = b;
cout << "Parameterized Constructor of Rectangle class is called" << endl;
}
};
class String { //declaring and defining class String
private:
char * text; //character pointer text
public:
String(){//constructor of class String
cout << "Default Constructor of String class is called" << endl;
}
~String(){//destructor of class String
cout << "Destructor of String class is called" << endl;
}
};
int main(){
// creating array of objects of Rectangle class
Rectangle rectangle[5] = { Rectangle(4.6,2.3) , Rectangle(7.6,4.3) };
// dynamically creating an array of String class
String * str;
str = new String [5];
// delete operator to deallocate memory
delete []str;
return 0;
}
Lab # 11
Topics Covered:
Operator Overloading
Problem Statement:
Write a program in C++ that add two class objects by overloading “plus (+)” operator. You are
required to create a class named “MathClass“ and declare the class member and member
functions. Also declare a class data member named as “number” and a parameterized constructor
which take one argument and initializes the number. Define the ‘+’ operator overloaded function
to add two object’s numbers. Also define another member function named as “Display ()” that
shows the calculation result. Create three class objects for example: obj1, obj2 and result. Values
are passed by calling the parameterized constructor in main () function. Add two object values by
calling the ‘+’ operator overloaded function and then call display () function using obj1, obj2 and
result.
Solution:
#include <iostream>
private:
public:
number = x ;
MathClass temp;
temp.number= number+m.number;
return temp;
};
int main()
{
MathClass first(4), second(2), result; //value passing
result.Display();
system("pause");
Lab # 10
Topics Covered:
Classes
Constructors and their types
Problem Statement:
Write a C++ program which implement a class named “Employee”. This class has the following
data members:
Solution:
private:
char name[30];
char gender[10];
double id;
int age;
public:
void setGender(char[]);
void setAge(int);
void setId(double);
//getter functions of the class
char* getName();
char* getGender();
int getAge();
double getId();
};
strcpy(name, "Empty");
strcpy(gender, "Empty");
age = 0;
id = 0.0;
strcpy(name, Name);
strcpy(gender, Gender);
age = Age;
id = Id;
}
void Employee::setName(char Name[]) // Setter function to set name
age = Age;
id = Id;
{
return name;
}
{
return age;
return id;
int main () {
cout<< "The name of the Employee is " << e1.getName() <<endl; // displaying Name of
employee
cout<< "The gender of the Employee is " << e1.getGender() <<endl; // displaying gender of
employee
cout<< "The age of the Employee is " << e1.getAge() <<endl; // displaying age of employee
cout<< "The Id of the Employee is " << e1.getId() <<endl; // displaying id of employee
system("pause");
return 0;
Lab # 9
Topics Covered:
Function Overloading
Problem Statement:
Write a program that overloads function named “myfunc” for integer, double and character data
types. For example, if an integer value is passed to this function, the message "using integer
myfunc" should be printed on screen, on passing a double type value, the message "using
double myfunc" and on passing character value, the message "using character myfunc"
should get printed.
Solution:
#include <iostream> //Including iostream header files
int myfunc (int x); // function for integer data type parameter
double myfunc (double y); // function for double data type parameter
main()
system("pause");
return x;
return y;
}
char myfunc(char z) //function definition for character type
return z;
}
Lab # 8
Topics Covered:
AND, OR operators
Bitwise manipulation
Problem Statement:
Write a program which declare two variables of integer type and take their values as input from
user. Also, perform the following operations on them and print their values.
1. Logical AND
2. Bitwise AND
3. Logical OR
4. Bitwise OR
Solution:
int main(){
cout<< "Enter First Value: "; //Prompt to get first variable value
cout<< "Enter Second Value: "; //Prompt to get second variable value
var3 = var1 & var2; // Performing Bitwise AND using & operators
}
Lab # 7
Topics Covered:
Structures
- Declaration of a Structure
- Initializing Structures
- Functions and structures
Problem Statement:
Solution:
struct MyStruct{
int i;
float f;
ms4 = {0,0.0};
main(){
cin>> ms1.i;
cin>> ms2.i;
cin>> ms2.f;
cout << ms3.i << "\t" << ms3.f <<endl; //Display the values of resultant structure
system("pause");
Lab # 6
Topics Covered:
Multi-dimensional Arrays
Problem Statement:
Write a program in which you need to declare a multidimensional integer type array of size 4*4. In this
program:
You should take input values from the users and store it in 4*4 matrix.
Display this matrix on the screen.
Also, display the transpose of this matrix by converting rows into cols.
Solution:
#include <iostream>
void readMatrix(int arr[][arraySize]); // prototype of a function that takes input values from the user
and will store it into the array.
void transposeMatrix(int a[][arraySize]); // protype of a function that will change rows into columns and
columns into rows.
main(){
int a[arraySize][arraySize];
cout << "\n\n" << "The original matrix is: " << '\n';
displayMatrix(a); // Calling a function that is written to display array values from a multi-
dimensional array.
cout << "\n\n" << "The transposed matrix is: " << '\n';
displayMatrix(a); // Calling a function that is written to display array values from a multi-
dimensional array. This time it will show swapped values.
// Defining a readMatrix() function, in which we are using nested for loop to build a multi-dimensional
matrix.
cout << "\n" << "Enter " << row << ", " << col << " element: ";
// Defining displayMatrix() function, that will traverse through the whole array and will display array
elements.
// A function that is swapping whole rows into columns and columns into arrays.
int temp;
a[row][col] = a[col][row];
}//end of lab=6
Lab # 5
Topics Covered:
Array Manipulation
Pointers
Problem Statement:
1. Write a program that will take two strings as input from the user in the form of
character arrays namely as “firstArray” and “secondArray” respectively.
2. Both arrays along with the size will be passed to the function compareString.
3. compareString function will use pointer to receive arrays in function and then
start comparing both arrays using while loop.
4. If all the characters of both these arrays are same then the message “Both strings
are same” should be displayed on the screen.
Note: For comparing both these arrays, the size should be same.
//function definition
//Initializing variables
int flag = 1;
int i = 0;
while(i<array_size){
else
cout<< "Both strings are not same!" <<endl;
int main() {
char firstArray[20];
char secondArray[20];
else {
}
Lab # 4
Topics Covered:
Arrays
Functions
Problem Statement:
Write a program in which you have to declare an integer array of size 10 and initializes it with
numbers of your choice. Find the maximum and minimum number from the array and output the
numbers on the screen.
For finding the maximum and minimum numbers from the array you need to declare two
functions findMax and findMin which accept an array and size of array (an int variable) as
arguments and find the max min numbers, and return those values.
Solution:
#include <iostream>
//functions declaration
int main() {
//Array initialization
int number[10] = {
21,25,89,83,67,81,52,100,147,10
};
return 0;
int min = 0;
min = array[0];//Storing the value of the first element of array in 'min' variable
if(min > array[i])//Testing if the value of 'min' variable is greater than the current
element of array
int max = 0;
max = array[0];//Storing the value of the first element of array in 'max' variable
if(max < array[i]) //Testing if the value of 'max' variable is less than the current
element of array
Lab # 3
Topics Covered:
Functions
Repetition Structure (Loop)
Problem Statement:
Write a program in which you have to define a function displayDiagnol which will have two
integer arguments named rows and cols. In the main function, take the values of rows and
columns from the users. If the number of rows is same as numbers of columns then call the
displayDiagnol function else show a message on screen that number of rows and columns is not
same.
The function will take the value of rows and cols which are passed as argument and print the
output in matrix form. To print the values in the matrix form, nested loops should be used. For
each loop, you have to use a counter variable as counter. When the value of counters for each
loop equals, then it prints the value of row at that location and prints hard coded zero at any other
location.
Example if the user enters rows and cols as 3, then the output should be like this
100
020
003
Solution:
#include <iostream>
int main(){
rows = 0;
columns = 0;
cin>> rows;
cin>> columns;
else
cout<< "Wrong input! Num of rows should be equal to num of columns";
return 0;
// function definition
if(i==j)//diagonal check
else
cout<< "\n";
}
Lab # 2
Topics Covered:
“Calculate the average age of a class of ten students using while loop. Prompt the user to enter
the age of each student.”
⮚ We need 10 values of variable age of int type to calculate the average age.
int age;
⮚ “Prompt the user to enter the age of each student” this requires cin>> statement.
For example:
cin>> age;
⮚ Average can be calculated by doing addition of 10 values and dividing sum with 10.
Solution:
#include<iostream>
using namespace std;
main() {
cin>>age;
}
Lab # 1
Topics Covered:
● Variables
● Data Types
● Arithmetic Operators
● Precedence of Operators
Problem Statement:
“Calculate the average age of a class of ten students. Prompt the user to enter the age of each
student.”
\\ Case Sensitive
⮚ Average can be calculated by doing addition of 10 variables and dividing sum with 10.
TotalAge = age1 + age2 + age3 + age4 + age5 + age6 + age7 + age8 +age9 + age10;
Solution:
#include<iostream>
int main(){
cin>>age1;
cin>>age2;
cin>>age3;
cin>>age4;
cout<<"please enter the age of student 5 ";
cin>>age5;
cin>>age6;
cin>>age7;
cin>>age8;
cin>>age9;
cin>>age10;
Alternative Solution
#include<iostream>
// using for loop to take input of each students and adding them
for (int i = 1;i<=10;i++){
cin>>age;
TotalAge += age;