Rabia Dsa Lab 1
Rabia Dsa Lab 1
Write the prototype of a function named DS() which accepts an array of integers, a
Answer:
int a[]={1,2,3,4,5};
int *p;
p = a;
Cout<<”elements”<<a[i]<<endl;
Cout<<”elements”<<p[i]<<endl;
Function overloading: refers to the ability to define multiple functions in the same class or scope with
the same name but different parameter lists (i.e., a different number or type of parameters). It allows
you to create functions with the same name that perform different tasks based on their parameter
signatures.
4. Write C++ statement(s) to allocate space for 10 doubles (using dynamic memory allocation).
Double *ptr;
Ptr=new double[10];
5 Study the given program and determine what the program is intended to do. (Hint: Dry
run the program with array {1,2,1,2,3} and analyze the output).
Consider we have entered 1,2,1,2,3 in array.first the program will compare the first index value with the
second index value if both are save it will remover the dublicate element and print the array unique
elements so, the main purpose of program is to remove dublicate elements of array.
Write a C++ function which accepts an array of integers and the size of the array and finds :
In the main program, declare an array of 7 integers using dynamic memory allocation and call
the aforementioned function. Display the output of the function within the main.
include <iostream>
void Array(int arr[], int size, int& sum, double& average, int& minVal, int& maxVal) {
if (size <= 0) {
return;
sum = 0;
minVal = arr[0];
maxVal = arr[0];
sum += arr[i];
maxVal = arr[i];
int main() {
int size = 7;
std::cout << "Enter " << size << " integers:" << std::endl;
double average;
delete[] arr;
return 0;
}
Exercise 2
Write a program with a function which accepts an array of integers and a key value. The
function should return the sum of all the multiples of the key value in the array. For example,
for the array {1, 4, 10, 12, 15, 20, 22} and the key value 5, the function should return the sum
10+15+20.
Task#2
#include <iostream>
int sum = 0;
if (arr[i] % key == 0) {
sum += arr[i];
}
}
return sum;
int main() {
int size; std::cout << "Enter the size of the array: ";
scout << "Enter " << size << " integers:" << endl;
int key;
std::cout << "Sum of multiples of " << key << ": " << result << std::endl;
delete[] arr;
return 0;
}
Task #3
Exercise 3
Lab 1
overload the ‘+’ operator to add two matrices. Likewise, overload the ‘~’ operator to find the
determinant of the matrix. Also provide a display() member function to print the matrix. In the
main program, create an object of class Matrix and call its member functions.
#include <iostream>
class Matrix {
private:
int mat[2][2];
public:
Matrix() {
mat[i][j] = 0;
mat[0][0] = a;
mat[0][1] = b;
mat[1][0] = c;
mat[1][1] = d;
void get_input() {
Matrix result;
return result;
int operator~() {
void display() {
};
int main() {
std::cout << "Enter values for the first matrix:" << std::endl;
matrix1.get_input();
std::cout << "Enter values for the second matrix:" << endl;
matrix2.get_input();
result.display();
std::cout << "Determinant of the first matrix: " << determinant << std::endl;
return 0;