cpp.vectors.1: Filling and Shuffling a C++ Vector

In this tutorial, we will learn how to fill a vector, and then randomly shuffle the numbers contained in that vector.

Learning objectives:

  • Explain the srand function in the C standard library.
  • Explain the rand function in the C standard library.
  • Explain the difference between random and pseudo-random.
  • Build a C++ vector program that incorporates pseudo-random numbers.

cpp.vectors.1: Filling and Shuffling a C++... by Djere University

The LibreOffice Impress-generated HTML slideshow for this lecture is here:
http://djere.com/lectures/node21lecture/Filling_and_Shuffling_a_Cplusplu...

***The source code for this tutorial's computer program begins directly below:***

// cpp.vectors.1.cpp
// cpp.vectors.1: Filling a C++ Vector and Randomly Shuffling the Numbers
// written by Rex Djere

// This programs asks the user for a single integer number and the size of their vector.
// It then fills the vector with a series of increasing integers based on the number entered.
// For example, if the user enters a first number of 1and a vector size of 5
// the program would fill the vector with 1,2,3,4,and 5.
// The program then randomly shuffles the numbers in vector
// and prints the output.

// This source code and the associated tutorial will be maintained at
// http://djere.com/node/21

#include <iostream> // needed for input and output e.g. from keyboard and to screen
#include <vector> // needed for vector functions
#include <algorithm> // needed for the srand and random_shuffle functions
#include <ctime> // we need system time to seed random number generator
# include <iomanip> // we need this for the setw function to set column width

using namespace std;

// function prototypes for print functions
void printVectorHeader(int);
void printVector(vector<int> &, int, int);


int main()
{
   srand(time(0)); // seeds the random number generator
   vector<int> myVector(0); // creates a vector of integers named myVector with intial size of 0
   int number, vectorSize; // stores number input by user and vector size
   int i,j; // counter variables
   int rowCount, vectorColumns;  // counts number of rows in vector, number of columns in vector

   cout<< "Enter the size of your vector: ";
   cin >> vectorSize; // stores vector size
   cout<< "Enter the first number: ";
   cin >> number; // inputs number entered by user

   if (vectorSize>1 && vectorSize<12) // if vector size is 1, then we obviously only need one column
   // is vector size is between 2 and 11, column size is limited to vector size
   {
      cout << "How many columns would you like to have in your vector printout " << endl;
      cout << "(Enter a number between 1 and " << vectorSize << " )? ";
      cin >> vectorColumns; // input number of columns
   }

   else if (vectorSize>=12) // if vector size is > or equal to 12, the number of columns is limited to 12 by display terminal characteristics
   {
      cout << "How many columns would you like to have in your vector printout " << endl;
      cout << "(Enter a number between 1 and 12)? ";
      cin >> vectorColumns;
   }

   else vectorColumns = 2;

   for( i=0; i<vectorSize; i++)
   {
       myVector.push_back(number+i); // this sequentially fills the vector with a series of integers above the first number entered by the user
   }

    // prints the vector before random shuffle
    cout << endl << "Here is your vector before the random shuffle: " << endl;
    printVectorHeader(vectorColumns);
   printVector( myVector, vectorSize, vectorColumns);
  cout << endl;

  // randomly shuffles the vector
  random_shuffle(myVector.begin(), myVector.end());


  // prints the vector after the shuffle
  cout << endl << "Here is your vector after the random shuffle: " << endl;
  printVectorHeader(vectorColumns);
  printVector(myVector,vectorSize, vectorColumns);
  cout << endl;

  return 0;
}

// full definition of printVectorHeader function
// prints the header for the vector
void printVectorHeader(int columns)
{
   int j; // counter
   cout << setw(10) << "       "; // spacing

  // header top line
  for (j=0; j<columns; j++)
  {
      cout << "_____";
  }

  // header "columns" row
   cout << endl << setw(7) << "column:";

  for (j=0; j<columns; j++)
  {
      cout << setw(5) << j + 1;

  }

  // spacing
  cout << endl;
  cout << setw(10) << "       ";


  // header bottom line
  for (j=0; j<columns; j++)
  {
      cout << "_____";

  }
  cout << endl; // spacing
}


// full definition of printVector function
// prints the vector
void printVector(vector<int> & vector, int size, int columns)
{
   int k;
   cout << setw(7) << "       "; // spacing
   int rowCount =0;
    for (k=0; k<size; k++)
  {

      cout << setw(5) << vector.at(k); // spaces and prints vector values
      if(k%((rowCount+1)*(columns-1) + rowCount) == 0 && k > 0)
      {
          rowCount++;
          cout << endl;
          cout << setw(7) << "       "; // spacing
      }
  }
}

***The source code for this tutorial's program ended directly above.***

The Compiled Output of cpp.vectors.1.cpp on a Fedora 17 GNU/Linux System:


The Compiled Output of cpp.vectors.1.cpp on a Windows 7 System: