Oop Lab 3: Muhammad Muaaz Ulhaq
Oop Lab 3: Muhammad Muaaz Ulhaq
Oop Lab 3: Muhammad Muaaz Ulhaq
3/25/2021
}
int max, min, avgClass = 0, sumClass = 0;
max = totalmarks[0], min = totalmarks[0];
for (int i = 0; i < noOfstuds; i++) {
if (totalmarks[i] > max) {
max = totalmarks[i];
}
if (totalmarks[i] < min) {
min = totalmarks[i];
}
sumClass = totalmarks[i] + sumClass;
}
avgClass = sumClass / noOfstuds;
file << "Class Average = " << avgClass << "\t";
file << "Minimum in Class = " << min << "\t" << "Maximum in Class = " << max <<
"\t" << endl;
file.close();
ifstream readfile;
readfile.open("grades.txt", ios::in);
char arr[100] = { 0 };
while (!readfile.eof()) {
readfile.getline(arr,100);
cout << arr << endl;
}
readfile.close();
}
Task-No 3
#include <iostream>
#include <cstring>
using namespace std;
char* getString(char* ptr, int& size);
int main() {
char* cPtr=nullptr;
int sizeCounter = 0;
getString(cPtr,sizeCounter);
system("pause");
return 0;
}
char* getString(char* ptr, int& size) {
char local[5][20]; // because we have to make 2D array of 100 elements
size = 0;
cout << "Enter a sentence: ";
cin.get(local[0],20);
size = strlen(local[0]);
ptr = new char[size + 1]; // because we have to make one index for null
character
ptr = local[0];
cout << "Array copied in dynamic array: \n";
for (int i = 0; i < size; i++) {
cout << *(ptr + i);
}
cout << endl;
return ptr;
}
Task-No 4
#include <iostream>
using namespace std;
void input(int** p, int row, int col);
void display(int** p, int row, int col);
int** sum(int** p, int row, int col, int** q, int row2, int col2);
int main() {
int** a = nullptr;
int** b = nullptr;
int** c = nullptr;
int rowA, rowB, colA, colB, sizeCounterA = 0, sizeCounterB = 0;
cout << "Enter rows of Matrix A: ";
cin >> rowA;
cout << "Enter columns of Matrix A: ";
cin >> colA;
a = new int* [rowA];
for (int i = 0; i < rowA; i++) { // making 2d dynamic array
a[i] = new int[colA];
}
input(a, rowA, colA);
cout << "Enter rows of Matrix B: ";
cin >> rowB;
cout << "Enter columns of Matrix B: ";
cin >> colB;
b = new int* [rowB];
for (int i = 0; i < rowB; i++) { // making 2d dynamic array
b[i] = new int[colB];
}
input(b, rowB, colB);
c=sum(a, rowA, colA, b, rowB, colB);
cout << "Matrix A: \n";
display(a, rowA, colA);
cout << "Matrix B: \n";
display(b, rowB, colB);
cout << "Matrix C: \n";
display(c, rowA, colA);
system("pause");
return 0;
}
void input(int** p, int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << "Enter col " << j + 1 << " of row " << i + 1 << " : ";
cin >> p[i][j];
}
}
}
void display(int** p, int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << p[i][j] << " ";
}
cout << endl;
}
}
int** sum(int** p, int row, int col, int** q, int row2, int col2) {
int** r = nullptr;
if (row == row2 && col == col2) { // condition to check their sizes
r = new int* [row];
for (int i = 0; i < row; i++) {
r[i] = new int[col];
for (int j = 0; j < col; j++) {
r[i][j] = p[i][j] + q[i][j]; // adding matrix A and B
}
}
}
else
cout << "Matrix A and B have not equal size!\n";
return r;
}
Task-No 5
#include <iostream>
using namespace std;
int* columns = nullptr;
void printing(int** c, int& r);
void filling(int** p, int& r);
int main() {
int** matrix = nullptr;
int row;
cout << "Enter the number of rows: ";
cin >> row;
matrix = new int* [row];
columns = new int[row];
filling(matrix, row);
printing(matrix, row);
system("pause");
return 0;
}
void filling(int** p, int& r) {
for (int i = 0; i < r; i++) {
cout << "Enter the number of col in row " << i + 1 << " : ";
cin >> columns[i];
p[i] = new int[columns[i]];
for (int j = 0; j < columns[i]; j++) {
cout << "Enter number in col " << j + 1 << " : ";
cin >> p[i][j];
}
}
}
void printing(int** c, int& r) {
cout << "Matrix is: \n";
for (int i = 0; i < r; i++) {
for (int j = 0; j < columns[i]; j++) {
cout << c[i][j] << " ";
}
cout << endl;
}
}