0

in my problem i have to enter the components of matrix in 2D array
i.e

input:
1 5 6
3 9 10
8 23 15


output:
1 3 5
6 8 9
0 15 23

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Aia
0

Well there are several methods , but I would go with the simplest...

Assume that you take the dimension of nxn martix as the input from the user where user feeds in value of n. And array[][] is the one in which he feeds in the value.

Just have another integer pointer as

int *ptr;

After the user feeds in the value of n you can allocate a linear array as

ptr=(int *)malloc(n*n*sizeof(int));

Use this array to sort your elements out using the pointer itself as index to the array as:

*(ptr+i)

or even as:

ptr[i]

Once you are done wit sorting then you can just free the memory as

free(ptr);

This would be just like using a temporary array for sorting.

Hope this helps... :)

0
ptr=(int *)malloc(n*n*sizeof(int));

Casting malloc is a residue practice from the times when the C programming language didn't support void type. It is best not to cast malloc since doing it can hide errors. #include <stdlib.h>

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.