Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Центр помощи > [C++] Создать одномерный массив [?]


Автор: Глашечка 16.11.2011, 10:47
Помогите , пожалуйста, выплнить задание

Создать в динамической (...непонятное слово...) одномерный массив целых чисел. Вычислить сумму целых отрицательных элементов

Автор: t_gran 16.11.2011, 18:37
Глашечка, наверное так:
Цитата

Создать динамический одномерный массив целых чисел. Вычислить сумму целых отрицательных элементов 


Если да, то вот решение:
Код

#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

Бинарник с исходником ниже

Автор: Глашечка 16.11.2011, 20:21
Спасибо большое-пребольшое!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)