There is 2D array with pointers from user, filled with random numbers, program count sum of every row. I need to sort array by sum of every row and print it. For example if we have array:1 2 2 (sum=5)2 9 9 (sum=20)2 1 6 (sum=9)
output should be:1 2 2 (sum=5)2 1 6 (sum=9)2 9 9 (sum=20). Thanks for help.
int main () {
int i, j, row, column, **array,sum;
time_t seconds;
time (&seconds);
srand ((unsigned int)seconds );
printf ("Write number of rows:");
scanf ("%d", &row);
printf ("Write number of columns:");
scanf ("%d", &column);
array=(int**) malloc (row * sizeof(int *));
if (array!=NULL){
for (i=0; i<row;i++)
array[i]= (int*) malloc (column *sizeof(int));
}
for (i=0; i<row;i++)
for (j=0; j<column;j++)
array[i][j]=(rand()%100);
for (i=0; i<row;i++){
for (j=0; j<column;j++)
printf("%d ",array[i][j] );
printf ("\n");
}
for(i=0;i<row;i++){ //find sum of each row
sum=0;
for(j=0;j<column;j++){
sum=sum+array[i][j];
}
printf("%d \n",sum);
}
return 0;
}
sample of option 1
#include <stdio.h>
#include <stdlib.h>
int COLUMNS;
int sum(int len, int *array){
int i, sum = 0;
for(i=0; i<len; ++i)
sum += *array++;
return sum;
}
int cmp(const void *a, const void *b){
int sum1 = sum(COLUMNS, *(int**)a);
int sum2 = sum(COLUMNS, *(int**)b);
return (sum1 > sum2) - (sum1 < sum2);
}
int main(void){
int i, j, row, column, **array;
row = 3; column = 3;
array = (int**) malloc (row * sizeof(*array));//cast of (int**) is redundant.
array[0] = (int []){1, 2, 2};
array[1] = (int []){2, 9, 9};
array[2] = (int []){2, 1, 6};
COLUMNS = column;//size of columns pass to compare function by global variable.
qsort(array, row, sizeof(*array), cmp);
for (i=0; i<row;i++){
for (j=0; j<column;j++)
printf("%d ",array[i][j] );
printf ("\n");
}
free(array);
return 0;
}
sample of option 2
#include <stdio.h>
#include <stdlib.h>
int sum(int len, int *array){
int i, sum = 0;
for(i=0; i<len; ++i)
sum += *array++;
return sum;
}
typedef struct pair {
int *p;//or index
int sum;
} Pair;
int cmp(const void *a, const void *b){
Pair const *x = a;
Pair const *y = b;
return (x->sum > y->sum) - (x->sum < y->sum);
}
int main(void){
int i, j, row, column, **array;
row = 3; column = 3;
array = (int**) malloc (row * sizeof(*array));//cast of (int**) is redundant.
array[0] = (int []){1, 2, 2};
array[1] = (int []){2, 9, 9};
array[2] = (int []){2, 1, 6};
Pair *temp = malloc(row * sizeof(*temp));
for(i = 0; i < row; ++i){
temp[i].p = array[i];
temp[i].sum = sum(column, array[i]);
}
qsort(temp, row, sizeof(*temp), cmp);
for (i=0; i<row;i++){
for (j=0; j<column;j++)
printf("%d ", temp[i].p[j] );
printf ("\n");
}
free(temp);
free(array);
return 0;
}
Related
I am trying to create a program to find the transpose of a matrix my dynamic memory allocation. However, while entering the elements of the matrix I can't input more than one element, its only taking the a[0][0] as the input and the program is ending after that.
#include <stdio.h>
#include <stdlib.h>
void createMatrix(int **, int, int);
void inputElements(int **, int, int);
void transpose(int **, int **, int, int);
void display(int **, int, int);
void main()
{
int **matrix, **trans, rows, cols;
printf("\nEnter number of rows in the matrix: ");
scanf("%d", &rows);
printf("\nEnter number of columns in the matrix: ");
scanf("%d", &cols);
createMatrix(matrix, rows, cols);
createMatrix(trans, cols, rows);
inputElements(matrix, rows, cols);
transpose(matrix, trans, rows, cols);
printf("\nMATRIX:\n");
display(matrix, rows, cols);
printf("\nTRANSPOSE OF THE MATRIX:\n");
display(trans, rows, cols);
}
void createMatrix(int **a, int r, int c) //for allocating memory for the matrix
{
int i, j;
a = (int **)malloc(sizeof(int *) * r);
for(i = 0; i < r; i++)
a[i] = (int *)malloc(sizeof(int) * c);
}
void inputElements(int **a, int r, int c) //for entering matrix elements
{
int i, j, t;
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("\nEnter matrix element[%d][%d]: ", i + 1, j + 1);
fflush(stdin);
getchar();
scanf("%d", &(a[i][j]));
}
}
}
void transpose(int **a, int **t, int r, int c) //for finding out the transpose of the matrix
{
int i, j;
for (i = 0; i < c; i++)
{
for (j = 0; j < r; j++)
t[i][j] = a[j][i];
}
}
void display(int **a, int r, int c) //for displaying the matrix
{
int i, j;
for (i = 0; i < r; i++)
{
printf("\n");
for (j = 0; j < c; j++)
printf("\t%d", a[i][j]);
}
}
There are many posts about scanf in loops I've seen, but I wasn't able to connect my issue with any of them.
I am working on a problem where I have to transpose a matrix. I am passing the address of the original matrix, but once I execute the function it does not change!
I have tried to add a * infront of matrix in the transpose function, thinking that it will be pointing to the whole 2d array, but it did not work.
#include<stdio.h>
#include <stdlib.h>
void transpose(int *r,int *c, int **matrix);
void printMatrix(int r,int c, int **matrix){
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
printf("%2d ",matrix[i][j]);
printf("\n");
}
}
int main() {
int **matrix;
int r =3;
int c =2;
matrix = (int**) malloc(r*sizeof(int*));
for(int i=0;i<r;i++)
matrix[i] = (int*) malloc(c*sizeof(int));
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
matrix[i][j] = (3*i+2*j)%8+1;
}
printf("Before transpose:\n");
printMatrix(r,c,matrix);
transpose(&r, &c ,matrix);
printMatrix(r,c,matrix);
return 0;
}
void transpose(int *r,int *c, int **matrix){
int newR = *c;
int newC = *r;
int **newMatrix;
newMatrix = (int**) malloc(newR*(sizeof(int*)));
for(int i=0; i<newR;i++)
newMatrix[i] = (int*) malloc(newC*(sizeof(int)));
for(int i=0; i<newR; i++)
for(int j=0;j<newC;j++)
newMatrix[i][j] = matrix[j][i];
*c = newC;
*r = newR;
matrix = (int**) malloc((*r)*sizeof(int*));
for(int i=0;i<*r;i++)
matrix[i] = (int*) malloc((*c)*sizeof(int));
for(int i=0; i<newR; i++){
for(int j=0;j<newC;j++){
matrix[i][j] = newMatrix[i][j];
}
printf("\n");
}
}
I have this matrix
1 3
4 6
7 1
and want to get
1 4 7
3 6 1
however I am getting
1 3 0
1 4 0
It looks like all you forgot to do was actually use the transposed matrix. All I did was change the function signature and return the matrix you had already allocated and manipulated, and I got the output you were looking for.
#include <stdio.h>
#include <stdlib.h>
int** transpose(int *r,int *c, int **matrix);
void printMatrix(int r, int c, int **matrix) {
for (size_t i = 0; i < r; ++i){
for (size_t j = 0; j < c; ++j) {
printf("%2d ",matrix[i][j]);
}
printf("\n");
}
}
int main()
{
int r = 3;
int c = 2;
int **matrix = calloc(sizeof(int*), r);
for (size_t i = 0; i < r; ++i) {
matrix[i] = calloc(sizeof(int), c);
}
for (size_t i = 0; i < r; ++i) {
for (size_t j = 0; j < c; ++j) {
matrix[i][j] = (3 * i + 2 * j) % 8 + 1;
}
}
printf("Before transpose:\n");
printMatrix(r, c, matrix);
int** newMatrix = transpose(&r, &c ,matrix);
printMatrix(r, c, newMatrix);
return EXIT_SUCCESS;
}
int** transpose(int *r, int *c, int **matrix) {
int newR = *c;
int newC = *r;
int **newMatrix = calloc((sizeof(int*)), newR);
for (size_t i = 0; i < newR; ++i) {
newMatrix[i] = (int*) malloc(newC*(sizeof(int)));
}
for (size_t i = 0; i < newR; ++i) {
for (size_t j = 0; j < newC; ++j) {
newMatrix[i][j] = matrix[j][i];
}
}
*c = newC;
*r = newR;
matrix = calloc(sizeof(int*), *r);
for (size_t i = 0; i < *r; ++i) {
matrix[i] = calloc(sizeof(int), *c);
}
for (size_t i = 0; i < newR; ++i) {
for (size_t j = 0; j < newC; ++j) {
matrix[i][j] = newMatrix[i][j];
}
printf("\n");
}
return newMatrix;
}
Output:
1 4 7
3 6 1
I changed a few things, especially because I prefer using calloc over malloc, since it zeroes out the newly-allocated memory, and there is a dedicated parameter for the size of the requested memory, which I think semantically is a better idea.
As a side note, you don't have to cast the result of malloc in C. I tend to feel more strongly about that than other people, I think, because code noise, especially when you're working in C is one of the worst things you can do to yourself. This is a pretty good example of that, since all I did was reformat your code and the answer was right there. Don't be stingy with the whitespace, either; it really does make a difference.
Anyways, I hope this helped somewhat, even if you literally had basically everything done.
#include<stdio.h>
#include <stdlib.h>
void transpose(int *r,int *c, int ***matrix);
void printMatrix(int r,int c, int **matrix){
int i=0,j=0;
for(i=0;i<r;i++){
for(j=0;j<c;j++)
printf("%2d ",matrix[i][j]);
printf("\n");
}
}
int main() {
int **matrix;
int r =3;
int c =2;
int i=0,j=0;
matrix = (int**) malloc(r*sizeof(int*));
for(i=0;i<r;i++)
matrix[i] = (int*) malloc(c*sizeof(int));
for(i=0;i<r;i++){
for(j=0;j<c;j++)
matrix[i][j] = (3*i+2*j)%8+1;
}
printf("Before transpose:\n");
printMatrix(r,c,matrix);
transpose(&r, &c, &matrix);
printMatrix(r,c,matrix);
return 0;
}
void transpose(int *r,int *c, int ***matrix){
int newR = *c;
int newC = *r;
int **newMatrix;
int i=0,j=0;
newMatrix = (int**) malloc(newR*(sizeof(int*)));
for(i=0; i<newR;i++)
newMatrix[i] = (int*) malloc(newC*(sizeof(int)));
for(i=0; i<newR; i++)
for(j=0;j<newC;j++) {
newMatrix[i][j] = (*matrix)[j][i];
}
*c = newC;
*r = newR;
// free matrix..
*matrix = newMatrix;
/*matrix = (int**) malloc((*r)*sizeof(int*));
for(i=0;i<*r;i++)
matrix[i] = (int*) malloc((*c)*sizeof(int));
for(i=0; i<newR; i++){
for(j=0;j<newC;j++){
matrix[i][j] = newMatrix[i][j];
}
printf("\n");
}*/
}
The rest of my functions work fabulously, however the last function has my goat. The goal of this function is to use pointers to obtain the values of two different arrays and add those values to a third array. However, when I run the main method to make the function run, it pauses for a second and provides a wedge exit code that does not work.
I've tried removing the if((sizeof(*ptr1)) == (sizeof(*ptr2)){
---insert code here---
}
from the for loop, however, the problem seems to be just the for loop itself.
//===================================Broken Code========================================
#include <stdio.h>
#define MAXIMUM 1000
int sumArrays(int arr1[], int arr2[]);
int addArrays(int arr1[], int arr2[]);
int main()
{
int arrayOne[MAXIMUM];
int arrayTwo[MAXIMUM];
for(int i = 0; i <= MAXIMUM; i++)
arrayOne[i] = i;
printf("Arrayone %d\n", arrayOne);
for(int j = 0; j <= MAXIMUM; j++)
arrayTwo[j] = j;
printf("ArrayTwo %d\n", arrayTwo);
printf(" The sum of the arrays is : %d\n",sumArrays(arrayOne, arrayTwo));
printf("%d", addArrays(arrayOne, arrayTwo));
return 0;
}
int sumArrays(int arr1[],int arr2[]){
int *ptr_1;
int *ptr_2;
ptr_1 = &arr1[0];
ptr_2 = &arr2[0];
int sum;
for(int i = 0; i < MAXIMUM; i++){
sum += *ptr_1 + i;
sum += *ptr_2 + i;
}
return sum;
}
int addArrays(int arr1[],int arr2[]){
int *ptr1 = &arr1[0];
int *ptr2 = &arr2[0];
int sum = 0;
int i = 0;
int arr3[0];
if(sizeof(*ptr1) == sizeof(*ptr2)){
for(int i = 0; i < MAXIMUM; i++){
sum += *ptr1 +i;
sum += *ptr2 +i;
arr3[i] = sum;
}
}
printf("The value of array3 is %d", arr3);
}
The other function works perfectly, but the addArrays function does a wedge exit and doesn't cooperate.
I expect the addArrays function to take the elements from each array, add them together and assign them to the third array.
Thank you for your time.
UPDATE: WORKING CODE
#include <stdio.h>
#define MAXIMUM 1000
#define ARRAY_SZ(x) (sizeof(x) / sizeof((x)[0]))
int sumArrays(int arr1[], int arr2[], size_t len);
int addArrays(int arr1[], int arr2[], int arr3[], size_t len);
int main()
{
int arrayOne[MAXIMUM];
int arrayTwo[MAXIMUM];
int arrayThree[MAXIMUM];
for(int i = 0; i <= MAXIMUM; i++)
arrayOne[i] = i;
printf("Array One %d\n", ARRAY_SZ(arrayOne));
for(int j = 0; j <= MAXIMUM; j++)
arrayTwo[j] = j;
printf("Array Two %d\n", ARRAY_SZ(arrayTwo));
printf(" The sum of the arrays is : %d\n",sumArrays(arrayOne, arrayTwo, ARRAY_SZ(arrayOne)));
printf("%d", addArrays(arrayOne, arrayTwo, arrayThree, MAXIMUM));
return 0;
}
int sumArrays(int arr1[],int arr2[], size_t len){
int *ptr_1;
int *ptr_2;
ptr_1 = &arr1[0];
ptr_2 = &arr2[0];
int sum = 0 ;
for(int i = 0; i < len; i++){
sum += *ptr_1++;
sum += *ptr_2++;
}
return sum;
}
int addArrays(int arr1[],int arr2[], int result[], size_t len){
int *ptr1 = &arr1[0];
int *ptr2 = &arr2[0];
int *ptr3 = &result[0];
int sum = 0;
int sum2 = 0;
int i = 0;
for(int i = 0; i < MAXIMUM; i++){
sum = *ptr1 ++;
sum += *ptr2 ++;
result[i] = sum;
printf("The result of array 3 is %d\n", *ptr3++);
}
}
Here are some notes:
When you assign/pass/print the and array using the name of the array, you are actually passing the memory location of the first element in the array (a pointer).So when you write:
printf("Arrayone %d\n", arrayOne);
You will see the memory address of the first element of the array being printed. If you would like to print the entire array you will need to loop through it. In this case you would be printing 1000 integers which might be undesirable.
void printArray(int * array, size_t len)
{
while(len--)
{
printf("%d ", *array++);
}
}
To get the number of elements in an array you can do something like this:
sizeof(arrayOne) / sizeof(arrayOne[0])
and you can put it in a macro like this:
#define ARRAY_SZ(x) (sizeof(x) / sizeof((x)[0]))
and call it like this:
ARRAY_SZ(arrayOne);
You cannot get the array size if you are receiving an array in a function (it has decayed to a pointer), instead you should pass the array size to the function too. Here because you initialize the arrays with the size MAXIMUM we don't actually need to calculate the array size, but we can just to show it works.
If you want to return an array (like in addArrays()) you should create an empty array and pass it to the function, then the function can update the array with the result.
When looping through an array you never want to do array[maximum] because the array indices range from 0 to maximum - 1
#include <stdio.h>
#define MAXIMUM 1000
#define ARRAY_SZ(x) (sizeof(x) / sizeof((x)[0]))
int sumArrays(int arr1[], int arr2[]);
int addArrays(int arr1[], int arr2[]);
int main()
{
int arrayOne[MAXIMUM];
int arrayTwo[MAXIMUM];
int arrayThree[MAXIMUM];
for(int i = 0; i < MAXIMUM; i++)
arrayOne[i] = i;
printf("Array one size %d\n", ARRAY_SZ(arrayOne));
for(int j = 0; j < MAXIMUM; j++)
arrayTwo[j] = j;
printf("Array Two size %d\n", ARRAY_SZ(arrayTwo));
printf(" The sum of the arrays is : %d\n",sumArrays(arrayOne, arrayTwo, ARRAY_SZ(arrayOne)));
addArrays(arrayOne, arrayTwo, arrayThree, MAXIMUM);
return 0;
}
int sumArrays(int arr1[],int arr2[], size_t len)
{
int *ptr_1;
int *ptr_2;
ptr_1 = &arr1[0];
ptr_2 = &arr2[0];
int sum;
for(int i = 0; i < len; i++){
sum += *ptr_1 + i;
sum += *ptr_2 + i;
}
return sum;
}
void addArrays(int arr1[], int arr2[], int result[], size_t len){
int *ptr1 = arr1;
int *ptr2 = arr2;
int sum = 0;
int i = 0;
for(int i = 0; i < len; i++){
sum = *ptr1 +i;
sum += *ptr2 +i;
result[i] = sum;
}
}
I don't have a lot of experience with pointers, but I want to try to make an array of pointers, each pointer pointing to a scanned string.
For example, you first input how many strings you want to scan (for example 5), and then I want to scan those strings and make an array of 5 pointers that point to those strings.
Because I didn't have a lot experience with something like this, I first tried it with normal arrays instead of strings, what I got is this:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<assert.h>
int **array(int m, int n) {
int i, j;
int **array = malloc(n*sizeof(int*));
for (j=0; j<m; j++) {
for (i=0; i<n; i++) {
array[i]=malloc(m * sizeof(int));
scanf("%d", &array[j][i]);
printf("array[%d][%d] is scanned and has value %d\n", j, i, array[j][i]);
}
}
return array;
}
int main(int argc, char*argv[]){
int m, n, *p, k;
scanf("%d %d", &m, &n);
printf("m is %d and n is %d\n", m, n);
p=*array(m, n);
printf("the numbers are:\n");
for (k=0; k<m*n; k++) {
printf("%d\n", p[k]);
}
return 0;
}
But here it's already going wrong, and I don't know why...
At the last printf, I always get wrong numbers, 0's and 17's...
Can someone explain me why this is and what I'm doing wrong? I think it's something with the returning of the array but I'm not sure..
If someone could explain this to me it would be great.
The problem with your code is the following:
// m = 3, n = 5
// array = ptr1, ptr2, ptr3, ptr4, ptr5
// | |
// 3 ints |
// 3 ints ..
int **array(int m, int n) {
int i, j;
int **array = (int**)malloc(n*sizeof(int*));
for (j=0; j<m; j++) {
for (i=0; i<n; i++) {
array[i]=(int*)malloc(m * sizeof(int));
scanf("%d", &array[j][i]);
printf("array[%d][%d] is scanned and has value %d\n", j, i, array[j][i]);
}
}
return array;
}
In the above example (m=3, n=5) you allocated 5 pointers to integers and then tried to populate them by allocating memory at each iteration in the inner-loop (WRONG). If you allocate new memory at each iteration, you're gonna lose the pointer to the previously allocated memory and the data you stored!
Plus the indices seem to be wrong for the inner and outer loop, a correct version of your code is:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<assert.h>
// 3, 5
// array = ptr1, ptr2, ptr3, ptr4, ptr5
// | |
// 3 ints |
// 3 ints ..
int **array(int m, int n) {
int i, j, index;
int **array = (int**)malloc(n*sizeof(int*));
index = 0;
for (j=0; j<n; j++) {
array[j]=(int*)malloc(m * sizeof(int)); // Allocate once per each j
for (i=0; i<m; i++) {
array[j][i] = index++;
printf("array[%d][%d] is scanned and has value %d\n", j, i, array[j][i]);
}
}
return array;
}
int main(int argc, char*argv[]){
int m, n, **p, k, i, j;
m = 3;
n = 5;
printf("m is %d and n is %d\n", m, n);
p=array(m, n);
printf("the numbers are:\n");
for (j=0; j<n; j++)
for(i=0; i<m; i++)
printf("%d\n", p[j][i]);
return 0;
}
And the above version is STILL NOT CORRECT : You need to free the allocated memory!
I'll leave that as an exercise.. hint: you CAN'T simply do "free(p);" :]
Are you sure about this for loop? If you've the malloc inside the inner loop you're not creating a matrix because every time you override the same cells...
int i, j;
int **array = malloc(n*sizeof(int*));
for (j=0; j<m; j++) {
for (i=0; i<n; i++) {
array[i]=malloc(m * sizeof(int));
scanf("%d", &array[j][i]);
printf("array[%d][%d] is scanned and has value %d\n", j, i, array[j][i]);
}
}
It should be something like:
int i, j;
int **array = malloc(n*sizeof(int*));
for (i=0; i<n; i++) {
array[i]=malloc(m * sizeof(int));
for (j=0; j<m; j++) {
scanf("%d", &array[i][j]);
printf("array[%d][%d] is scanned and has value %d\n", i, j, array[i][j]);
}
}
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<assert.h>
void *array(int m, int n) {
int i, j;
int (*array)[n] = malloc(sizeof(int [m][n]));//m*n*sizeof(int)
for (j=0; j<m; j++) {
for (i=0; i<n; i++) {
scanf("%d", &array[j][i]);
printf("array[%d][%d] is scanned and has value %d\n", j, i, array[j][i]);
}
}
return array;
}
int main(int argc, char*argv[]){
int m, n, *p, k;
scanf("%d %d", &m, &n);
printf("m is %d and n is %d\n", m, n);
p=(int*)array(m, n);
printf("the numbers are:\n");
for (k=0; k<m*n; k++) {
printf("%d\n", p[k]);
}
return 0;
}
#include <stdlib.h>
#include <stdio.h>
char **array(int m, int n) {
int i;
char **array = malloc(m*sizeof(char*));
for (i=0; i<m; ++i) {
array[i] = malloc(n*sizeof(char));//Fixed length : like char array[m][n] (char *array[m])
scanf("%s", array[i]);//!! There is no length constraints.
printf("array[%d] is scanned and has value %s\n", i, array[i]);
}
return array;
}
int main(int argc, char*argv[]){
int m, n, k;
char **p;
scanf("%d %d", &m, &n);
printf("m is %d and n is %d\n", m, n);
p=array(m, n);
printf("the string are:\n");
for (k=0; k<m; ++k) {
printf("%s\n", p[k]);
}
return 0;
}
I'm not sure if I do this smart, but I usually allocate the pointer array and then allocate the whole memory chunk to the first item. Then I get continuous memory for the data. Like:
_array = (float**) malloc( n * sizeof ( float * ));
assert ( _array != NULL );
_array[0] = (float*) malloc( n * m * sizeof ( float ));
assert ( _array[0] != NULL );
for ( idx = 0; idx < n; idx++ )
_array[ idx ] = _array[ 0 ] + idx * m;
(float instead of int in my case. And please don't comment on the return of malloc casting, nor on the silly user of assert())
#include "stdio.h"
void main(){
int a[2][2]={1, 2, 3, 4};
int a[2][2]={1, 2, 3, 4};
display(a, 2, 2);
show(a, 2, 2);}
}
display(int *k, int r, int c){
int i, j, *z;
for(i = 0; i < r; i++){
z = k + i;
printf("Display\n");
for(j = 0; j < c; j++){
printf("%d", *(z + j));
}
}
}
show(int *q, int ro, int co){
int i, j;
for(i = 0; i < ro; i++){
printf("\n");
for(j = 0; j < co; j++){
printf("%d", *(q + i*co + j));
}
}
}
Output:
Display
12
23
Show
12
34
Why Display() is not showing 1223 while show() gives 1234? Both uses the same logic to display the 2d array. Can any one help please?
In display you are using two counters, i for rows and j for columns. Since the array is laid out sequentially in memory you need to increase i by the size of a column, i.e. c, each time you want to move from one row to the next. So, you should add i*c to k, not i.
The complete function:
display(int *k,int r,int c){
int i,j,*z;
for(i=0;i<r;i++){
z=k+i*c;
printf("Display\n");
for(j=0;j<c;j++){
printf("%d",*(z+j));
}
}
}
To access 2D array using pointers:
#define R 2
#define C 2
...
int A[R][C]={1, 2, 3, 4};
for(i=0;i<R;i++)
for(j=0;j<C;j++)
printf("%d ",*(*(A+i)+j));
...