-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.cpp
159 lines (143 loc) · 3.08 KB
/
array.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
using namespace std;
int linearSearch(int arr[], int size, int target)
{
for (int i = 0; i < size; i++)
{
if (arr[i] == target)
{
return i;
}
}
return -1;
}
void reverseArray(int arr[], int size)
{
int start = 0;
int end = size - 1;
while (start < end)
{
swap(arr[start], arr[end]);
start++;
end--;
}
cout << "Reversed Array: ";
for (int i = 0; i < size; i++)
{
cout << arr[i];
if (i < size - 1)
{
cout << ", ";
}
}
cout << endl;
}
int sum(int array[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += array[i];
}
return sum;
}
int product(int array[], int size)
{
int pro = 1;
for (int i = 0; i < size; i++)
{
pro *= array[i];
}
return pro;
}
int findMax(int arr[], int size)
{
int max = 0;
for (int i = 1; i < size; i++)
{
if (arr[max] < arr[i])
{
max = i;
}
}
return max;
}
int findMin(int arr[], int size)
{
int min = 0;
for (int i = 1; i < size; i++)
{
if (arr[min] > arr[i])
{
min = i;
}
}
return min;
}
void swapMinMax(int arr[], int size)
{
int maxIndex = findMax(arr, size);
int minIndex = findMin(arr, size);
swap(arr[maxIndex], arr[minIndex]);
cout << "\n Array after swapping min and max: ";
for (int i = 0; i < size; i++)
{
cout << arr[i];
if (i < size - 1)
{
cout << ", ";
}
}
cout << endl;
}
void uniqueVal(int arr[], int size)
{
cout << "Unique value of Array:";
for (int i = 0; i < size; i++)
{
bool isUnique = true;
for (int j = 0; j < size; j++)
{
if (i != j && arr[i] == arr[j])
{
isUnique = false;
break;
}
}
if (isUnique)
{
cout << arr[i] << " ";
}
}
}
void intersectionOfTwoArrays(int arr1[], int arr2[], int size1, int size2)
{
cout << "Intersection of Two Arrays:";
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
if (arr1[i] == arr2[j])
{
cout << arr1[i] << " ";
}
}
}
}
int main()
{
int arr[5] = {2, 5, 5, 13, 17};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 90;
int result = linearSearch(arr, size, target);
cout << "Linear Search Result: " << result << endl;
// reverseArray(arr, size);
cout << "Sum of all numbers in the array: " << sum(arr, size) << endl;
cout << "Product of all numbers in the array: " << product(arr, size) << endl;
uniqueVal(arr, size);
swapMinMax(arr, size);
int a1[4] = {1, 2, 3, 4};
int a2[3] = {2, 3, 4};
intersectionOfTwoArrays(a1, a2, 4, 3);
return 0;
}