Hi, I just started a C++ class and I'm already having troubles with some parts. The instructions are as follows:

"Create a C++ console applications to compile the following statistics on a list of integers:
• minimum value;
• maximum value;
• median value;
• arithmetic mean;
• variance (population variance);
• standard deviation (population standard deviation);
• mode (including multi-modal lists).
• frequency distribution (fixed to 10 evenly distributed intervals);
• histogram (of the frequency distribution);

Your program must handle any length of list. The list will be piped in from the command line and terminated with end-of-stream (^Z). "

Now I assumed I would have to order the ints into a vector in order to calculate the different stats, but I'm having trouble getting started. I'm not sure how to fill a vector if I don't know in advance how many numbers there will be. If some1 could help me get started with filling the vector with user input I would be most grateful.

Recommended Answers

All 2 Replies

You'll get best help if you show some code (using the CODE button to keep indentation, etc).

However, just for starters, a [URL="http://www.cplusplus.com/reference/stl/vector/"]vector[/URL] can grow as large as you need, so you just create one and [icode]push_back[/icode] each new value in turn. There are some efficiency issues with resizing a vector, but for a homework assignment, they aren't important

The list will be piped in from the command line and terminated with end-of-stream (^Z).

This means that you can simply do it like this:

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> my_vector;

    int temp;

    while (std::cin >> temp)
        my_vector.push_back(temp);

    //...
}

When you reach the end-of-stream (CTRL-Z), std::cin >> temp will evaluate to false and the loop will break.

EDIT: Oh, I thought giving out such a small piece of code wouldn't hurt...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.