C++ Functions using Vectors of Vectors! (tables)
![]()
Example program that creates a table, and then uses functions to reverse the ones and zeres and to provide the total table count
// example program using vectors of vectors
// this program creates a vectors of vectors (a table)
// and then fills the table with ones or zeros,
// based on a user specified probability
#include <iostream>
#include <vector>
using namespace std;
// function declarations
// this function returns a table that has been reversed in value
vector<vector<int> > reverseT(vector<vector<int> > table);
// this function returns the count of the living cells
int tableCount(vector<vector<int> > table);
int main ()
{
cout << endl;
cout << "This program creates a vector of vectors (called a table) " << endl;
cout << "and fills each element with either a zero or one, based on" << endl;
cout << "a user-specified probability." << endl << endl;
cout << "Please input size of list to be input: ";
int sizeV;
cin >> sizeV;
cout << endl;
// now declare vector of appropriate size
vector<vector<int> > table(sizeV, sizeV);
// ask for probability of having a one in each element
cout << "Please input the probability of having a one in ";
cout << "each element: ";
double probONE;
cin >> probONE;
cout << endl;
// error checking to weed out those too ignorant to know what a probability is
// using a loop that will continue until the user gets it right!
while ( (probONE < 0) or (probONE > 1) )
{ cout << "The probability of something must be between zero and one. Please try again.";
cout << endl << endl;
cout << "Please input the probability of having a one in";
cout << "each element: ";
cin >> probONE;
}
// now fill the table with ones and zeros
// I will use nested whiles because we haven't gotten to "for" loops yet
int i = 0;
int j = 0;
while (i < sizeV)
{
j = 0;
while (j < sizeV)
{
if (probONE > static_cast<double>(rand())/RAND_MAX )
{table [i] [j] = 1;}
else
{table [i] [j] = 0;}
j=j+1;
}
i=i+1;
}
// need to output the table to the screen
i = 0; // remember not to redeclare these counters, just zero them out again
j = 0;
while (i < sizeV)
{
j = 0;
while (j < sizeV)
{
cout << table [i] [j] << " ";
j=j+1;
}
cout << endl;
i=i+1;
}
cout << endl; // carriage returns to make the screen look pretty
// function call here
cout << "There are currently " << tableCount(table) << " lives." << endl;
//function call here
table = reverseT(table);
// print out again
cout << endl << endl << "The reversed table looks like:" << endl;
i = 0; // remember not to redeclare these counters, just zero them out again
j = 0;
while (i < sizeV)
{
j = 0;
while (j < sizeV)
{
cout << table [i] [j] << " ";
j=j+1;
}
cout << endl;
i=i+1;
}
cout << endl; // carriage returns to make the screen look pretty
// function call here
cout << "Now there are " << tableCount(table) << " lives." << endl;
return 0;
}
///////////////////////////////////////////////////////////
// this function returns a table that has been reversed in value
vector<vector<int> > reverseT(vector<vector<int> > table)
{
vector<vector<int> > newTable(table.size(), table[0].size());
for (int i=0; i<table.size(); i=i+1)
{for (int j=0; j<table[0].size(); j=j+1)
{if (table[i] [j] == 1)
{newTable[i] [j] = 0;}
else
{newTable[i] [j] = 1;}
}
}
return newTable;
}
///////////////////////////////////////////////////////////
// this function returns the count of the living cells
int tableCount(vector<vector<int> > table)
{
int sum = 0;
for (int i=0; i<table.size(); i=i+1)
{for (int j=0; j<table[0].size(); j=j+1)
{sum = sum + table[i] [j];
}
}
return sum;
}