Matrix is not printing properly, Can anyone tell me where I went wrong??
int i , j;
int n =0 , m =0;
int p =0 , q =0;
int matrix1[n][m];
int matrix2[p][q];
printf("ENTER THE NUMBER OF ROWS AND COLUMNS IN 1st MATRIX\n");
scanf ("%d%d",&n,&m);
for (i=0; i<n; i++)
for (j=0; j<m; j++)
{
printf ("ENTER THE matrix1[%d][%d]: ",i,j);
scanf ("%d",&matrix1[i][j]);
}
I believe this is where the problem is.
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
printf ("%d ",matrix1[i][j]);
}
printf ("\n");
}
If you want memory to store objects whose size is known only during runtime, I would suggest you to allocate it as required, use the following code
int i, j;
int n, m;
int **matrix;
printf("ENTER THE NUMBER OF ROWS AND COLUMNS IN 1st MATRIX\n");
scanf ("%d%d",&n,&m);
matrix = malloc(sizeof(int *) * n);
if (!matrix) {/* no memory; handle it */}
for (i=0; i<n; i++)
matrix[i] = malloc(sizeof(int) * m);
for (j=0; j<m; j++)
{
printf ("ENTER THE matrix[%d][%d]: ",i,j);
scanf ("%d",&matrix[i][j]);
}
Don't forget to free your memory
for (i = 0; i < n; i++)
free(matrix[i]);
free(matrix);
Related
I have been trying to create an NxN square matrix (the size of the square matrix is determined by the user) but seem to have run into an issue. The displayed matrix is actually a symmetric matrix which is not what I am trying to as I want to be able to have each value hold a unique integer. Any help is greatly appreciated. Here's my code:
#include <stdio.h>
int main()
{
int i;
int j;
int size = 1;
int array [size][size];
printf ("Enter the size you would like for the nxn matrix: ");
scanf ("%d", &size);
printf ("Now enter elements into the matrix. \n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("[%d][%d] = ", i, j);
scanf ("%d", &array [i][j]);
}
}
printf ("------------------------\n\n");
printf ("the matrix is: \n\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf (" %d ", array [i][j]);
}
printf ("\n");
}
return (0);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Create variables
int i, j, size;
// Create 2D array
printf ("Enter the size you would like for the nxn matrix: ");
scanf ("%d", &size);
int *array = (int *)malloc(size * size * sizeof(int));
// Fill 2D array with values
printf ("Now enter elements into the matrix. \n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("[%d][%d] = ", i, j);
scanf ("%d", &array[i * size + j]);
}
}
printf ("------------------------\n\n");
// Display values of 2D array
printf ("the matrix is: \n\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("%d", array[i * size + j]);
}
printf ("\n");
}
free(array);
return (0);
}
SO i was solving this problem and i am getting this error
What is my mistake here?Why is it saying invalid argument type?Is there any declaration mistake I made?
I am a newbie still I am trying hard to learn these. A detailed explanation will be useful
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int m,n;
printf("Input the number of Rows: ");
scanf("%i", &m);
printf("Input the number of Columns: ");
scanf("%i", &n);
int *arr=(int*)malloc(m * sizeof(int));
for(int i=0;i<m;i++)
{
arr[i] = (int*)malloc(n*sizeof(int));
}
printf("Populate Matrix Row by Row\n--------------------------\n");
for(int i=0; i<m; i++){
printf("Row [%i]\n--------\n",i);
for(int j=0; j<n; j++){
printf("At Col [%i]= ",j);
scanf("%i", &*(*(arr+i) + j));
printf("\n");
}
}
printf("[MATRIX]\n------------------\n");
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
printf("%i ",*(*(arr+i) + j));
}
printf("\n");
}
printf("------------------\n");
printf("The duplicate value(s) are:\n");
int temp_index=0;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
temp_index=j;
for(int x=i; x<m; x++){
for(int y=temp_index; y<n; y++){
if(j!=y){
if(*(*(arr+i) + j) == *(*(arr+x) + y)){
printf("%i in position (%i, %i)\n",*(*(arr+i) + j),x,y);
}
}
}
temp_index=0;
}
}
}
free(arr);
return 0;
}
int *arr=(int*)malloc(m * sizeof(int));
*(*(arr+i) + j): *(arr+i) is int, you are trying to dereference the int value *(arr+i) + j . I assume you would wish
int **arr = malloc(m * sizeof(int*));
It would be more clear and shorter if you had used arr[i][j] instead of *(*(arr+i) + j).
I need to make some functions for 2-dimensional arrays (a.k.a. matrixes) and the problem just starts with the first one.
I allocated the matrix in the heap memory using malloc() and then I tried to make a function that initializes the matrixes using a for loop of scanf()s, but after 3 times it takes the input, the program crashes.
Here is the code I made
void initMatrix(int **mat,int row,int col)
{
for(int i=0;i<row;i++){
printf("\n");
for(int j=0;j<col;i++){
printf("Cell (%d,%d): ",i,j);
scanf("%d",&mat[i][j]);
}
}
}
int main()
{
int r,c;
printf("Number of rows: ");
scanf("%d",&r);
printf("Number of columns: ");
scanf("%d",&c);
int **arr = (int **)malloc(sizeof(int*) * r);
for(int i=0;i<r;i++)
arr[i]=(int*)malloc(sizeof(int) * c);
// int count=0;
// for (int i = 0; i < r; i++)
// for (int j = 0; j < c; j++)
// arr[i][j] = ++count;
for (int i = 0; i < r; i++){
printf("\n");
for (int j = 0; j < c; j++)
printf("%d ", arr[i][j]);
}
initMatrix(arr,r,c);
printf("\n");
free(arr);
}
If I manually insert the content of the matrix, the program does work (without the initmatrix() function), I don't know why… I probably made a mistake somewhere.
I'm trying to multiply 2 dynamically allocated arrays. I'm having 2 problems:
When I try unequal sized arrays like [2],[3] and [3],[2] I got a segmentation fault 11, and after staring at my allocations I still can't figure out why.
My final array is formatted with the correct rows and columns, but it displays all 0's.
I'm assuming that this is because I didn't allocate the memory correctly.
-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **a, **b, **c; //pointers to arrays
int m1_r,m1_c,m2_r,m2_c; //declaring arrays
int i,j,k;
printf("\n");
again://repeat if first matrixes are bad
printf("Enter rows and columns for the first matrix.\n");//first matrix
scanf("%d%d" ,&m1_r,&m1_c);
printf("Enter rows and Columns for the second matrix.\n");//second matrix
scanf("%d%d",&m2_r,&m2_c);
if(m1_c!=m2_r) {
printf("You tried to break my code. Nice try.");
goto again;
}
//memory for first matrix
a = malloc(m1_r * sizeof(int *));
for(i=0; i < m1_r; i++) {
a[i] = malloc(m1_c * sizeof(int));
}
//memory for second matrix
b = malloc(m2_r * sizeof(int *));
for(i=0; i < m2_r; i++) {
b[i] = malloc(m2_c * sizeof(int));
}
//memory for 3rd matrix
c = malloc(m1_r * sizeof(int *));
for(i=0; i < m2_r; i++) {
c[i] = malloc(m2_c * sizeof(int));
}
//input 1st matrix
printf("Enter the numbers of the first matrix.\n");
for (i=0; i<m1_r; i++) {
for (j = 0; j<m1_c; j++) {
scanf("%d", &a[i][j]);
}
}
//input 2nd matrix
printf("Enter the second of the first matrix.\n");
for (i=0; i<m1_r; i++) {
for (j = 0; j<m1_c; j++) {
scanf("%d", &b[i][j]);
}
}
printf("\n");
printf("1st matrix looks like this:\n");
//print 1st matrix
for (i=0; i<m1_r; i++) {
for (j = 0; j<m1_c; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
//print 2nd matrix
printf("\n");
printf("2nd matrix looks like this:\n");
//print 2st matrix
for (i=0; i<m2_r; i++) {
for (j = 0; j<m2_c; j++) {
printf("%d\t", b[i][j]);
}
printf("\n");
}
//initialize result matrix to 0
for(i=0; i<m2_r; i++)
for(j=0; j<m2_c; j++) {
c[i][j]=0;
}
//multipication
for(i=0; i<m1_r; i++)
for(j=0; j<m2_c; j++)
for(k=0; k<m1_c; k++) {
c[i][j]+= a[i][k]*b[k][j];
}
//print result
printf("\nThe result of the matrix multiplication is:");
for(i=0; i<m1_r; i++) {
printf("\n");
for(k=0; k<m2_c; k++) {
printf("%d\t", c[i][j]);
}
}
printf("\n");
return 0;
}
You allocate the wrong amount of memory for the third matrix:
c = malloc(m1_r * sizeof(int *));
for(i=0; i < m2_r; i++)
The loop count should be the same as the number of pointers you malloc.
To avoid this sort of error, consider making a function which you pass in the dimensions and it returns the pointer.
Later on, you overwrite its bounds using different indices again:
for(i=0;i<m2_r;i++)
for(j=0;j<m2_c;j++)
{
c[i][j]=0;
}
Then you overwrite the bounds of b (it was m2_r and m2_c):
for (i=0; i<m1_r; i++) {
for (j = 0; j<m1_c; j++) {
scanf("%d", &b[i][j]);
}
}
To avoid this sort of error, you could use a better naming convention for your variables; and also consider using a struct which holds each pointer plus its dimension variables. Then you can have a function that zeroes any matrix and you only need to pass it a pointer to one of your matrix structs.
BTW if you use calloc instead of malloc then you don't need this loop at all (although you might want to have this function anyway so that you can zero a matrix).
Also you should check for success of scanf and malloc.
There are hell lot of bugs in your code:
First
//memory for 3rd matrix
c = malloc(m1_r * sizeof(int *));
for(i=0; i < m2_r; i++) <----- error: used m2_r instead of m1_r
You assigned m1_r and loop till m2_r.
Second
//input 2nd matrix
printf("Enter the second of the first matrix.\n");
for (i=0; i<m1_r; i++) { <----- error: used m1_r instead of m2_r
for (j = 0; j<m1_c; j++) { <----- error: used m1_c instead of m2_c
scanf("%d", &b[i][j]);
}
}
You are using rows and columns of 1st matrix.
Third
//initialize result matrix to 0
for(i=0; i<m2_r; i++) <----- error: used m2_r instead of m1_r
for(j=0; j<m2_c; j++) {
c[i][j]=0;
}
You used row value of second matrix rather than the first matrix
This is the only way I know to create a matrix (2D array) in C, dynamically, and reading user input into its elements:
Creating a pointer to an array of x pointers, where each pointer
represents a line in the matrix - x is the number of lines in the matrix (its height).
Pointing each pointer in this array to an array with y elements,
where y is the number of columns in the matrix (the width).
int main()
{
int i, j, lines, columns, **intMatrix;
printf("Type the matrix lines:\t");
scanf("%d", &lines);
printf("Type the matrix columns:\t");
scanf("%d", &columns);
intMatrix = (int **)malloc(lines * sizeof(int *));
//pointer to an array of [lines] pointers
for (i = 0; i < lines; ++i)
intMatrix[i] = (int *)malloc(columns * sizeof(int));
//pointer to a single array with [columns] integers
for (i = 0; i < lines; ++i)
{
for (j = 0; j < columns; ++j)
{
printf("Type a number for <line: %d, column: %d>\t", i+1, j+1);
scanf("%d", &intMatrix[i][j]);
}
}
Are there other ways to do this?
You can try like this
int main()
{
int i, j, lines, columns, *intMatrix;
printf("Type the matrix lines:\t");
scanf("%d", &lines);
printf("Type the matrix columns:\t");
scanf("%d", &columns);
intMatrix = (int *)malloc(lines * columns * sizeof(int));
for (i = 0; i < lines; ++i)
{
for (j = 0; j < columns; ++j)
{
printf("Type a number for <line: %d, column: %d>\t", i+1, j+1);
scanf("%d", &intMatrix[i*lines + j]);
}
}
From C99 onwards (but not C++), you can use variable length arrays:
int main()
{
int i, j, lines, columns;
printf("Type the matrix lines:\t");
scanf("%d", &lines);
printf("Type the matrix columns:\t");
scanf("%d", &columns);
{
int intMatrix[lines][columns];
for (i = 0; i < lines; ++i)
{
for (j = 0; j < columns; ++j)
{
printf("Type a number for <line: %d, column: %d>\t", i+1, j+1);
scanf("%d", &intMatrix[i][j]);
}
}
}
}
Or even like this:
void readData (int lines, int columns, int array[lines][columns])
{
int i, j;
for (i = 0; i < lines; ++i)
{
for (j = 0; j < columns; ++j)
{
printf("Type a number for <line: %d, column: %d>\t", i+1, j+1);
scanf("%d", &array[i][j]);
}
}
}
int main()
{
int lines, columns;
printf("Type the matrix lines:\t");
scanf("%d", &lines);
printf("Type the matrix columns:\t");
scanf("%d", &columns);
{
int intMatrix[lines][columns];
readData (lines, columns, intMatrix);
}
}
But, in both cases, the array data is all stored on the stack, not the heap, so there's no way to store it properly, and you can't put it in a struct or anything malloc'd.
struct matrix {
type *mem;
};
struct matrix* matrix_new () {
struct matrix *M = malloc (sizeof(matrix));
M->mem = malloc (sizeof (type) * rows * cols);
return M;
}
use realloc,memcpy,memmove and free to modify chunks of the array. To access single elemnts use something like mem[row*cols+col] or mem[rows*col+row] depend on what has priority for you (or what you define a row and a column).