-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble Sort Algorithm, Array Operations.cpp
More file actions
78 lines (68 loc) · 2 KB
/
Bubble Sort Algorithm, Array Operations.cpp
File metadata and controls
78 lines (68 loc) · 2 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
#include<iostream>
using namespace std;
// Class for performing operations on an array
class ArrayOperations {
private:
int arr[20], n; // Private member variables
public:
void input() {
cout << "\nEnter the value for n: ";
cin >> n;
}
void getData();
void performOperations();
void sortArray();
void output();
};
void ArrayOperations::getData() {
for (int i = 0; i < n; i++) {
cout << "\nEnter the " << i << "th element: ";
cin >> arr[i];
}
}
void ArrayOperations::performOperations() {
int max, min, sum;
max = min = sum = arr[0];
// Finding maximum, minimum, and sum of elements
for(int i = 1; i < n; i++) {
if (max < arr[i])
max = arr[i];
if (min > arr[i])
min = arr[i];
sum += arr[i];
}
float avg = sum / n; // Calculating average
cout << "\nMaximum value is: " << max
<< "\nMinimum value is: " << min
<< "\nSum of elements: " << sum
<< "\nAverage value is: " << avg;
}
void ArrayOperations::sortArray() {
int i, j, temp;
// Bubble sort algorithm to sort the array in ascending order
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void ArrayOperations::output() {
cout << "\nThe array elements are as follows:\n";
// Displaying the elements of the array
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
int main() {
ArrayOperations obj;
obj.input(); // Taking input for the size of the array
obj.getData(); // Taking input for the elements of the array
obj.performOperations(); // Performing operations on the array
obj.sortArray(); // Sorting the array
obj.output(); // Displaying the array elements
return 0;
}