OOP Lab 1
OOP Lab 1
OOP Lab 1
Class: BSAI-1-B
Assignment:1
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>
4) Write a code to copy elements from one dynamic array into another
Program:
#include <iostream>
#include <iostream>
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;
}
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;
}
return 0;
}