int main() { int n = 10, s = 0, i; int a[n]; srand(time(NULL)); cout<<"array:"<<endl; for (i=0; i<n; i++) { a[i]=rand() % 40-20; cout<<a[i]<<" "; } cout<<endl; i = 0; while ((i<n) && (a[i] != 0)) { s += a[i]; i++; } if (i == n) cout << "В массиве нет элементов = 0"; else cout << "s = " << s; system("pause"); return(0); }
Answers & Comments
using namespace std;
int main() {
int n = 10, s = 0, i;
int a[n];
srand(time(NULL));
cout<<"array:"<<endl;
for (i=0; i<n; i++)
{
a[i]=rand() % 40-20;
cout<<a[i]<<" ";
}
cout<<endl;
i = 0;
while ((i<n) && (a[i] != 0)) {
s += a[i];
i++;
}
if (i == n) cout << "В массиве нет элементов = 0";
else cout << "s = " << s;
system("pause");
return(0);
}
Пример:
12 -9 17 15 0 13 -10 19 18 15
s = 35
Verified answer
#include <iostream>#include <array>
#include <numeric>
using namespace std;
const size_t N = 10;
int main()
{
array<int, N> a = { 1, 5, 9, 6, 3, 0, 1, 4, -2, 4 };
cout << accumulate(a.cbegin(), find(a.cbegin(), a.cend(), 0), 0);
}
интересно на паскале короче бы вышло?