נשלח בתאריך: 25 April 2009 בשעה 14:01 | | IP רשוּם
|
|
|
|
include <stdio.h> #include <stdlib.h> #include <malloc.h>
typedef struct { int SUM; int I; int J; }list_type;
typedef struct list_element { list_type data; struct list_element *ptr_next,*ptr_prev; } list_element;
typedef struct { list_element *head, *tail; } list;
typedef list_element *position;
int **input_matrix (int r,int c); void create_list (list *pl); int insert_to_list (list_type x, position p,list *pl); position next (position p, list *pl); int retrieve (position p, list *pl, list_type *px); int num_of_equals (int **m, int r, int c);
void main () { int **M; int row,col,solution; printf ("enter size of matrix\n"); scanf_s ("%d%d",&row,&col); M=input_matrix (row,col); solution = num_of_equals (M,row,col); printf ("the size of the array created is: %d\n",solution); free (M); }
int **input_matrix (int r,int c) { int i,j; int **m; m = calloc (r,sizeof(int *)); if (!m) return 0; for (i=0;i<r;i++) { m[i]=calloc (c,sizeof(int)); if (!m[i]) return 0; } printf ("enter matrix %d X %d elements\n",r,c); for (i=0;i<r;i++) for (j=0;j<c;j++) scanf_s ("%d",&m[i][j]); return m; }
void create_list (list *pl) { pl->head = malloc(sizeof(list_element)); pl->tail = malloc(sizeof(list_element)); pl->head->ptr_next = pl->tail; pl->head->ptr_prev = NULL; pl->tail->ptr_prev = pl->head; pl->tail->ptr_next = NULL; }
int insert_to_list (list_type x, position p, list *pl) { position q; if (p != pl->tail) { q = malloc(sizeof(list_element)); if (!q) return 0; q->data = x; p->ptr_next->ptr_prev = q; q->ptr_next = p->ptr_next; p->ptr_next = q; q->ptr_prev = p; return 1; } else return insert_to_list (x, pl->head, pl); }
position next (position p, list *pl) { if (p != pl->tail) return p->ptr_next; else return pl->head->ptr_next; } int retrieve (position p, list *pl, list_type *px) { if (p != pl->tail) { *px = p->data; return 1; } return 0; }
int num_of_equals (int **m, int r, int c) { int i,j,counter=0; list *l=NULL; position p; list_type temp; list_type *a; create_list (l); p=l->head; for (i=0;i<r;i++) for (j=0;j<c;j++) if (m[i][j]==i+j) { temp.SUM=i+j; temp.I=i; temp.J=j; insert_to_list(temp,p,l); p=next(p,l); counter++; } a=calloc (counter,sizeof(list_type)); for (i=0;i<counter;i++) { retrieve (p,l,&a[i]); p=next(p,l); } return counter; }
|