Bojan Nikolic: Numerical and Quantitative Methods in C++

[website home] | [BN Algorithms]

Taking input at the run time

In previous examples of the mean program (see for example the previous article) the inputs to our calculation were specified together with the program. In order compute the mean of a different set of numbers it was necessary to re-compile the program. This of course is cumbersome and unnecessary; here is the simplest way of specifying the inputs at run-time.

Improved program: mean_v6

Here is the improved version of the program:

// mean_v6.cpp
// Bojan Nikolic <bojan@bnikolic.co.uk>
// Proper input

#include <iostream>
#include <boost/format.hpp>

double csum(const double * data,
        size_t n)
{
  double sum =0;
  for (size_t i =0 ; i < n ; ++i )
  {
    sum += data[i];
  }
  return sum;
}

double cmean( const double * data,
          size_t n)
{
  return (csum(data,n)/n);
}

void readInput(double * data ,
           const size_t n)
{
  for (size_t i =0 ; i < n; ++i)
  {
    std::cin>>data[i];
  }
}

int main(void)
{
  const size_t n = 5;
  double data[n];

  readInput(data,n);

  std::cout<<
    boost::format("The sum is: %.2f and the mean is: %.2f")
    % csum(data,n) % cmean(data, n)
    <<std::endl;
}

The main change to the program is the new function named readInput:

void readInput(double * data ,
           const size_t n)
{
  for (size_t i =0 ; i < n; ++i)
  {
    std::cin>>data[i];
  }
}

This function takes two arguments: a pointer to double-precision floating point number (double * data), and the number of elements that should be read in (const size_t n). As before, we will make use of the ambiguity between pointers to single elements and arrays. That is, nominally double * data points to a floating pointing number; however, by for example writing data[1] it is possible to access the next floating number in memory, and so on, allowing access to elements of arrays.

Note that, in contrast to the functions csum and cmean, the parameter data is declared without the const keyword. The reason is that we wish to store the numbers read from the user into this array and so need to be able to modify it. In general, when pointers are passed without const this indicates that (some of) the outputs of a function will be placed in the object pointed to.

The novel construct in the function readInput is the expression which actually reads in the inputs from the user:

std::cin>>data[i];

This expression reads in a number from the standard input, normally this is just what the user types in, and stores it in the i-th element of the array data. Like std::cout, the object std::cin is defined by C++ standard library in the header <iostream>. It defines the operator >> to stream in data from the console.

If you try out this version of the program you will find it works as expected, although it does have quirks. For example, if you type something other than numbers, it will not process any further data. We will see in the next article how to correct this behaviour.

Summary

You should normally try to separate as much as possible the algorithm from the data that the algorithm processes. Normally, it is very convenient to read the data in at program run time. The simplest way of achieving this is by using the std::cin object from <iostream> part of the standard library.