#include<stdio.h>
#include<stdlib.h>
int** addMatrix(int**, int, int, int**);
int main(void)
{
int i, j, m, n, **p, **q, **sum; //i = row, j = column
printf("Enter the size of the row: ");
scanf("%d", &m);
printf("Enter the size of the col: ");
scanf("%d", &n);
p = (int**)malloc(m * sizeof(int*));
for(i=0; i<n; i++)
{
p[i] = (int*)malloc(n * sizeof(int));
}
printf("Enter the elements of the Matrix M:\n"); //taking input
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
scanf("%d", ((p+i)+j)); // LINE 27
}
q = (int**)malloc(m * sizeof(int*));
for(i=0; i<n; i++)
{
q[i] = (int*)malloc(n * sizeof(int));
}
printf("Enter the elements of the Matrix N:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
scanf("%d", ((q+i)+j)); // LINE 41
}
sum = addMatrix(p, m, n, q);
printf("Vector Sum:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("%d ", *(*(sum+i)+j));
printf("\n");
}
}
int** addMatrix(int **p, int m, int n, int **q)
{
int i, j, **add;
add = (int**)malloc(m * sizeof(int*));
for(i=0; i<n; i++)
{
add[i] = (int*)malloc(n * sizeof(int));
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
*(*(add+i)+j) = *(*(p+i)+j) + *(*(q+i)+j);
}
return add;
}
main.c:27:21: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int **’ [-Wformat=]
main.c:41:21: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int **’ [-Wformat=]
the lines for taking the input of the matrix. this is the error I am getting.
The for loops like this
for(i=0; i<n; i++)
{
p[i] = (int*)malloc(n * sizeof(int));
}
are wrong. You have to use the variable m (the number of rows) instead of n (the number of columns)
for(i=0; i<m; i++)
{
p[i] = (int*)malloc(n * sizeof(int));
}
The calls of scanf like this
scanf("%d", ((p+i)+j));
are also wrong.
You have to write
scanf("%d", *(p+i)+j );
The expression *(p+i)+j is equivalent to p[i] + j that can be also written like &p[i][j].
And you should free all the allocated memory before exiting the program.
In general you should check whether values for the variables m and n are valid and whether memory was allocated successfully. And it will be better if the variables m and n will have an unsigned integer type as for example size_t instead of the signed integer type int.
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 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).
What I'm trying to do is find a way to add extra elements to the already hardcoded int array matrix. Here is my current code:
#include <stdio.h>
int main(){
int matrix[25] = {2,3};
int i;
int j;
for(i=4,j=2; i<21 && j<17; i++,j++){
matrix[j] = i;
}
printf("%d", matrix);
}
I'm not sure what went wrong here.
You can't print array elements with integer type specifier %d. You need to iterate through the array elements using a loop such as for and then print each element.
for(int x=0; x < 17; x++) {
printf("%d", matrix[x]);
}
You are giving to print directly matrix but it will show a warning like this
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
this is because when you give only matrix without indexes([]) or '*' it prints garbage value so you can use loops for printing a matrix
for(int i=0; i < 17; i++)
printf("%d", matrix[i]);
OR
for(int i=0; i < 17; i++)
printf("%d ", *(matrix+i));
Peace!
Happy Coding!
int main()
{
int r = 3, c = 4;
int *arr = (int *)malloc(r * c * sizeof(int));
int i, j, count = 0;
for (i = 0; i < r; i++) *emphasized text*
for (j = 0; j < c; j++)
*(arr + i*c + j) = ++count;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
printf("%d ", *(arr + i*c + j));
Use can use malloc to to create dynamic array. by using the malloc if the size of the array is full you can reallocate the array by using the malloc function. The malloc will copy the previous array in the new array with the new size.
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
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);