-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq9.cpp
More file actions
93 lines (80 loc) · 2.67 KB
/
Copy pathq9.cpp
File metadata and controls
93 lines (80 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
a) Write a function int** AllocateMemory(int& rows, int& cols) that takes size of matrix (rows and columns) from user, allocates memory for the matrix and return its pointer.
b) Write a function void InputMatrix(int** matrix, const int rows, const int cols) which takes input the values in matrix from user (console).
c) Write a function void DisplayMatrix(int** matrix, const int& rows, const int& cols) that displays the matrix in proper format.
d) Write a function called maxCol that takes as parameters a pointer to a 2D array and its dimensions. It should return the largest element in each column of the array. Since there is more than one column in 2D array, you have to return a dynamic array that contains largest of each column.
e) Write a function void DeallocateMemory(int** matrix, const int& rows) that deallocates all the memory.
*/
#include<iostream>
using namespace std;
//function to allocate memory to matrix
int** AllocateMemory(int& rows, int& cols) {
int** matrix = new int* [rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i] = new int[cols];
}
}
return matrix;
}
//function to take input matrix
void InputMatrix(int** matrix, const int rows, const int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix[i][j];
}
}
}
//function to display matrix
void DisplayMatrix(int** matrix, const int rows, const int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
//function to compute maximum values of each column
int* MaxCol(int** matrix, const int rows, const int cols) {
int* maxArr = new int[cols];
int maxVal;
for (int j = 0; j < cols; j++) {
maxVal = matrix[0][j];
for (int i = 1; i < rows; i++) {
if (maxVal < matrix[i][j]) {
maxVal = matrix[i][j];
}
}
maxArr[j] = maxVal;
}
return maxArr;
}
//function to deallocate memory
void DeallocateMemory(int** matrix, const int& rows) {
for (int i = 0; i < rows; i++) {
delete[]matrix[i];
}
}
int main()
{
int rows, cols;
cout << "Enter Rows: ";
cin >> rows;
cout << "Enter Columns: ";
cin >> cols;
int** matrix = AllocateMemory(rows, cols);
cout << "Input Matrix:" << endl;
InputMatrix(matrix, rows, cols);
cout << "Display Matrix:" << endl;
DisplayMatrix(matrix, rows, cols);
cout << "\nMaximum Column Values: ";
int* MaxColValue = MaxCol(matrix, rows, cols);
for (int i = 0; i < cols; i++) {
cout << *(MaxColValue + i) << ",";
}
cout << endl << endl;
DeallocateMemory(matrix, rows);
delete[]MaxColValue;
system("pause");
return 0;
}