Другие языки программирования и технологии
помогите с C++
в одномерном массиве состоящем из n вещественных элементов вычислить сумму элементов между первым и последним нулевыми элементами
Ну и чем помогать? Алгоритмом?
Или делать за тебя?
Алгоритм то можно хоть немного описать, но код делать влом, ночь однако
Или делать за тебя?
Алгоритм то можно хоть немного описать, но код делать влом, ночь однако
http://www.cplusplus.com/reference/algorithm/find/
http://www.cplusplus.com/reference/numeric/accumulate/
#include < iostream >
#include < numeric >
#include < algorithm >
using namespace std;
int main()
{
const size_t n = 6;
float a[n]={1.1, 0, 2.1, 3.1, 0, 1.5},
*it1, *it2,
sum = 0;
it1 = find(a, a + n, 0);
if (it1 != a + n)
if ((it2 = find(++it1, a + n, 0)) != a + n) {
cout << "Sum:" << accumulate(it1, it2, sum) << endl;
return 0;
}
cout << "Zero segment not found" << endl;
return -1;
}
http://www.cplusplus.com/reference/numeric/accumulate/
#include < iostream >
#include < numeric >
#include < algorithm >
using namespace std;
int main()
{
const size_t n = 6;
float a[n]={1.1, 0, 2.1, 3.1, 0, 1.5},
*it1, *it2,
sum = 0;
it1 = find(a, a + n, 0);
if (it1 != a + n)
if ((it2 = find(++it1, a + n, 0)) != a + n) {
cout << "Sum:" << accumulate(it1, it2, sum) << endl;
return 0;
}
cout << "Zero segment not found" << endl;
return -1;
}
#include "iostream"
#include "vector"
#include "algorithm"
#include "cstdlib"
#include "time.h"
using namespace std;
int main() {
vector < int > array(50);
vector < int >::iterator it;
srand(time(NULL));
for (it = array.begin(); it != array.end(); ++it) {
*it = rand() % 10; cout << *it << " ";
}
vector::reverse_iterator rit;
rit = find(array.rbegin(), array.rend(), 0);
if (rit == array.rend()) {
cout << "array has no zeros";
cin.ignore(255, '\n').get();
return 1;
}
int rev_numb;
rev_numb = array.rend() - rit - 1;
int sum = 0;
for (it = find(array.begin(), array.end(), 0); it != array.begin() + rev_numb; ++it) {
sum += *it;
}
cout << endl << "sum (from 1st to last zero) = " << sum;
cin.ignore(255, '\n').get();
return 0;
}
#include "vector"
#include "algorithm"
#include "cstdlib"
#include "time.h"
using namespace std;
int main() {
vector < int > array(50);
vector < int >::iterator it;
srand(time(NULL));
for (it = array.begin(); it != array.end(); ++it) {
*it = rand() % 10; cout << *it << " ";
}
vector::reverse_iterator rit;
rit = find(array.rbegin(), array.rend(), 0);
if (rit == array.rend()) {
cout << "array has no zeros";
cin.ignore(255, '\n').get();
return 1;
}
int rev_numb;
rev_numb = array.rend() - rit - 1;
int sum = 0;
for (it = find(array.begin(), array.end(), 0); it != array.begin() + rev_numb; ++it) {
sum += *it;
}
cout << endl << "sum (from 1st to last zero) = " << sum;
cin.ignore(255, '\n').get();
return 0;
}
Похожие вопросы
- Помогите срочно C#
- Люди, помогите скачать C++!!
- помогите с C++
- помогите, пожалуйста, c делфи!
- помогите с C програм не выводит результаты
- Помогите Диск C сам заполняеться вот например выйду и диска памать 6гб опять зайду уже 5.65гб и вотак до 1гб помогите
- помогите массив c#.
- Помогите по C#
- Помогите с C++
- Помогите с C++