0% found this document useful (0 votes)
31 views7 pages

OOP Lab 1

The document contains 10 programming tasks involving dynamic arrays. The tasks include creating and filling a dynamic array, finding the sum and maximum/minimum of elements, counting even numbers, reversing and copying arrays, removing duplicates, and multiplying matrices.

Uploaded by

abdulmueezali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views7 pages

OOP Lab 1

The document contains 10 programming tasks involving dynamic arrays. The tasks include creating and filling a dynamic array, finding the sum and maximum/minimum of elements, counting even numbers, reversing and copying arrays, removing duplicates, and multiplying matrices.

Uploaded by

abdulmueezali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name: Abdul Moiz Ali

Reg ID: 231239

Class: BSAI-1-B

Subject: OOP Lab

Assignment:1

Submitted to: UMME SADIMA


Lab-Tasks:
1) Write a code to create a dynamic array and fill it with user input values.
Program:
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int* dynamicArray = new int[size];
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> dynamicArray[i];
}
cout << "The elements of the array are:\n";
for (int i = 0; i < size; ++i) {
cout << dynamicArray[i] << " ";
}
cout << endl;
return 0;
}

2) Write a code to find the sum of all elements in the dynamic array.
Program:
using namespace std;

int main() {
int size;
int sum = 0;
cout << "Enter the size of the array: ";
cin >> size;
int* dynamicArray = new int[size];
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> dynamicArray[i];
}
for (int i = 0; i < size; ++i) {
sum += dynamicArray[i];
}
cout << "The sum of all elements in the array is: " << sum << endl;
return 0;
}

3) Write a code to count the number of even numbers in the dynamic array.
Program:
#include <iostream>

#include <iostream>

using namespace std;


int main() {
int size;
int evenCount = 0;
cout << "Enter the size of the array: ";
cin >> size;
int* dynamicArray = new int[size];
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> dynamicArray[i];
}
for (int i = 0; i < size; ++i) {
if (dynamicArray[i] % 2 == 0) {
evenCount++;
}
}
cout << "The number of even numbers in the array is: " << evenCount << endl;
return 0;
}

4) Write a code to copy elements from one dynamic array into another
Program:
#include <iostream>

#include <iostream>

using namespace std;

int main() {
int size;
int evenCount = 0;
cout << "Enter the size of the array: ";
cin >> size;
int* dynamicArray = new int[size];
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> dynamicArray[i];
}
for (int i = 0; i < size; ++i) {
if (dynamicArray[i] % 2 == 0) {
evenCount++;
}
}
cout << "The number of even numbers in the array is: " << evenCount << endl;
return 0;
}

5) Write a code to reverse the elements in the dynamic array.

Program:
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int* dynamicArray = new int[size];
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> dynamicArray[i];
}
for (int i = 0; i < size / 2; ++i) {
int temp = dynamicArray[i];
dynamicArray[i] = dynamicArray[size - i - 1];
dynamicArray[size - i - 1] = temp;
}
cout << "The reversed array is:\n";
for (int i = 0; i < size; ++i) {
cout << dynamicArray[i] << " ";
}
cout << endl;
return 0;
}

6) Write a code to find the maximum of elements in the dynamic array.


Program:
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int* dynamicArray = new int[size];
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> dynamicArray[i];
}
int maximum = dynamicArray[0];
for (int i = 1; i < size; ++i) {
if (dynamicArray[i] > maximum) {
maximum = dynamicArray[i];
}
}
cout << "The maximum element in the array is: " << maximum << endl;

return 0;
}

7) Write a code to remove the duplicates from the dynamic array.


Program:
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int* dynamicArray = new int[size];
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> dynamicArray[i];
}
unordered_set<int> uniqueElements;
cout << "Array with duplicates removed: ";
for (int i = 0; i < size; ++i) {
if (uniqueElements.insert(dynamicArray[i]).second) {
cout << dynamicArray[i] << " ";
}
}
cout << endl;
return 0;
}

8) Write a code to find the minimum of elements in the dynamic array


Program:
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int* dynamicArray = new int[size];
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> dynamicArray[i];
}
int minimum = dynamicArray[0];
for (int i = 1; i < size; ++i) {
if (dynamicArray[i] < minimum) {
minimum = dynamicArray[i];
}
}
cout << "The minimum element in the array is: " << minimum << endl;
return 0;
}

9) Write a code to find square of each element in the dynamic array.


Program:
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int* dynamicArray = new int[size];
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> dynamicArray[i];
}
cout << "Squares of elements in the array:\n";
for (int i = 0; i < size; ++i) {
cout << dynamicArray[i] << "^2 = " << dynamicArray[i] * dynamicArray[i] << endl;
}
return 0;
}

10) Write a code to multiply the matrix using 2D arrays.


Program:
#include <iostream>
using namespace std;
int main() {
int rows1, cols1, rows2, cols2;
cout << "Enter the number of rows and columns of the first matrix: ";
cin >> rows1 >> cols1;
cout << "Enter the number of rows and columns of the second matrix: ";
cin >> rows2 >> cols2;
if (cols1 != rows2) {
cout << "Matrix multiplication is not possible: Number of columns of the first matrix must be equal to the number of
rows of the second matrix." << endl;
return 1;
}
int matrix1[rows1][cols1], matrix2[rows2][cols2], resultMatrix[rows1][cols2];
cout << "Enter elements of the first matrix:\n";
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols1; ++j) {
cin >> matrix1[i][j];
}
}
cout << "Enter elements of the second matrix:\n";
for (int i = 0; i < rows2; ++i) {
for (int j = 0; j < cols2; ++j) {
cin >> matrix2[i][j];
}
}
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
resultMatrix[i][j] = 0;
for (int k = 0; k < cols1; ++k) {
resultMatrix[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
cout << "Resultant matrix after multiplication:\n";
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
cout << resultMatrix[i][j] << " ";
}
cout << endl;
}
return 0;
}

You might also like