Data Structures & Algorithms Lab Lab Journal 1: Give Answers To The Following
Data Structures & Algorithms Lab Lab Journal 1: Give Answers To The Following
Lab Journal 1
Give answers to the following
1. Write the prototype of a function named fnct() which accepts an array of integers, a
pointer to double, a float by reference and returns a character.
Char fnct( int a[],double *p,float &b )
{
Char character;
return character;
}
2. Write statements as directed:
int main()
{
int a[] = { 1, 2, 3, 4, 5 };
int *p;
p = a;
//Print the elements of array using the mentioned notation:
for (int i = 0; i < 5; i++)
{
//Subscript notation with name of array
cout << "a[" << i << "] = " << a[i] <<endl;
}
//Subscript notation with pointer 'p'
for (int j = 0; j <= 4; ++j)
{
cout << "pointer[" << j << "] = " << p[j] << endl;
}
//Offset notation using array name
for (int offset = 0; offset <= 4; ++offset)
{
cout << "*(a + " << offset << ") = " << *(a + offset) <<endl;
}
//Offset notation using pointer 'p'
for (int offset1 = 0; offset1 <= 4; ++offset1)
{
cout << "*(p + " << offset1 << ") = " << *(p + offset1) << endl;
}
3. What is the difference between function overloading and function overriding?
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).
The given array will display the valuses as it is but if we give repeated value to a
arraylike {2,2} then it display 2 one time otherwise it display the given array as it is.
2. Write a C++ function which accepts an array of integers and the size of the array and finds :
a. Sum of the elements in the array b. Average of the array elements c. Minimum and
maximum values in the array (Hint: Declare array of 10 integers.)
CODE:
#include <iostream>
#include<conio.h>
using namespace std;
// function to return sum of elements
// in an array of size n
int sum(int arr[], int n)
{
int sum = 0, i;
return sum / n;
}
//function to return maximum num of elements
// in an array of size n
int maximum(int arr[], int n)
{
int i;
int max = arr[0];
for (i = 1; i < n; i++)
{
if (arr[i] > max)
max = arr[i];
}
return max;
}
// function to return minimum num of elements
// in an array of size n
int minimum(int arr[], int n)
{
int i;
int min = arr[0];
for (i = 1; i < n; i++)
{
if (arr[i] < min)
min = arr[i];
}
return min;
}
int main()
{
int x, n;
cout << "Enter the number of items:" << "\n";
cin >> n;
int *arr = new int(n);
cout << "Enter " << n << " items" << endl;
for (x = 0; x < n; x++) {
cin >> arr[x];
}
cout << "You entered: " << endl;
for (x = 0; x < n; x++) {
cout << arr[x] << " ";
}
cout << endl;
cout << "Sum of given array is " << sum(arr, n);
cout << endl;
cout << "Average of given array is " << average(arr, n);
cout << endl;
cout << "maximum of given array is " << maximum(arr, n);
cout << endl;
cout << "minimum of given array is " << minimum(arr, n);
cout << endl;
system("pause");
_getch();
return 0;
}
OUTPUT:
3. Create a class Matrix to model 2x2 matrices. Provide a default and parameterized constructor to
assign values to the matrix. Using a member function, 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.
CODE:
#include <iostream>
#include<conio.h>
#include <string>
using namespace std;
class matrix
{
int m, n, x[12][10];
public:
matrix() :m(0), n(0) //default constructor
{}
matrix(int a, int b) //paramatized constructor
{
m = a;
n = b;
}
void getdata()
{
cout << "\n Enter values into the matrix" << endl;
for (int i = 0; i<m; i++)
for (int j = 0; j<n; j++)
cin >> x[i][j];
}
void display()
{
cout << "\n Given matrix is" << endl;
for (int i = 0; i<m; i++)
{
for (int j = 0; j<n; j++)
cout << x[i][j] << "\t";
cout << endl;
}
}
matrix operator +(matrix b)
{
matrix c(m, n);
for (int i = 0; i<m; i++)
for (int j = 0; j<n; j++)
c.x[i][j] = x[i][j] + b.x[i][j];
return c;
}
int operator ~()
{
int det;
det = ((x[0][0] * x[1][1]) - (x[1][0] * x[0][1]));
cout << "Determinant of matrix is ::";
cout << det;
cout << endl;
return 0;
}
};
int main()
{
int m, n;
cout << "\n Enter the size of the array" << endl;
cin >> m >> n;
matrix a(m, n), b(m, n), c;
a.getdata();
b.getdata();
c = a + b;
c.display();
~c;
system("pause");
_getch();
return 0;
}
OUTPUT: