Reputation: 77
I want understand more about arrays. I came up with this question of creating multiplication table that would display this way
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
I though of using arrays this way
int myArray[] = {1,2,3,4,5};
int myArray2[] = {1,2,3,4,5}
The first array would serve as the horizontal set and the 2nd would be the vertical. The problem is I dont know how exactly am I going to multiply each index and display them as a multiplication table.
Upvotes: 1
Views: 4626
Reputation: 46
Below are some examples that you should be able to peak through to get more knowledge on arrays and two dimensional arrays
//X HEADING PLUS MATH
for (int i =0; i < 13; i ++){
System.out.print(i + "\t");
}
System.out.print("\n");
//MATH PORTION PLUS HEADING
for (int y =1; y < 13; y++){
System.out.print(y + "\t");
for (int x=1; x<13; x++){
System.out.print((x*y) + "\t");
}
System.out.print("\n");
}
System.out.print("\n");
//MATH PORTION
//
for (int y =1; y < 13; y++){
for (int x=1; x<11; x++){
System.out.print((x*y) + "\t");
}
System.out.print("\n");
}
System.out.print("\n");*/
//************************************//
//EXAMPLE USING TWO DIMENSIONAL ARRAY //
//W/OUT HEADERS***********************//
//************************************//
//JFrame frame = new JFrame ("HW09");
//int length = Integer.parseInt(JOptionPane.showInputDialog(frame, "Enter the multiplication table length")) + 1;
int length = 13;
int[][] multi = new int[length][length];//created 2 dim array
//formatting heading
for (int i = 0; i<length; i++) {
multi[0][i] = i;
multi[i][0] = i;
}
//fills array with values
for (int yPos = 1; yPos<length; yPos++){//loops through all y positions
for ( int xPos =1; xPos < length; xPos++){//loops through x positions of y position
multi[xPos][yPos] =(xPos)*(yPos);
}
}
//prints out values
for (int yPos = 0; yPos<length; yPos++){//loops through all y positions
for ( int xPos =0; xPos < length; xPos++){//loops through horizontal positions of y position
System.out.print(multi[xPos][yPos] + "\t");
}
System.out.print("\n");
}
System.out.print("\n");
//************************************/
// EXAMPLE USING TWO ARRAYS //
//************************************//
int[] y = new int[] {1,5,6,9};//array for x
int[] x = new int[] {2,5,7,8,10};//array for y
//prints out horizontal header
System.out.print("x\t");
for (int i =0; i < x.length; i ++){
System.out.print(x[i] + "\t");
}
System.out.print("\n");
//prints out y header and then does math
for (int i =0; i < y.length; i ++){
System.out.print(y[i] + "\t");
//loop to do math for what above me
for (int i2 =0; i2 < x.length; i2++ ){
System.out.print((y[i] * x[i2]) + "\t");
}
System.out.print("\n");
}
Upvotes: 1
Reputation: 724
Almost the same as the above, but with formatted output.
public static void main(String[] args) {
int[] h = {1, 2, 3, 4, 5};
int[] v = {1, 2, 3, 4, 5};
for (int i = 0; i < h.length; i++) {
for (int j = 0; j < v.length; j++) {
System.out.printf("%5d", h[i] * v[j]);
}
System.out.println();
}
}
The "%5d" gives you some space, so that the numbers do not clutter together. (Here, I'm assuming that the language is Java, by the way. But you can easily convert this to other languages as you wish. The idea is the same. =) )
Upvotes: 2
Reputation: 3266
You can use something like this assuming you are using Java:
for (int j = 1; j <= 5; j++)
{
for (int i = 0; i < myArray.length; i++)
{
System.out.print(array[i] * j + " ");
if (i == myArray.length - 1)
{
System.out.println();
}
}
j++;
}
The outer loop will walk the numbers from 1 to 5.
The inner loop will use the outer loop's 'j' value to multiply each number in your array.
Then it puts an empty line whenever the inner loop reaches the end of the array.
Upvotes: 1