-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindAmplitudeOfArray.cpp
More file actions
51 lines (50 loc) · 1.27 KB
/
FindAmplitudeOfArray.cpp
File metadata and controls
51 lines (50 loc) · 1.27 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
#include <iostream>
#include <valarray>
#include <stdlib.h>
#include <math.h>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
{
int nElems;
std::cout << "Input a count of elements in array\r\n";
std::cin >> nElems;
if (nElems == 0)
{
std::cout << "Empty arrays not allowed\r\n";
std::cin.ignore();
std::cin.get();
return 1;
}
std::valarray<int> iNumbers(nElems);
srand(time(nullptr));
std::cout << "Generating array by filling " << nElems << " elements\r\n";
for (int i = 0; i < nElems; i++)
{
iNumbers[i] = 1 + rand() % 100;
std::cout << iNumbers[i] << " ";
}
int iMax = iNumbers[0];
int nMaxIndex = 0;
int iMin = iNumbers[0];
int nMinIndex = 0;
for (int i = 1; i < nElems; i++)
{
if (iNumbers[i] > iMax)
{
iMax = iNumbers[i];
nMaxIndex = i;
}
if (iNumbers[i] < iMin)
{
iMin = iNumbers[i];
nMinIndex = i;
}
}
std::cout << "\r\nFound maximum element: " << iMax << " with index "
<< nMaxIndex << "\r\n";
std::cout << "Found minimum element: " << iMin << " with index "
<< nMinIndex << "\r\n";
std::cout << "Found amplitude:" << iMax - iMin << "\r\n";
std::cin.ignore();
std::cin.get();
}