Is there a more efficient way?

I'm relatively new to c++(barely understand what I'm doing). but I tried to make a table. This however, needs some work. I basically made a user input and multiplied everything by eachother. If any of you have time could you tell me a faster or more efficient way of doing this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main() {
  int x, xx, xxx;
  int y, yy, yyy;
  int xy, xxy, xxxy, xyy, xxyy, xxxyy, xyyy, xxyyy, xxxyyy;
	
  cout << "The first 3 numbers are: ";
  cin >> x >> xx >> xxx;
  cout << "The second 3 numbers are: ";
  cin >> y >> yy >> yyy;

  xy = x * y;
  xxy = 2 * x * y;
  xxxy = 3 * x * y;
  xyy = 2 * x * y;
  xxyy = 2 * 2 * x * y;
  xxxyy = 3 * 2 * x * y;
  xyyy = 3 * x * y;
  xxyyy = 2 * 3 * x * y;
  xxxyyy = 3 * 3 * x * y;

  cout << " x  " << x << "  " << xx << "  " << xxx << "\n";
  cout << " " << y << "  " << xy << "  " << xxy << "  " << xxxy << "\n";
  cout << " " << yy << "  " << xyy << "  " << xxyy << "  " << xxxyy << "\n";
  cout << " " << yyy << "  " << xyyy << "  " << xxyyy << "  " << xxxyyy <<"\n";

  return 0;
}
You should look into arrays. In looks like you need 1- and 2-dimensional arrays.

Look into arrays to get the idea of how they are accessed and how you can loop over them. Later you may learn how do represent a 2-dimensional array as a 1-dimensional array. This frequently helps with performance.

After you get an understanding of them, then look into std::vector. It is like an array, but is much safer.

Others may think you should skip arrays and go straight to std::vector, but I think for the purpose of understanding how array-type memory is accessed via loops, using a raw array removes some level of complexity.
Yes, Thx a lot 🙏🏻
reuse what you did already:
xxy = 2*xy;
etc...
some compilers can change like 3*3 to 9 and some are less intelligent. I prefer to do it myself so I know it was done, though.
xxxyyy = 9*xy; //one multiplication instead of 4.

side note on style: I can't make sense of what you are doing but the name xxxyyy to me implies x*x*x*y*y*y not 9*xy. Its ok if it makes sense to you and your peers, but to me, the name is very misleading.
Last edited on
Topic archived. No new replies allowed.