Files
ooplab/lab2/file.org
Amneesh Singh 7ba9554ed5 labs 7: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-26 16:53:39 +05:30

5.6 KiB

Write a program for multiplication of two matrices using OOP. [Hint: Declare class maths and then define variables and functions inside the class].

#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;
}

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()]

#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;
}

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]

#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;

  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();

  cout << "\nStudents with percentage greater than 70:\n";

  for (i = 0; i < NSTUDENTS; i++)
    if (a[i].getPercentage() > 70)
      cout << a[i].getName() << endl;

  return 0;
}
Name: Amaang
Branch: InformationTechnology
Enter Enrollment number: 413
Age: 14
Gender (m/f): m
Enter Marks for
Subject 1: 99
Subject 2: 93
Subject 3: 9
Subject 4: 91
Subject 5: 3
Name: Allu
Branch: Physics
Enter Enrollment number: 444
Age: 19
Gender (m/f): f
Enter Marks for
Subject 1: 100
Subject 2: 0
Subject 3: 100
Subject 4: 0
Subject 5: 100
Name: Asli
Branch: Law
Enter Enrollment number: 33
Age: 99
Gender (m/f): m
Enter Marks for
Subject 1: 1
Subject 2: 1
Subject 3: 1
Subject 4: 1
Subject 5: 2
Name: how
Branch: Music
Enter Enrollment number: 9999
Age: 9
Gender (m/f): m
Enter Marks for
Subject 1: 12
Subject 2: 22
Subject 3: 32
Subject 4: 42
Subject 5: 52
Name: sabji
Branch: CivilEngineering
Enter Enrollment number: 91

Students with percentage greater than 70:
sabji