Computer Science Canada

Help on dynamic arrays

Author:  omni [ Wed Nov 10, 2004 6:10 pm ]
Post subject:  Help on dynamic arrays

I want to make a program that will just calculate an average of numbers. It will receive input from a file. I want my program to be able to accept a variable amount of numbers.
I'm thinking of using dynamic memory for this.

(pseudocode)
Open file stream
create array
set SUM=0
set COUNTER=0
Loop
receive input from file
move input into array
SUM=SUM + input from the array
increase counter
allocate more memory for the next number
end loop
AVERAGE=SUM / COUNTER
(END)

Is this logic right ?

Author:  wtd [ Wed Nov 10, 2004 6:11 pm ]
Post subject: 

Storing the numbers in an array would be more flexible, but isn't necessary for this problem.

Each time you read a number, add it to a total, then increment a counter by 1. At the end, divide the total by the counter.

Author:  omni [ Wed Nov 10, 2004 7:28 pm ]
Post subject: 

LMAO, thank you wtd. I don't know what I was thinking.
What if I want to keep the numbers in the array for later use?

Author:  wtd [ Wed Nov 10, 2004 7:53 pm ]
Post subject: 

If we're talking about C++, then don't use arrays. Instead, use a vector.

code:
#include <vector>
#include <iostream>

int main()
{
   std::vector<int> input_numbers;
   bool cont(true);
   int total(0);

   while (cont)
   {
      int input;
      char response('n');
      std::cin >> input;
      input_numbers.push_back(input);
      std::cout << "Continue?" << std::endl;
      std::cin >> response;
      cont = response == 'y' || response == 'Y';
   }

   for (std::vector<int>::iterator iter(input_numbers.begin()); iter != input_numbers.end(); iter++)
   {
      total += *iter;
   }

   int average(total / input_numbers.size());

   std::cout << "Sum: " << total << " Average: " << average << std::endl;

   return 0;
}


Or wrap up the sum and average functionality by subclassing vector.

code:
#include <vector>
#include <iostream>

class NumberCollection : public std::vector<int>
{
   public:
      NumberCollection() : std::vector<int>() { }
      NumberCollection(size_t s) : std::vector<int>(s) { }

      int sum()
      {
         int total(0);
         for (iterator iter(begin()); iter != end(); iter++)
         {
            total += *iter;
         }
         return total;
      }

      int average()
      {
         return sum() / size();
      }
};

int main()
{
   NumberCollection input_numbers;
   bool cont(true);

   while (cont)
   {
      int input;
      char response('n');
      std::cin >> input;
      input_numbers.push_back(input);
      std::cout << "Continue?" << std::endl;
      std::cin >> response;
      cont = response == 'y' || response == 'Y';
   }

   std::cout << "Sum: " << input_numbers.sum() << " Average: " << input_numbers.average() << std::endl;

   return 0;
}

Author:  omni [ Wed Nov 10, 2004 8:18 pm ]
Post subject: 

Ow my head hurts now. Gotta learn vectors now. *shudders* reminds me fricking physics class which i'm failing.


: