0% found this document useful (0 votes)
17 views13 pages

OOP Lab Task

The document contains a series of C++ programs demonstrating various programming concepts such as arrays, matrices, string manipulation, functions, pointers, and dynamic memory allocation. Each program includes user input and output, showcasing functionalities like reversing an array, checking even/odd numbers, swapping values, and managing structures. The final programs also illustrate memory management using both 'new' and 'malloc' for dynamic array allocation.

Uploaded by

f2024408034
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)
17 views13 pages

OOP Lab Task

The document contains a series of C++ programs demonstrating various programming concepts such as arrays, matrices, string manipulation, functions, pointers, and dynamic memory allocation. Each program includes user input and output, showcasing functionalities like reversing an array, checking even/odd numbers, swapping values, and managing structures. The final programs also illustrate memory management using both 'new' and 'malloc' for dynamic array allocation.

Uploaded by

f2024408034
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/ 13

Name : Noor Fatima

Course : OOP Lab


ID : F2024408054
OOP Lab Task
Program .1
#include <iostream>

using namespace std;

int main() {

int arr[5];

cout << "Enter 5 integers: " << endl;

for (int i = 0; i < 5; i++) {

cin >> arr[i];

cout << "Array in reverse order: ";

for (int i = 4; i >= 0; i--) {

cout << arr[i] << " ";

return 0;

}
Program .2
#include <iostream>

using namespace std;

int main()

int arr[2][3];

cout << "Enter 6 integers to fill the 2x3 matrix:" << endl;

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 3; j++) {

cin >> arr[i][j];

cout << "The matrix is:" << endl;

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 3; j++) {

cout << arr[i][j] << " ";

}
cout << endl;

return 0;

Program . 3
#include <iostream>

#include <string>

#include <algorithm>

using namespace std;

int main() {

string name;

cout << "Enter your name: ";

getline(cin, name);

reverse(name.begin(), name.end());

cout << "Your name in reverse order is: " << name << endl;

return 0;

}
Program . 4
#include <iostream>

using namespace std;

bool isEven(int num) {

return num % 2 == 0;

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

if (isEven(number)) {

cout << number << " is even." << endl;

} else {

cout << number << " is odd." << endl;

}
return 0;

Program . 5
#include <iostream>

using namespace std;

void swap(int &a, int &b) {

int temp = a;

a = b;

b = temp;

int main() {

int num1, num2;

cout << "Enter the first number: ";

cin >> num1;

cout << "Enter the second number: ";


cin >> num2;

swap(num1, num2);

cout << "After swapping: " << endl;

cout << "First number: " << num1 << endl;

cout << "Second number: " << num2 << endl;

return 0;

Program . 6
#include <iostream>

using namespace std;

int main()

int num = 34;

int* ptr = &num;


cout << "Original value of num: " << num << endl;

*ptr = 57;

cout << "Updated value of num: " << num << endl;

return 0;

Program. 7
#include <iostream>

using namespace std;

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int* arr = new int[size];

cout << "Enter " << size << " integers to fill the array: " << endl;
for (int i = 0; i < size; i++) {

cin >> arr[i];

cout << "The array elements are: ";

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

cout << endl;

delete[] arr;

return 0;

Task . 8
#include <iostream>
using namespace std;

struct Person {

string name;

int age;

};

void passByValue(Person p) {

p.age += 5;

cout << "Inside passByValue function: " << p.name << " is now " << p.age << " years old." <<
endl;

void passByReference(Person &p)

p.age += 5;

cout << "Inside passByReference function: " << p.name << " is now " << p.age << " years old."
<< endl;

int main() {

Person person1 = {"John", 30};

cout << "Initial values in main: " << person1.name << " is " << person1.age << " years old."
<< endl;

passByValue(person1);

cout << "After passByValue function in main: " << person1.name << " is " << person1.age << "
years old." << endl;

passByReference(person1);

cout << "After passByReference function in main: " << person1.name << " is " << person1.age
<< " years old." << endl;

return 0;

}
Task . 9
#include <iostream>

using namespace std;

struct Person {

string name;

int age;

};

void input(Person people[], int size) {

for (int i = 0; i < size; i++) {

cout << "Enter details for person " << i + 1 << ":" << endl;

cout << "Enter name: ";

cin >> people[i].name;

cout << "Enter age: ";

cin >> people[i].age;

}
}

void display(const Person people[], int size) {

cout << "\nDisplaying information of all persons:\n";

for (int i = 0; i < size; i++) {

cout << "Person " << i + 1 << ": " << people[i].name << ", Age: " << people[i].age << endl;

int main()

const int SIZE = 3;

Person people[SIZE];

input(people, SIZE);

display(people, SIZE);

return 0;

Task . 10
#include <iostream>

#include <cstdlib>

using namespace std;

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int* arr = (int*)malloc(size * sizeof(int));

if (arr == nullptr ) {

cout << "Memory allocation failed!" << endl;

return 1;

cout << "Enter " << size << " elements for the array:" << endl;

for (int i = 0; i < size; i++) {

cout << "Element " << i + 1 << ": ";

cin >> arr[i];

cout << "\nThe elements in the array are:" << endl;

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

cout << endl;

free(arr);

cout << "Memory deallocated successfully." << endl;

return 0;

You might also like