Having problems with my code

i've been having trouble trying to build a multiplication table because it cuts some numbers (compile the code and you'll see)


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
#include <iostream>
#include <iomanip>
using namespace std;

int main(){

int yaxis[10][1] = {{1},{2},{3},{4},{5},{6},{7},{8},{9}};
int xaxis[1][10] = {{0,1,2,3,4,5,6,7,8,9}};

cout << "    ";
for(int j = 1; j < 11; j++){
cout<< setw(4) << xaxis[1][j];
}

cout << endl;

for(int x = 1; x < 9; x++){
cout << setw(4) << x + 2;
    for(int i = 1; i < 11; i++){
    cout << setw(4) << yaxis[x][1] * xaxis[1][i] ;
        }
    cout << endl;
    }
    
system("pause");
    return 0;
}


If any of you have any suggestions on how to build a nice looking tale please let me know.
arrays are zero-based.
so this is okay, like you've done:
 
int xaxis[1][10] = {{0,1,2,3,4,5,6,7,8,9}};


but to access the first element you need to use zero.

So instead of:
1
2
3
for(int j = 1; j < 11; j++){
cout<< setw(4) << xaxis[1][j];
}

something like this:
1
2
3
	for (int j = 0; j < 10; j++){
		cout << setw(4) << xaxis[0][j];
	}


and the same with your other for loops too.

smaller example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
using namespace std;

int main(){

	const int size = 10;

	int myArray[size] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

	for (int i = 0; i < size; i++)
	{
		cout << "array index = " << i << ". myArray[" << i + 1 << "]" << " = " << myArray[i] << endl;

	}

	return 0;
} 
Last edited on
Thank you mutexe it is all really helpful.
Done:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
using namespace std;

int main(){

int yaxis[10][1] = {{1},{2},{3},{4},{5},{6},{7},{8},{9}};
int xaxis[1][10] = {{1,2,3,4,5,6,7,8,9}};



for(int x = 0; x < 9; x++){
    for(int i = 0; i < 9; i++){
    cout << setw(4) << yaxis[x][0] * xaxis[0][i] ;
        }
    cout << endl;
    }
    
system("pause");
    return 0;
}
Topic archived. No new replies allowed.