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.
Related
Here in this code I am trying to dynamically allocate memory to a 2d array and print it The problem I face here is I cant get the output properly lets assume my input is an 2d array of size 2*2 [1,2,3,4] but the output printed is [1,1,1,1].
#include <stdio.h>
#include <stdlib.h>
int output_func(int row, int col, int *ptr)
{
printf("\nThis is your first matrix: \n\n");
for (int i = 0; i < row; i++)
{
printf("\t |");
for (int j = 0; j < col; j++)
{
printf(" %d ", *ptr);
}
printf("|\n");
}
}
int main()
{
int **ptr;
int row, col;
int i, j;
printf("enter the no of rows for the matrix: ");
scanf("%d", &row);
printf("enter the no of col for the matrix: ");
scanf("%d", &col);
ptr = (int **)malloc(row * sizeof(int *));
for (int i = 0; i < row; i++)
{
ptr[i] = (int *)malloc(col * sizeof(int));
}
// input of first matrix
printf("\nEnter the elements for first matrix :\n");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf("\t A[%d,%d] = ", i, j);
scanf("%d", &ptr[i][j]);
}
}
output_func(row, col, *ptr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// int* ptr -> int** ptr
void output_func(int row, int col, int** ptr)
{
printf("\nThis is your first matrix: \n\n");
for (int i = 0; i < row; i++)
{
printf("\t |");
for (int j = 0; j < col; j++)
{
// *ptr -> ptr[i][j]
printf(" %d ", ptr[i][j]);
}
printf("|\n");
}
}
int main()
{
int** ptr;
int row, col;
printf("enter the no of rows for the matrix: ");
scanf("%d", &row);
printf("enter the no of col for the matrix: ");
scanf("%d", &col);
ptr = (int**)malloc(row * sizeof(int*));
for (int i = 0; i < row; i++)
{
ptr[i] = (int*)malloc(col * sizeof(int));
}
// input of first matrix
printf("\nEnter the elements for first matrix :\n");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf("\t A[%d,%d] = ", i, j);
scanf("%d", &ptr[i][j]);
}
}
output_func(row, col, ptr);
return 0;
}
If you want a function to print the array allocated using an array of pointers to arrays of int (also known as a jagged array), you must pass the pointer to the array of pointers.
In other words - this is wrong:
int output_func(int row, int col, int *ptr)
^^^^^^^^
A int-pointer but you want
a pointer to a pointer to int
which is "int **ptr"
So do
int output_func(int row, int col, int **ptr)
{
printf("\nThis is your first matrix: \n\n");
for (int i = 0; i < row; i++)
{
printf("\t |");
for (int j = 0; j < col; j++)
{
printf(" %d ", ptr[i][j]);
}
printf("|\n");
}
}
and call it like:
output_func(row, col, ptr);
So, my functions in my code don't talk to each other and I cannot for the life of me figure it out. What do you put in the parenthesis of the function headings. the variable names go in them but how are they formatted? Do I need pointers? If so can someone explain them. Thanks!
int FindTranspose(????)
int main(????)
FindTranspose (????)
#include <stdio.h>
int FindTranspose(int before[3][3], int after[3][3], int i, int j){
int rows, columns;
printf("\nThe orginial matrix: \n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("%d ", before[i][j]);
}
}
for(rows = 0; rows < i; rows++){
for(columns = 0;columns < j; columns++){
after[columns][rows] = before[rows][columns];
}
}
printf("\n");
printf("\nThe Transpose of the matrix: \n");
printf("\n");
for(rows = 0; rows < j; rows++){
for(columns = 0; columns < i; columns++){
printf("%d ", after[rows][columns]);
}
printf("\n");
}
}
int main(){
int before[3][3], after[3][3], i, j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("Enter number for array - [%d],[%d] : ",i,j);
scanf("%d", &before[i][j]);
}
}
FindTranspose (before[3][3], after[3][3], i, j);
return 0;
}
Picture of code
Picture of warnings
Dealing with multi-dimensional arrays as pointers is really tricky in C.
For example the multi-dimensional array before[3][3] defined in main:
The array name before is not "as usual" a pointer to before[0][0] although it's a pointer to before[0] so before actually has the type int (*) [3] which read as 'Pointer to a one-dimensional array of 3 elements'
Anyway, you should learn more about "pointer and multi-dimensional arrays".
For the moment here is the solution to your issue:
#include <stdio.h>
void FindTranspose(int (*before)[3], int (*after)[3], int i, int j){
int rows, columns;
printf("\nThe orginial matrix: \n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("%d ", before[i][j]);
}
}
for(rows = 0; rows < i; rows++){
for(columns = 0;columns < j; columns++){
after[columns][rows] = before[rows][columns];
}
}
printf("\n");
printf("\nThe Transpose of the matrix: \n");
printf("\n");
for(rows = 0; rows < j; rows++){
for(columns = 0; columns < i; columns++){
printf("%d ", after[rows][columns]);
}
printf("\n");
}
}
int main(){
int before[3][3], after[3][3], i, j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("Enter number for array - [%d],[%d] : ",i,j);
scanf("%d", &before[i][j]);
}
}
FindTranspose (before, after, i, j);
return 0;
}
Of course, there is an easier but complex to understand solution which is dealing with the multi-dimensional array as a one-dimensional array.
Here is an example of that:
#include <stdio.h>
#define NUM_OF_ROWS 3
#define NUM_OF_COLS 3
void FindTranspose(int *before){
int rows, columns, i;
int after[NUM_OF_ROWS][NUM_OF_COLS];
int *p, (*q)[NUM_OF_COLS];
printf("\nThe orginial matrix: \n");
i = 0;
for(p = before; p < before + NUM_OF_COLS * NUM_OF_ROWS; p++){
printf("%d ", *p);
if(++i >= NUM_OF_COLS){
i = 0;
printf("\n");
}
}
columns = 0;
for(p = before; p < before + NUM_OF_COLS * NUM_OF_ROWS;){
for(q = &after[0]; q < &after[NUM_OF_ROWS]; q++){
(*q)[columns] = *p++;
}
columns++;
}
printf("\n");
printf("\nThe Transpose of the matrix: \n");
for(rows = 0; rows < NUM_OF_ROWS; rows++){
for(columns = 0; columns < NUM_OF_COLS; columns++){
printf("%d ", after[rows][columns]);
}
printf("\n");
}
}
int main(){
int before[NUM_OF_ROWS][NUM_OF_COLS], i, j;
for(i=0; i<NUM_OF_ROWS; i++){
for(j=0; j<NUM_OF_COLS; j++){
printf("Enter number for array - [%d][%d]: ", i, j);
scanf("%d", &before[i][j]);
}
}
FindTranspose ( &before[0][0] );
return 0;
}
Recommended reading: C Programming A modern approach 2nd edition for K.N. King
I am trying to create a program which prints a matrix of integers, but the output returns weird numbers before the actual matrix. There are no compiling errors.
This is what my code looks like: //ignore void function for now, focus on main function::
#include <stdio.h>
#include <stdlib.h>
//array[30][30] = 2D array of max 30 rows and 30 columns
//n = number of rows and columns
void printmatrix(int array[30][30], int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d", array[i][j]);
}
printf("\n");
}
return;
}
int main(){
int n;
scanf("%d", &n);
int ints2D[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &ints2D[i][j]);
}
}
printmatrix(ints2D, n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", ints2D[i][j]);
}
printf("\n");
}
return 0;
}
And this is my output (I only want the last three lines)
123
-514159984327663
-51415932632766-514159305
1 2 3
4 5 6
7 8 9
You are missing a space in "%d" in printmatrix, and, more importantly, it is not proper to pass an int [n][n] array for an int [30][30] parameter unless n is 30.
Change void printmatrix(int array[30][30], int n) to void printmatrix(int n, int array[n][n]), and change printmatrix(ints2D, n); to printmatrix(n, ints2D);. That makes the type of the argument you are passing match the type of the parameter.
In your function args you defined the array as fixed size ([30][30]) but you are passing a VLA ([3][3] in your example) which makes it find uninitialized memory and why you are seeing the strange numbers.
#Eric Postpischil answer is spot on. Another way to solve it: 2d arrays could be flatten into 1d. Here is a working code for you:
#include <stdio.h>
#include <stdlib.h>
//array[30][30] = 2D array of max 30 rows and 30 columns
//n = number of rows and columns
void printmatrix(int *array, int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", array[i * n + j]);
}
printf("\n");
}
return;
}
int main(){
int n;
scanf("%d", &n);
int ints2D[n * n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &ints2D[i * n + j]);
}
}
printmatrix(ints2D, n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", ints2D[i * n + j]);
}
printf("\n");
}
return 0;
}
I need to make a little project but I completely don't know how. Im giving matrix A of size n, and it have to return me matrix B which is matrix A with zeroed first and penultimate column. All I did is
#include<stdio.h>
#include<math.h>
int main()
{
int i,n,j,;
int tab[n][n];
printf("Size of matrix:");
scanf("%d",&n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
printf("A[%d][%d]=",i,j);
scanf("%lf",&tab[i][j]);
}
printf("Data:");
printf("Matrix A[%d][%d]",n,m);
}
Which I think should let me to type my matrix. What I should do next? Please help me.
There are a lot of errors in your code, the variable m is not declared, the double array is declared with n non-initialized. As the size of matrix is only known at runtime (entered by user), you need to use dynamic memory allocation functions to allocate memory for your matrix.
Try this code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j, n;
printf("Size of matrix: ");
scanf("%d", &n);
int *tab = (int*)malloc(sizeof(int)*n*n);
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("A[%d][%d]=",i,j);
scanf("%d",(tab+i*n+j));
}
}
for (i = 0; i < n; i++)
{
*(tab+i*n) = 0;
*(tab+i*n+n-2) = 0;
}
//Print tab
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", *(tab+i*n+j));
}
printf("\n");
}
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I've tried to create two matrices and do the product in another matrix, but the compiler gives an error of core dump. The creation of the first two matrices is right; something is wrong with the third matrix.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m;
int n;
int t;
int i;
int **A;
int **B;
int k;
int j;
scanf("%d",&n);
scanf("%d",&m);
scanf("%d",&t);
A=malloc(n*sizeof(int*));
for(i=0;i<n;i++){
A[i]=malloc(m*sizeof(int));
}
for(i=0;i<n;i++){ // A[n][m]
for(j=0;j<m;j++)
{
scanf("%d",&(A[i][j]));
}
}
B=malloc(t*sizeof(int*));
for(i=0;i<t;i++) //B[m][t]
{
B[i]=malloc(n*sizeof(int));
}
for(i=0;i<t;i++){
for(j=0;j<n;j++)
{
scanf("%d",&(B[i][j]));
}
}
int **C;
C=malloc(t*sizeof(int*));
for(i=0;i<t;i++){{A[i]=malloc(m*sizeof(int));}
for(i=0;i<t;i++){
for(j=0;j<m;j++){
C[i][j]=0;
for(k=0;k<n;k++)
{
(C[i][j])=(C[i][j])+((A[k][j])*(B[i][k]));
}
}
}
}
return 0;
}
You must separate memory for C, not for A. That is why when you try to access C[i][j] it generates this error. Change:
for(i=0;i<t;i++){{A[i]=malloc(m*sizeof(int));}
to
for(i=0;i<t;i++){ C[i]=malloc(m*sizeof(int));}
Complete code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m;
int n;
int t;
int i;
int **A;
int **B;
int k;
int j;
scanf("%d",&n);
scanf("%d",&m);
scanf("%d",&t);
A=malloc(n*sizeof(int*));
for(i=0;i<n;i++){
A[i]=malloc(m*sizeof(int));
}
for(i=0;i<n;i++){ // A[n][m]
for(j=0;j<m;j++){
scanf("%d", &(A[i][j]));
}
}
B=malloc(t*sizeof(int*));
for(i=0;i<t;i++) //B[m][t]
{
B[i]=malloc(n*sizeof(int));
}
printf("B\n");
for(i=0;i<t;i++){
for(j=0;j<n;j++)
{
scanf("%d",&(B[i][j]));
}
}
int **C;
C=malloc(t*sizeof(int*));
for(i=0;i<t;i++){
C[i]=malloc(m*sizeof(int));
}
for(i=0;i<t;i++){
for(j=0;j<m;j++){
C[i][j]=0;
for(k=0;k<n;k++)
{
(C[i][j])=(C[i][j])+((A[k][j])*(B[i][k]));
}
}
}
return 0;
}
Previous answers may have fixed the problem that caused the crash, but the code is still broken. It would be nice if the variables had more descriptive names. Since each variable is declared on a separate line, you could make use of the space to provide descriptive comments about the variables.
It is conventional to reference the rows of a matrix first, and then the columns. So, I will diverge from your usage, and say that matrix A has m rows and n columns. B must then have n rows, and it looks like you intend for t to hold the number of columns in B. Then the product C will be a m X t matrix.
Your code started to go wrong when you allocated space for B. You allocated for t rows of n elements, when you should have allocated for m rows of t elements (by your own notation). In my code below, because I have changed the order of m and n, I allocate for n rows of t elements.
Then, for the product matrix, I have allocated for m rows of t elements, where you had allocated for t rows of m elements. The calculation of the elements of the product matrix was also wrong in your code. The [i][j] element of C is the vector dot-product of the ith row of A and the jth column of B. The way you have calculated this element, C[i][j] is the dot-product of the jth column of A and the ith row of B.
Here is the code with corrections. I included some input prompts, and some code to display the entered matrices and the resulting product.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int m; // rows in A
int n; // cols in A
int t; // cols in B
int i;
int **A; // points to first row of A
int **B; // points to first row of B
int **C; // points to first row of C
int k;
int j;
printf("Enter number of rows in A: ");
scanf("%d", &m);
printf("Enter number of columns in A: ");
scanf("%d", &n);
printf("Enter number columns in B: ");
scanf("%d", &t);
A = malloc(sizeof(int*) * m); // A[m][n]
for(i = 0;i < m; i++){
A[i] = malloc(sizeof(int) * n);
}
for(i = 0; i < m; i++){
for(j = 0; j < n; j++)
{
scanf("%d", &(A[i][j]));
}
}
B = malloc(sizeof(int*) * n); // B[n][t]
for(i = 0; i < n; i++)
{
B[i] = malloc(sizeof(int) * t);
}
for(i = 0; i < n; i++){
for(j = 0; j < t; j++)
{
scanf("%d", &(B[i][j]));
}
}
C = malloc(sizeof(int*) * m); // C[m][t]
for(i = 0; i < m; i++){
C[i] = malloc(sizeof(int) * t);
}
for(i = 0; i < m; i++){
for(j = 0; j < t; j++){
C[i][j] = 0;
for(k = 0; k < n; k++)
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}
printf("Matrix A:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%-5d", A[i][j]);
}
putchar('\n');
}
printf("Matrix B:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < t; j++) {
printf("%-5d", B[i][j]);
}
putchar('\n');
}
printf("Matrix product:\n");
for (i = 0; i < t; i++) {
for (j = 0; j < m; j++) {
printf("%-5d", C[i][j]);
}
putchar('\n');
}
return 0;
}
Here is the result of a test run:
λ> ./a.out
Enter number of rows in A: 2
Enter number of columns in A: 3
Enter number columns in B: 2
2 3 4
1 3 5
3 4
5 6
7 8
Matrix A:
2 3 4
1 3 5
Matrix B:
3 4
5 6
7 8
Matrix product:
49 58
53 62