So the latest challenge from jumping into c++ is as following.
Code:
Write a function that builds the multiplication table of arbitrary dimensions
This chapter also talks a ton about 2d arrays.
So I've built my program thus far as this.
Code:
#include <iostream>
using namespace std;
int drawTable(int,int);
int main()
{
drawTable(5,5);
}
/*
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16
*/
int drawTable(int x, int y)
{
int table[x][y];
int xMax = x;
int yMax = y;
int xSlider = 0;
int ySlider = 0;
bool loop = true;
while(loop == true)
{
table[xSlider][ySlider] = xSlider * ySlider;
if(xSlider < xMax && ySlider == yMax)
{
loop == false;
}
if(ySlider < yMax)
{
ySlider++;
}
else if(ySlider == yMax)
{
xSlider++;
ySlider = 0;
}
cout << table[xSlider][ySlider];
cin.get();
}
return 0;
}
So basically, the idea is that I can use the arrays dimensions as a placeholder, and just multiple them to get that specific spot, so table[0][0] = 0, [0][1] = 0 and so on. However the output actually seems to be randomly generated numbers so I'd like to ask, what am I doing wrong? Am I on the right track? Or have I missed the bus stop completely.
Thanks.