Ip 1

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

: DATA STRUCTURE AND ALGORITHM

UNITY UNIVERSITY
DEPARTIMENTS OF COMPUTER SCIENCE

SECTION 2 COURSE TITLE: DATA STRUCTURE AND


ALGORITHM
INDIVIDUAL ASSGNMENT : 1

NAME ID

TIZITA TESFAYE 04569/14

SUBMITTED TO:TESHOME M.
SUBMITTED DATE

TIZITA TESFAYE 1 04569/14


: DATA STRUCTURE AND ALGORITHM

1. Write a C++ program to find the largest element of a given array of integers

#include <iostream>
using namespace std;

int findLargest(int arr[], int size) {


int largest = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
return largest;
}

int main() {
int arr[] = {5, 2, 9, 1, 7};
int size = sizeof(arr) / sizeof(arr[0]);

int largest = findLargest(arr, size);

cout << "Largest element: " << largest << endl;

return 0;
}

2. Write a C++ program to find the largest three elements in an array.

#include <iostream>
#include <algorithm>
using namespace std;

void findLargestThree(int arr[], int size) {


sort(arr, arr + size, greater<int>());

cout << "Largest three elements: ";


for (int i = 0; i < 3; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
int arr[] = {5, 2, 9, 1, 7};

TIZITA TESFAYE 2 04569/14


: DATA STRUCTURE AND ALGORITHM
int size = sizeof(arr) / sizeof(arr[0]);

findLargestThree(arr, size);

return 0;
}

3. Write a C++ program to find k largest elements in a given array of integers.

#include <iostream>
#include <algorithm>
using namespace std;

void findKLargest(int arr[], int size, int k) {


sort(arr, arr + size, greater<int>());

cout << "Largest " << k << " elements: ";


for (int i = 0; i < k; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
int arr[] = {5, 2, 9, 1, 7};
int size = sizeof(arr) / sizeof(arr[0]);
int k = 3;

findKLargest(arr, size, k);

return 0;
}

4. Write a C++ program to find the most occurring element in an array of integers

#include <iostream>
#include <unordered_map>
using namespace std;

int findMostOccurring(int arr[], int size) {


unordered_map<int, int> frequency;

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


frequency[arr[i]]++;
}

int mostOccurring = arr[0];


int maxFrequency = frequency[arr[0]];

TIZITA TESFAYE 3 04569/14


: DATA STRUCTURE AND ALGORITHM

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


if (frequency[arr[i]] > maxFrequency) {
mostOccurring = arr[i];
maxFrequency = frequency[arr[i]];
}
}

return mostOccurring;
}

int main() {
int arr[] = {5, 2, 9, 1, 7, 5};
int size = sizeof(arr) / sizeof(arr[0]);

int mostOccurring = findMostOccurring(arr, size);

cout << "Most occurring element: " << mostOccurring << endl;

return 0;
}

5. Write a C++ program to update every array element by multiplication of next and
previous values of a given array of integers

#include <iostream>
using namespace std;

void updateArray(int arr[], int size) {


int prev = arr[0];

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


int curr = arr[i];
int next = arr[i + 1];

arr[i] = prev * next;


prev = curr;
}

arr[size - 1] = prev * arr[size - 1];


}

int main() {
int arr[] = {5, 2, 9, 1, 7};
int size = sizeof(arr) / sizeof(arr[0]);

updateArray(arr, size);

TIZITA TESFAYE 4 04569/14


: DATA STRUCTURE AND ALGORITHM
cout << "Updated array: ";
for (int i0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}

6. Write a C++ program to find the two repeating elements in a given array of
integers.

#include <iostream>
#include <unordered_set>
using namespace std;

void findRepeatingElements(int arr[], int size) {


unordered_set<int> uniqueElements;
cout << "Repeating elements: ";

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


if (uniqueElements.find(arr[i]) != uniqueElements.end()) {
cout << arr[i] << " ";
} else {
uniqueElements.insert(arr[i]);
}
}

cout << endl;


}

int main() {
int arr[] = {5, 2, 9, 1, 7, 2, 5};
int size = sizeof(arr) / sizeof(arr[0]);

findRepeatingElements(arr, size);

return 0;
}
0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}

TIZITA TESFAYE 5 04569/14


: DATA STRUCTURE AND ALGORITHM
7. Write a menu driven C++ program to do following operation on two dimensional
array A of size m x n. You should use user-defined functions which accept 2-D array
A, and its size m and n as arguments. The options are:

a. To input elements into matrix of size m x n

b. To display elements of matrix of size m x n

c. Sum of all elements of matrix of size m x n

d. To display row-wise sum of matrix of size m x n

e. To display column-wise sum of matrix of size m x n

f. To create transpose of matrix B of size n x m

Here is the C++ code for the mentioned programs:

1. Menu driven program for operations on a 2D array:

#include <iostream>
using namespace std;

const int MAX_SIZE = 100;

void inputMatrix(int matrix[][MAX_SIZE], int m, int n) {


cout << "Enter matrix elements:" << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
}

void displayMatrix(int matrix[][MAX_SIZE], int m, int n) {


cout << "Matrix elements:" << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

TIZITA TESFAYE 6 04569/14


: DATA STRUCTURE AND ALGORITHM
int sumOfElements(int matrix[][MAX_SIZE], int m, int n) {
int sum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
sum += matrix[i][j];
}
}
return sum;
}

void displayRowWiseSum(int matrix[][MAX_SIZE], int m, int n) {


cout << "Row-wise sum of matrix:" << endl;
for (int i = 0; i < m; i++) {
int rowSum = 0;
for (int j = 0; j < n; j++) {
rowSum += matrix[i][j];
}
cout << "Row " << i + 1 << ": " << rowSum << endl;
}
}

