Professional Documents
Culture Documents
#include <iostream>
}
}
int main() {
int n;
cout << "Enter the size of the 2D array (n x n): ";
cin >> n;
int** arr;
arr = new int* [n];
for (int i = 0; i < n; i++)
arr[i] = new int[n];
return 0;
}
Task2:
Task3:
A: The outer loops runs n-1 times and the inner loops depends on the outer loop and it runs
following times:
C: : The outer loops runs n-1 times and the inner loops depends on the outer loop and it executes
one lesser time with the every iteration of the outer loop so it is decreasing one time every iteration,
so it runs following times:
D: This case has 2 cases. In the best case scenario it will executes N number of times if the conditions
met and the time complexity for it would be:
O(n)
In the worst case scenario it get into nested loop then its time complexity would be:
O(n^2)
Task4:
#include<iostream>
using namespace std;
class image{
int** img;
int height;
int width;
public:
image(){}
//O(n)
image(int h ,int w) {
height = h;
width = w;
img = new int*[h];
for (int i = 0; i < h; i++)
img[i] = new int[w];
}
// O(n^2)
void makeEmpty(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
img[i][j] = 0;
}
}
//O(1)
void storeValue(int i,int j,int value) {
img[i][j] = value;
}
//O(n^2)
void add(image& img1) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
img[i][j] += img1.img[i][j];
}
}
}
//O(n^2)
void Subtract(image& img1) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
img[i][j] -= img1.img[i][j];
}
}
}
//O(n^2)
void makeCopy(image& cImg) {
//O(n^2)
void transformation() {
float mean = 0.0;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
mean += img[i][j];
mean = mean / (height * width);
//O(n^2)
void displayImage() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++)
cout << img[i][j] << " ";
cout << endl;
}
}
//O(n)
~image() {
for (int i = 0; i < height; i++)
delete[]img[i];
delete[]img;
}
};
int main() {
int height, width;
cout << "Enter height: ";
cin >> height;
cout << "Enter width: ";
cin >> width;
image img(height,width);
image copyimg(height, width);
//make empty function
img.makeEmpty(10);
//store valuye function
img.storeValue(3, 2, 10);
img.storeValue(4, 8, 45);
img.storeValue(1, 2, 23);
img.storeValue(4, 6, 69);
img.storeValue(8, 2, 50);
img.displayImage();
cout << "======transform============" << endl;
//transformation function
img.transformation();
img.displayImage();
return 0;
}