Oop Lab 3: Muhammad Muaaz Ulhaq

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

OOP LAB 3

3/25/2021

Muhammad Muaaz Ulhaq


20F-0408 BSE-2A
Task-No 1
#include <iostream>
using namespace std;
void initializeArray(int* p, int s);
void printArray(int* p, int s);
int* deepCopy(int* p, int s);
int main() {
int* arr;
int* ptr = nullptr;
int size, x;
cout << "Enter size of Array: ";
cin >> size;
arr = new int[size];
initializeArray(arr, size);
cout << "First Array: ";
printArray(arr, size);
ptr = deepCopy(arr, size);
cout << "Copied Array: ";
printArray(ptr, size);
cout << "Deleting First Array...";
delete[]arr;
cout << "After Deleting first Array, copied array is still safe: \n";
printArray(ptr, size);
system("pause");
return 0;
}
void initializeArray(int* p, int s) {
for (int i = 0; i < s; i++) {
p[i] = i * 3;
}
}
void printArray(int* p, int s) {
for (int i = 0; i < s; i++) {
cout << p[i] << " ";
}
cout << endl;
}
int* deepCopy(int* p, int s) {
int* arr2;
arr2 = new int[s];
for (int i = 0; i < s; i++) {
arr2[i] = p[i];
}
return arr2;
}
Task-No 2
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void reading(ifstream& red, string names[], float** marks, int& noOfstud, int&
noOflabs);
char grades(float& avg);
void average(float** marks, int& noOfstud, int& noOflabs, float* avg, float*
totalmarks, float& grandTot, char* studgrades);
void maxMinPrint(string names[], float* totalmarks, float* avg, float** marks, char*
studgrades, int& noOfstuds, int& noOflabs);
int main() {
int noOfstud = 0, noOflabs, length;
float** studentMarks = nullptr;
float* avg = nullptr;
float* totalMarks = nullptr;
char* studentGrades = nullptr;
float grandtotal = 375;
ifstream read;
string namesOfStudent[28];
char arr[100] = { 0 }, chr = 0;
read.open("students marks.txt", ios::in);
read.getline(arr, 100);
length = strlen(arr);
for (int i = 0; i < length; i++) {
cout << arr[i];
}
noOfstud = 28;
cout << endl;
read.getline(arr, 100);
length = strlen(arr);
for (int i = 0; i < length; i++) {
cout << arr[i];
}
noOflabs = 12;
studentMarks = new float* [noOflabs];
reading(read, namesOfStudent, studentMarks, noOfstud, noOflabs);
average(studentMarks, noOfstud, noOflabs, avg, totalMarks, grandtotal,
studentGrades);
maxMinPrint(namesOfStudent, totalMarks, avg, studentMarks, studentGrades,
noOfstud, noOflabs);
read.close();
system("pause");
return 0;
}
void reading(ifstream& red, string names[], float** marks, int& noOfstud, int&
noOflabs) {
int i = 0, j = 0;
for (i = 0; i < noOflabs; i++) {
marks[i] = new float[noOfstud];
}
for (i = 0; red >> names[i]; i++) {
for (j = 0; j < noOfstud; j++)
{
red >> marks[i][j];
}
}
}
char grades(float& avg) {
if (avg >= 80 && avg <= 100)
return 'A';
else if (avg >= 70)
return 'B';
else if (avg >= 50)
return 'C';
else if (avg >= 33)
return 'D';
else if (avg < 33)
return 'F';
}
void average(float** marks, int& noOfstud, int& noOflabs, float* avg, float*
totalmarks,float& grandTot,char* studgrades ){
float sum = 0;
for (int i = 0; i < noOfstud; i++) {
sum = 0;
for (int j = 0; j < noOflabs; j++) {
sum = sum + marks[i][j];
}
totalmarks[i] = sum; // storing every individual marks seprately
avg[i] = (sum / grandTot) * 100; // finding average of every individual
to determine grade
studgrades[i] = grades(avg[i]);
}
}
void maxMinPrint(string names[], float* totalmarks, float* avg, float** marks, char*
studgrades, int& noOfstuds, int& noOflabs) {
ofstream file;
file.open("grades.txt", ios::out);
for (int i = 0; i < noOfstuds; i++) {
file << names[i] << "\t";
for (int k = 0; k < 12; k++)
{
file << "\t" << floor(marks[i][k]);
}
file << "Total Marks = " << int(marks[i]) << "\t";
file << "Percentage = " << setprecision(4) << avg[i] << "\t";
file << "Grade = " << studgrades[i] << "\t" << endl;

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

You might also like