void displayColumnWiseSum(int matrix[][MAX_SIZE], int m, int n) {


cout << "Column-wise sum of matrix:" << endl;
for (int j = 0; j < n; j++) {
int colSum = 0;
for (int i = 0; i < m; i++) {
colSum += matrix[i][j];
}
cout << "Column " << j + 1 << ": " << colSum << endl;
}
}

void createTranspose(int matrix[][MAX_SIZE], int m, int n, int transpose[]


[MAX_SIZE]) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
transpose[j][i] = matrix[i][j];
}
}
}

int main() {
int matrix[MAX_SIZE][MAX_SIZE];
int transpose[MAX_SIZE][MAX_SIZE];
int m, n;
char choice;

cout << "Enter number of rows and columns (m and n): ";
cin >> m >> n;

TIZITA TESFAYE 7 04569/14


: DATA STRUCTURE AND ALGORITHM

do {
cout << "Menu:" << endl;
cout << "a. To input elements into matrix of size m x n" << endl;
cout << "b. To display elements of matrix of size m x n" << endl;
cout << "c. Sum of all elements of matrix of size m x n" << endl;
cout << "d. To display row-wise sum of matrix of size m x n" << endl;
cout << "e. To display column-wise sum of matrix of size m x n" << endl;
cout << "f. To create transpose of matrix B of size n x m" << endl;
cout << "x. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 'a':
inputMatrix(matrix, m, n);
break;
case 'b':
displayMatrix(matrix, m, n);
break;
case 'c': {
int sum = sumOfElements(matrix, m, n);
cout << "Sum of elements: " << sum << endl;
break;
}
case 'd':
displayRowWiseSum(matrix,m, n);
break;
case 'e':
displayColumnWiseSum(matrix, m, n);
break;
case 'f':
createTranspose(matrix, m, n, transpose);
displayMatrix(transpose, n, m);
break;
case 'x':
cout << "Exiting program..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}

cout << endl;


} while (choice != 'x');

return 0;
}

TIZITA TESFAYE 8 04569/14


: DATA STRUCTURE AND ALGORITHM
8. Write a user defined function named Upper-half() which takes a two dimensional
array A, with size N rows and N columns as argument and prints the upper half of the
array.

e.g.,

23150 23150

71531 1531

2 5 7 8 1 Output will be: 178

01501 01

34915 5

#include <iostream>
using namespace std;

const int MAX_SIZE = 100;

void inputMatrix(int matrix[][MAX_SIZE], int m, int n) {


cout << "Enter matrix elements:" << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
}

void multiplyMatrix(int matrixA[][MAX_SIZE], int matrixB[][MAX_SIZE], int


result[][MAX_SIZE], int m, int l, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
result[i][j] = 0;
for (int k = 0; k < l; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
}

void displayMatrix(int matrix[][MAX_SIZE], int m, int n) {


cout << "Matrix elements:" << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << matrix[i][j] << " ";

TIZITA TESFAYE 9 04569/14


: DATA STRUCTURE AND ALGORITHM
}
cout << endl;
}
}

int main() {
int matrixA[MAX_SIZE][MAX_SIZE];
int matrixB[MAX_SIZE][MAX_SIZE];
int result[MAX_SIZE][MAX_SIZE];
int m, l, n;

cout << "Enter number of rows and columns for matrix A (m and l): ";
cin >> m >> l;

cout << "Enter number of rows and columns for matrix B (l and n): ";
cin >> l >> n;

if (l != l) {
cout << "Invalid input. Number of columns in matrix A should be equal to
number of rows in matrix B." << endl;
return 0;
}

inputMatrix(matrixA, m, l);
inputMatrix(matrixB, l, n);

multiplyMatrix(matrixA, matrixB, result, m, l, n);

cout << "Resultant matrix:" << endl;


displayMatrix(result, m, n);

return 0;
}

9. Write a program to multiply array A and B of order NxL and LxM

#include <iostream>
using namespace std;

const int MAX_SIZE = 100;

void inputMatrix(int matrix[][MAX_SIZE], int m, int n) {


cout << "Enter matrix elements:" << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
}

TIZITA TESFAYE 10 04569/14


: DATA STRUCTURE AND ALGORITHM

void multiplyMatrix(int matrixA[][MAX_SIZE], int matrixB[][MAX_SIZE], int


result[][MAX_SIZE], int m, int l, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
result[i][j] = 0;
for (int k = 0; k < l; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
}

void displayMatrix(int matrix[][MAX_SIZE], int m, int n) {


cout << "Matrix elements:" << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

int main() {
int matrixA[MAX_SIZE][MAX_SIZE];
int matrixB[MAX_SIZE][MAX_SIZE];
int result[MAX_SIZE][MAX_SIZE];
int m, l, n;

cout << "Enter number of rows and columns for matrix A (m and l): ";
cin >> m >> l;

cout << "Enter number of rows and columns for matrix B (l and n): ";
cin >> l >> n;

if (l != l) {
cout << "Invalid input. Number of columns in matrix A should be equal to
number of rows in matrix B." << endl;
return 0;
}

inputMatrix(matrixA, m, l);
inputMatrix(matrixB, l, n);

multiplyMatrix(matrixA, matrixB, result, m, l, n);

cout << "Resultant matrix:" << endl;


displayMatrix(result, m, n);

TIZITA TESFAYE 11 04569/14


: DATA STRUCTURE AND ALGORITHM
return 0;
}

TIZITA TESFAYE 12 04569/14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy