2

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: enter image description here

error msg: enter image description here

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);
}
VoronoiPotato
  • 3,113
  • 20
  • 30
Sagi Binder
  • 69
  • 3
  • 4
  • 13

2 Answers2

4

I noted this in general-comment, and I'll amplify it here. This is wrong, and wiil result in undefined behavior:

for(i=0; i<rows ; i++ )
    for( j=0 ; j<cols ; j++); // <<== this is wrong.
{
    temp=arr[i][j];
    arr[i][j]=arr[i][cols-1];
    arr[i][cols-1]=temp;
}

That trailing semi-colon is outright wrong. With it, the code literally becomes this:

// A useless nested for loop that runs `i` to `rows` and `j to `cols`
for(i=0; i<rows ; i++ )
    for( j=0 ; j<cols ; j++);

// Now `i=rows` and `j=cols`. then you do this
temp=arr[i][j];
arr[i][j]=arr[i][cols-1];
arr[i][cols-1]=temp;

You're accessing memory you don't own, as arr is only indexible to [rows-1][cols-1] Anything outside that is undefined behavior.

Correction

for(i=0; i<rows ; i++ )
    for( j=0 ; j<cols ; j++) // note: no semi-colon.
    {
        temp=arr[i][j];
        arr[i][j]=arr[i][cols-1];
        arr[i][cols-1]=temp;
    }

Fix your code by removing that semi-colon, and at least that part of your problem is addressed. I cannot speak for other issues.

Finally, enable your compiler warnings to pedantic levels. Any reasonable compiler configured with properly heightened warnings would catch this issue.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
2

The error has been already pointed out in another answer, but let me give you another piece of good advice: do not use pointers-to-pointers to represent multidimensional arrays.

Also, do NOT cast the return value of malloc().

This is what you should do (should have done):

int (*mat)[width] = malloc(height * sizeof(*mat));
Community
  • 1
  • 1