0

I am trying to multiply two single arrays and printing out a 2D array...basically I'm making a multiplication table. I keep getting SEGMENTATION FAULT error. Any suggestions?

#include <stdio.h>
#define COLUMN_SIZE 12
#define ROW_SIZE 12
#define SIZE 12

int main (void)
{

int i, j, k;

int column[COLUMN_SIZE] = {1,2,3,4,5,6,7,8,9,10,11,12};
int row[ROW_SIZE] = {1,2,3,4,5,6,7,8,9,10,11,12};
int product[][SIZE] = {{},{}};

        for(int i=0; i<ROW_SIZE; i++){
                for(int j=0; j<COLUMN_SIZE; j++){
                        product[i][j] = row[i] * column[j];
                }
        }

        printf("%d\n", product[i][j]);

return 0;

}
2
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by az-mi51
Featured Replies
  • You need to define the size of the 2d array. The way you have it now it should the compiler will make it 2X12 whn you actually need it to be 12X12. I would change it to `int prodouct[ROW_SIZE][COLUMN_SIZE];` Read More

0

Updated a little bit Now it prints out nothing. Probably infinite for loop somewhere.

#include <stdio.h>

int main (void)
{

int i, j;

int column[13] = {0,1,2,3,4,5,6,7,8,9,10,11,12};
int row[13] = {0,1,2,3,4,5,6,7,8,9,10,11,12};
int product[][13] = {{},{}};

        for(int i=0; i<sizeof(row); i++){
                for(int j=0; j<sizeof(column); j++){
                        product[i][j] = row[i] * column[j];
                }
        }

        printf("%d\n", product[i][j]);

return 0;

}
1

You need to define the size of the 2d array. The way you have it now it should the compiler will make it 2X12 whn you actually need it to be 12X12. I would change it to int prodouct[ROW_SIZE][COLUMN_SIZE];

Edited by NathanOliver

Votes + Comments
Thanks! Worked Perfectly
0

Comment Deleted

Edited by az-mi51: Comment Not Needed

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.