96
lab2/4.cpp
Normal file
96
lab2/4.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
class Matrix {
|
||||
private:
|
||||
unsigned int r, c;
|
||||
vector<vector<int>> matrix;
|
||||
|
||||
public:
|
||||
Matrix() = default;
|
||||
Matrix(int, int);
|
||||
void init(vector<vector<int>>);
|
||||
unsigned int getRows();
|
||||
unsigned int getColumns();
|
||||
Matrix operator*(Matrix);
|
||||
void display();
|
||||
};
|
||||
|
||||
Matrix::Matrix(int r, int c) {
|
||||
this->r = r, this->c = c;
|
||||
this->matrix = vector<vector<int>>(r, vector<int>(c));
|
||||
}
|
||||
|
||||
void Matrix::init(vector<vector<int>> src) {
|
||||
unsigned int i, j;
|
||||
|
||||
if (this->r > src.size())
|
||||
exit(1);
|
||||
|
||||
for (i = 0; i < this->r; i++) {
|
||||
if (c > src[i].size())
|
||||
exit(1);
|
||||
|
||||
for (int j = 0; j < this->c; j++)
|
||||
this->matrix[i][j] = src[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
inline unsigned int Matrix::getRows() { return r; }
|
||||
|
||||
inline unsigned int Matrix::getColumns() { return c; }
|
||||
|
||||
Matrix Matrix::operator*(Matrix operand) {
|
||||
unsigned int i, j, k;
|
||||
unsigned int r, c;
|
||||
unsigned int common;
|
||||
Matrix m;
|
||||
|
||||
if (this->getColumns() != operand.getRows())
|
||||
exit(1);
|
||||
|
||||
r = this->getRows();
|
||||
c = operand.getColumns();
|
||||
|
||||
m = Matrix(r, c);
|
||||
|
||||
common = this->getColumns();
|
||||
|
||||
for (i = 0; i < r; i++) {
|
||||
for (j = 0; j < c; j++) {
|
||||
int s = 0;
|
||||
for (k = 0; k < common; k++)
|
||||
s += this->matrix[i][k] * operand.matrix[k][j];
|
||||
m.matrix[i][j] = s;
|
||||
}
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void Matrix::display() {
|
||||
unsigned int i, j;
|
||||
|
||||
for (i = 0; i < this->r; i++) {
|
||||
for (j = 0; j < this->c; j++)
|
||||
cout << this->matrix[i][j] << ' ';
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
Matrix a(2, 3), b(3, 4);
|
||||
Matrix m;
|
||||
|
||||
a.init(vector<vector<int>>{{1, 2, -3}, {3, 2, 1}});
|
||||
|
||||
b.init(
|
||||
vector<vector<int>>{{1, -4, -4, 1}, {0, 4, 4, -34}, {3, 1231, 0, -9653}});
|
||||
|
||||
m = a * b;
|
||||
|
||||
m.display();
|
||||
|
||||
return 0;
|
||||
}
|
56
lab2/5.cpp
Normal file
56
lab2/5.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Time {
|
||||
private:
|
||||
uint h, m, s;
|
||||
|
||||
public:
|
||||
Time() = default;
|
||||
Time(uint, uint, uint);
|
||||
Time operator+(Time);
|
||||
void display();
|
||||
};
|
||||
|
||||
Time::Time(uint h, uint m, uint s) {
|
||||
if (m > 59 || s > 59)
|
||||
exit(1);
|
||||
|
||||
this->h = h;
|
||||
this->m = m;
|
||||
this->s = s;
|
||||
}
|
||||
|
||||
void Time::display() {
|
||||
cout << "Hours: " << this->h << endl
|
||||
<< "Minutes: " << this->m << endl
|
||||
<< "Seconds: " << this->s << endl;
|
||||
}
|
||||
|
||||
Time Time::operator+(Time op) {
|
||||
Time t;
|
||||
|
||||
t.h = this->h + op.h;
|
||||
t.m = this->m + op.m;
|
||||
t.s = this->s + op.s;
|
||||
|
||||
t.m += t.s / 60;
|
||||
t.s %= 60;
|
||||
|
||||
t.h += t.m / 60;
|
||||
t.m %= 60;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Time t;
|
||||
Time a(64, 32, 7);
|
||||
Time b(3, 59, 53);
|
||||
|
||||
t = a + b;
|
||||
|
||||
t.display();
|
||||
|
||||
return 0;
|
||||
}
|
72
lab2/6.cpp
Normal file
72
lab2/6.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const uint NSUBS = 5;
|
||||
const uint NSTUDENTS = 5;
|
||||
|
||||
class Student {
|
||||
private:
|
||||
char name[30];
|
||||
char branch[20];
|
||||
uint enrollment;
|
||||
uint age;
|
||||
char gender;
|
||||
uint marks[NSUBS];
|
||||
double percentage;
|
||||
|
||||
public:
|
||||
void input();
|
||||
char *getName();
|
||||
double getPercentage();
|
||||
};
|
||||
|
||||
void Student::input() {
|
||||
uint s = 0, i;
|
||||
|
||||
cout << "Name: ";
|
||||
cin >> this->name;
|
||||
cout << "Branch: ";
|
||||
cin >> this->branch;
|
||||
cout << "Enter Enrollment number: ";
|
||||
cin >> this->enrollment;
|
||||
cout << "Age: ";
|
||||
cin >> this->age;
|
||||
cout << "Gender (m/f): ";
|
||||
cin >> this->gender;
|
||||
|
||||
if (this->gender != 'm' || this->gender != 'f')
|
||||
exit(1);
|
||||
|
||||
cout << "Enter Marks for" << endl;
|
||||
|
||||
for (i = 0; i < NSUBS; i++) {
|
||||
cout << "Subject " << i + 1 << ": ";
|
||||
cin >> this->marks[i];
|
||||
|
||||
if (marks[i] > 100)
|
||||
exit(1);
|
||||
|
||||
s += this->marks[i];
|
||||
}
|
||||
|
||||
this->percentage = (double)s / NSUBS;
|
||||
}
|
||||
|
||||
inline char *Student::getName() { return this->name; }
|
||||
inline double Student::getPercentage() { return this->percentage; }
|
||||
|
||||
int main() {
|
||||
int i;
|
||||
|
||||
Student a[NSTUDENTS];
|
||||
|
||||
for (i = 0; i < NSTUDENTS; i++)
|
||||
a[i].input();
|
||||
|
||||
for (i = 0; i < NSTUDENTS; i++)
|
||||
if (a[i].getPercentage() > 70)
|
||||
cout << a[i].getName() << endl;
|
||||
|
||||
return 0;
|
||||
}
|
255
lab2/file.org
Normal file
255
lab2/file.org
Normal file
@@ -0,0 +1,255 @@
|
||||
* Write a program for multiplication of two matrices using OOP. [Hint: Declare class maths and then define variables and functions inside the class].
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 4.cpp :results output :exports both :wrap src text
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
class Matrix {
|
||||
private:
|
||||
unsigned int r, c;
|
||||
vector<vector<int>> matrix;
|
||||
|
||||
public:
|
||||
Matrix() = default;
|
||||
Matrix(int, int);
|
||||
void init(vector<vector<int>>);
|
||||
unsigned int getRows();
|
||||
unsigned int getColumns();
|
||||
Matrix operator*(Matrix);
|
||||
void display();
|
||||
};
|
||||
|
||||
Matrix::Matrix(int r, int c) {
|
||||
this->r = r, this->c = c;
|
||||
this->matrix = vector<vector<int>>(r, vector<int>(c));
|
||||
}
|
||||
|
||||
void Matrix::init(vector<vector<int>> src) {
|
||||
unsigned int i, j;
|
||||
|
||||
if (this->r > src.size())
|
||||
exit(1);
|
||||
|
||||
for (i = 0; i < this->r; i++) {
|
||||
if (c > src[i].size())
|
||||
exit(1);
|
||||
|
||||
for (int j = 0; j < this->c; j++)
|
||||
this->matrix[i][j] = src[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
inline unsigned int Matrix::getRows() { return r; }
|
||||
|
||||
inline unsigned int Matrix::getColumns() { return c; }
|
||||
|
||||
Matrix Matrix::operator*(Matrix operand) {
|
||||
unsigned int i, j, k;
|
||||
unsigned int r, c;
|
||||
unsigned int common;
|
||||
Matrix m;
|
||||
|
||||
if (this->getColumns() != operand.getRows())
|
||||
exit(1);
|
||||
|
||||
r = this->getRows();
|
||||
c = operand.getColumns();
|
||||
|
||||
m = Matrix(r, c);
|
||||
|
||||
common = this->getColumns();
|
||||
|
||||
for (i = 0; i < r; i++) {
|
||||
for (j = 0; j < c; j++) {
|
||||
int s = 0;
|
||||
for (k = 0; k < common; k++)
|
||||
s += this->matrix[i][k] * operand.matrix[k][j];
|
||||
m.matrix[i][j] = s;
|
||||
}
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void Matrix::display() {
|
||||
unsigned int i, j;
|
||||
|
||||
for (i = 0; i < this->r; i++) {
|
||||
for (j = 0; j < this->c; j++)
|
||||
cout << this->matrix[i][j] << ' ';
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
Matrix a(2, 3), b(3, 4);
|
||||
Matrix m;
|
||||
|
||||
a.init(vector<vector<int>>{{1, 2, -3}, {3, 2, 1}});
|
||||
|
||||
b.init(
|
||||
vector<vector<int>>{{1, -4, -4, 1}, {0, 4, 4, -34}, {3, 1231, 0, -9653}});
|
||||
|
||||
m = a * b;
|
||||
|
||||
m.display();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
#+begin_src text
|
||||
-8 -3689 4 28892
|
||||
6 1227 -4 -9718
|
||||
#+end_src
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Create a class TIME with members hours, minutes, seconds. Take input, add two time objects passing objects to function and display the result. [Hint: use class object as parameter of function add()]
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 5.cpp :results output :exports both :wrap src text
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Time {
|
||||
private:
|
||||
uint h, m, s;
|
||||
|
||||
public:
|
||||
Time() = default;
|
||||
Time(uint, uint, uint);
|
||||
Time operator+(Time);
|
||||
void display();
|
||||
};
|
||||
|
||||
Time::Time(uint h, uint m, uint s) {
|
||||
if (m > 59 || s > 59)
|
||||
exit(1);
|
||||
|
||||
this->h = h;
|
||||
this->m = m;
|
||||
this->s = s;
|
||||
}
|
||||
|
||||
void Time::display() {
|
||||
cout << "Hours: " << this->h << endl
|
||||
<< "Minutes: " << this->m << endl
|
||||
<< "Seconds: " << this->s << endl;
|
||||
}
|
||||
|
||||
Time Time::operator+(Time op) {
|
||||
Time t;
|
||||
|
||||
t.h = this->h + op.h;
|
||||
t.m = this->m + op.m;
|
||||
t.s = this->s + op.s;
|
||||
|
||||
t.m += t.s / 60;
|
||||
t.s %= 60;
|
||||
|
||||
t.h += t.m / 60;
|
||||
t.m %= 60;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Time t;
|
||||
Time a(64, 32, 7);
|
||||
Time b(3, 59, 53);
|
||||
|
||||
t = a + b;
|
||||
|
||||
t.display();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
#+begin_src text
|
||||
Hours: 68
|
||||
Minutes: 32
|
||||
Seconds: 0
|
||||
#+end_src
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Create a class Student which has data members as name, branch, roll no, age , gender, marks in five subjects. Display the name of the student and his percentage who has more than 70%.Use array of objects size 5.[Hint: also declare percentage variable and calculate it and then compare all the object of student class to find name of student with percentage > 70]
|
||||
|
||||
#+begin_src cpp :tangle 6.cpp :results output :exports both :wrap src text
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const uint NSUBS = 5;
|
||||
const uint NSTUDENTS = 5;
|
||||
|
||||
class Student {
|
||||
private:
|
||||
char name[30];
|
||||
char branch[20];
|
||||
uint enrollment;
|
||||
uint age;
|
||||
char gender;
|
||||
uint marks[NSUBS];
|
||||
double percentage;
|
||||
|
||||
public:
|
||||
void input();
|
||||
char *getName();
|
||||
double getPercentage();
|
||||
};
|
||||
|
||||
void Student::input() {
|
||||
uint s = 0, i;
|
||||
|
||||
cout << "Name: ";
|
||||
cin >> this->name;
|
||||
cout << "Branch: ";
|
||||
cin >> this->branch;
|
||||
cout << "Enter Enrollment number: ";
|
||||
cin >> this->enrollment;
|
||||
cout << "Age: ";
|
||||
cin >> this->age;
|
||||
cout << "Gender (m/f): ";
|
||||
cin >> this->gender;
|
||||
|
||||
if (this->gender != 'm' || this->gender != 'f')
|
||||
exit(1);
|
||||
|
||||
cout << "Enter Marks for" << endl;
|
||||
|
||||
for (i = 0; i < NSUBS; i++) {
|
||||
cout << "Subject " << i + 1 << ": ";
|
||||
cin >> this->marks[i];
|
||||
|
||||
if (marks[i] > 100)
|
||||
exit(1);
|
||||
|
||||
s += this->marks[i];
|
||||
}
|
||||
|
||||
this->percentage = (double)s / NSUBS;
|
||||
}
|
||||
|
||||
inline char *Student::getName() { return this->name; }
|
||||
inline double Student::getPercentage() { return this->percentage; }
|
||||
|
||||
int main() {
|
||||
int i;
|
||||
|
||||
Student a[NSTUDENTS];
|
||||
|
||||
for (i = 0; i < NSTUDENTS; i++)
|
||||
a[i].input();
|
||||
|
||||
for (i = 0; i < NSTUDENTS; i++)
|
||||
if (a[i].getPercentage() > 70)
|
||||
cout << a[i].getName() << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#+end_src
|
Reference in New Issue
Block a user