@Nameless One
5781 / 3430 / 255
Регистрация: 08.02.2010
Сообщений: 7,448
|
25.03.2010, 18:48
|
|
C++ | 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
| #include <iostream>
#include <ctime>
#include <cmath>
#include <limits>
//Вычисление суммы и количества элементов
template<class T>
T sum(T* begin, T* end, T lim, size_t& count)
{
T result=0;
count=0;
while(begin!=end)
if(fabs(*begin++)<=lim)
{
result+=*(begin-1);
count++;
}
return result;
}
int main()
{
srand(static_cast<size_t>(time(NULL)));
const size_t size=12;
double lin_table[size], a;
//Пределы, в которых лежать значения элементов
//lin_table[i] принимает значения из отрезка [l_lim; r_lim]
double l_lim, r_lim;
do
{
std::cout << "Input the left limit of table values: ";
std::cin >> l_lim;
std::cout << "Input the right limit of table values: ";
std::cin >> r_lim;
}
while(l_lim>=r_lim);
std::cout << "Input a: ";
std::cin >> a;
size_t count=0;
//Заполнение таблицы случ. значениями и их вывод
for(size_t i=0; i<size; i++)
{
lin_table[i]=((double)rand()/(double)RAND_MAX*(r_lim-l_lim))+l_lim;
std::cout << lin_table[i] << std::endl;
}
//Подсчет суммы и количества, вывод результатов
std::cout << "===========" << std::endl;
std::cout << "Sum = " << sum(lin_table, lin_table+size, a, count) << std::endl
<< "Count = " << count << std::endl;
system("pause");
return 0;
} |
|
1
|