VanillaCone

November 5th, 2010, 06:32 AM

I'm doing an assignment for school and I've been brought to a standstill, I'm VERY new to programming, this is my first year so it's pretty difficult for me.

I'm getting a segmentation fault when i try to run this and im not sure why.

#include <stdlib.h>

#include <stdio.h>

#include <stdbool.h>

int **createMatrix (int m, int n)

{

int **matrix;

int i;

matrix = (int**) malloc(m* sizeof (int*));

for (i=0; i<m; i++)

{

matrix[i]=(int*) malloc(n * sizeof (int));

}

return matrix;

}

int **readMatrix (int m, int n)

{

int **matrix = createMatrix(m, n);

int value;

int i = 0;

while (i < m)

{

int j = 0;

while (j < n)

{

scanf("%d", &value);

matrix[i][j] = value;

j++;

}

char newline;

scanf("%c", &newline);

i++;

}

}

void printMatrix (int **matrix, int m, int n)

{

int i;

int j;

for (i = 0; i < m; i++)

{

for (j = 0; j < n; j++)

{

printf("%10d", matrix[i][j]);

}

printf("\n");

}

}

int main (void)

{

int n = 4;

int m = 4;

int **A;

printf("Enter a 4x4 matrix:\n");

A = readMatrix(m, n);

printMatrix(A, m, n);

return 0;

}

edit- also im trying to figure out how to post this code in a more readable fashion, i apologize ><

Kay I found the PHP button sorry about that

So the objective Of this assignment is to dynamically Allocate Memory for a matrix Of unknown Size, then read a user inputted matrix Into each individual Slot of the matrix, then print it out.

Both My Create matrix and read matrix functions work fine, it was after i implemented my printMatrix function That i got a segmentation fault, any advice would be appreciated I dont know how to continue :)

I'm getting a segmentation fault when i try to run this and im not sure why.

#include <stdlib.h>

#include <stdio.h>

#include <stdbool.h>

int **createMatrix (int m, int n)

{

int **matrix;

int i;

matrix = (int**) malloc(m* sizeof (int*));

for (i=0; i<m; i++)

{

matrix[i]=(int*) malloc(n * sizeof (int));

}

return matrix;

}

int **readMatrix (int m, int n)

{

int **matrix = createMatrix(m, n);

int value;

int i = 0;

while (i < m)

{

int j = 0;

while (j < n)

{

scanf("%d", &value);

matrix[i][j] = value;

j++;

}

char newline;

scanf("%c", &newline);

i++;

}

}

void printMatrix (int **matrix, int m, int n)

{

int i;

int j;

for (i = 0; i < m; i++)

{

for (j = 0; j < n; j++)

{

printf("%10d", matrix[i][j]);

}

printf("\n");

}

}

int main (void)

{

int n = 4;

int m = 4;

int **A;

printf("Enter a 4x4 matrix:\n");

A = readMatrix(m, n);

printMatrix(A, m, n);

return 0;

}

edit- also im trying to figure out how to post this code in a more readable fashion, i apologize ><

Kay I found the PHP button sorry about that

So the objective Of this assignment is to dynamically Allocate Memory for a matrix Of unknown Size, then read a user inputted matrix Into each individual Slot of the matrix, then print it out.

Both My Create matrix and read matrix functions work fine, it was after i implemented my printMatrix function That i got a segmentation fault, any advice would be appreciated I dont know how to continue :)