You are on page 1of 7

Task1:

#include <iostream>

using namespace std;

// Time complexity: O(N^2)


void populateArray(int**arr, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << "Enter the value for a[" << i << "][" << j << "]: ";
cin >> arr[i][j];
}
}
}

// Time complexity: O(N^2)


void sortAscending(int**arr, int n) {
int temp = 0;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-1 ; j++) {

if (arr[i][j] > arr[i][j + 1]) {


temp = arr[i][j];
arr[i][j] = arr[i][j + 1];
arr[i][j + 1] = temp;
}

}
}

for (int i = 0; i < n - 1; i++) {


for (int j = 0; j < n - 1; j++) {
if (arr[i][j] > arr[i][j + 1]) {
temp = arr[i][j];
arr[i][j] = arr[i][j + 1];
arr[i][j + 1] = temp;
}
}
}
}

// Time complexity: O(N^2)


void sortDescending(int**arr, int n) {
int temp = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (arr[i][j] < arr[i][j + 1]) {
temp = arr[i][j];
arr[i][j] = arr[i][j + 1];
arr[i][j+1] = temp;
}
}
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (arr[i][j] < arr[i][j + 1]) {
temp = arr[i][j];
arr[i][j] = arr[i][j + 1];
arr[i][j + 1] = temp;
}
}
}
}

// Time complexity: O(N^2)


void displayArray(int**arr, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}

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];

// Populate the 2D array


populateArray(arr, n);

// Sort the 2D array in ascending order row-wise


sortAscending(arr, n);

cout << "2D array in ascending order row-wise:" << endl;


displayArray(arr, n);

// Sort the 2D array in descending order column-wise


sortDescending(arr, n);

cout << "2D array in descending order column-wise:" << endl;


displayArray(arr, 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:

(n-1) + (n-2) + …. Which is n(n-1)/2

The total time complexity of this pseudocode is O(n^2).


B: The outer loops runs n-1 times and the inner loops depends on the outer loop and it runs
following times:

(n-1) + (n-2) + …. Which is n(n-1)/2

The total time complexity of this pseudocode is O(n^2).

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:

(n-1) + (n-2) + …. Which is n(n-1)/2

The total time complexity of this pseudocode is O(n^2).

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) {

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


for (int j = 0; j < width; j++) {
cImg.img[i][j] = img[i][j];
}
}
}

//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);

for (int i = 0; i < height; i++)


for (int j = 0; j < width; j++)
img[i][j] /= mean;
}

//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();

//make copy function


cout << "+++++++++copy image+++++++++" << endl;
img.makeCopy(copyimg);
copyimg.displayImage();

return 0;
}

You might also like