Глашечка, наверное так:
| Цитата | Создать динамический одномерный массив целых чисел. Вычислить сумму целых отрицательных элементов
|
Если да, то вот решение:
| Код | #include <iostream> #include <cstdlib> #include <ctime>
using namespace std;
//----------------------------------------------// // Заполняем массив случайными числами void Random(int* theArray, unsigned theSize) { for (unsigned i = 0; i < theSize; ++i) { theArray[i] = ::rand() % 100 - 50; } } //----------------------------------------------// // Вывод на экран элементов массива void Print(int* theArray, unsigned theSize) { for (unsigned i = 0; i < theSize; ++i) { cout << theArray[i] << " "; } cout << endl; } //----------------------------------------------// // Сумма отрицтельных элементов int Amount(int* theArray, unsigned theSize) { int result = 0; for (unsigned i = 0; i < theSize; ++i) { if (theArray[i] < 0) { result += theArray[i]; } } return result; } //----------------------------------------------//
int main() { // Для генераци случайных чисел ::srand(::time(NULL));
unsigned count = 0; cout << "Enter the number of elements in the array: "; cin >> count;
int* array = new int[count]; Random(array, count); Print(array, count); cout << "Amount of negative numbers = " << Amount(array, count) << endl; delete [] array; return 0; }
|
http://codepad.org/xBFgDZpT
Бинарник с исходником ниже |