I would like to get your help to understand and finish my program.
This is what I have to do:
"You must exercise program that: First. An absorbing two - dimensional integer arr [M] [N]. M - number of rows N - number of columns. (Matrix size was received from the user) Two. The program uses auxiliary functions "shift" moves the values of the matrix to the right one place, as shown in the picture (2 entered instead of 1, 3 instead of 2, 4 instead of 3, ... 20 instead of 19, first place 20). Shift have to write a function and call her three times in the sample matrix loop .."
Example picture:
error msg:
After trying to solve the problem i gave up, i would like to get your help to understand what is wrong in my code. Is it about the memory? This is my code:
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "malloc.h"
void shift (int **arr,int rows,int cols);
void freemalloc ( int **arr,int rows);
void main()
{
int **arr,cols,rows;
int i,j;
printf("please insert rows and columns of the matrix: ");
scanf_s("%d%d",&rows,&cols);
arr=(int **)malloc(rows*sizeof(int *));
for(i=0; i<rows; i++)
arr[i]=(int *)malloc(cols*sizeof(int));
for (i=0; i<rows; i++)
for (j=0; j<cols; j++)
{
printf("rows %d , cols %d :\n", i, j);
scanf_s("%d", &arr[i][j]);
}
shift (arr,rows,cols);
freemalloc (arr,rows);
system("pause");
return ;
}
void shift (int **arr,int rows,int cols)
{
int i,j,temp=0;
for(i=0; i<rows ; i++ )
for( j=0 ; j<cols ; j++);
{
temp=arr[i][j];
arr[i][j]=arr[i][cols-1];
arr[i][cols-1]=temp;
}
for(i=0; i<rows ; i++)
{
for(j=0; j<cols ; j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
void freemalloc ( int **arr,int rows)
{
int i;
for (i=0 ; i<rows ; i++)
{
free(arr[i]);
}
free(arr);
}