Skip to content

Instantly share code, notes, and snippets.

@stanzheng
Last active Dec 20, 2015
Embed
What would you like to do?
Multiplication table using vectors with C++
// multiplication table
// enter two dimensions and the factor
// 3 3 would be a 3x4 grid multiplication grid
/*
1 2 3 4
2 4 6 8
3 6 9 12
*/
//lets do it with a vector
// the second factor would be the delimiter
//
#include <cstdlib>
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int main(){
int row=0, column=0, sum=0;
vector <int> table;
cout << "Enter Row and column: \n";
cin >> row >> column;
cout << "Multiplication Tables for " << row << " by " << column << "\n";
for (int i = 1; column >= i; ++i)
for (int j = 1; row >= j; ++j){
sum = i * j;
table.push_back(sum);
}
int iter =0;
for( std::vector<int>::const_iterator i = table.begin(); i != table.end(); ++i)
{
if ((iter % column)==0)
cout << "\n";
std::cout << setw(5) <<*i << ' ';
iter++;
}
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment