Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib>
- #include <stdio.h>
- int** init_2darray(int numOfRow, int numOfCol) {
- int i, j;
- int** array = (int**) malloc (sizeof(int*)*numOfRow);
- if (array==NULL)
- return NULL;
- for (i=0;i<numOfRow;i++) {
- array[i] = (int*) calloc (numOfCol,sizeof(int));
- if (array[i]==NULL) {
- for (j=0;j<i;j++)
- free(array[j]);
- free(array);
- return NULL;
- }
- }
- return array;
- }
- void free_2darray(int** array, int numOfRow) {
- int i;
- for (i=0;i<numOfRow;i++)
- free(array[i]);
- free(array);
- }
- void print_2darray(int** array, int numOfRow, int numOfCol) {
- int i, j;
- for (i=0;i<numOfRow;i++) {
- for (j=0;j<numOfCol;j++)
- printf("%10d",array[i][j]);
- printf("\n");
- }
- }
- int** add_to_2darray(int** array, int* numOfRow, int* numOfCol, int i, int j, int value)
- {
- int m,n;
- if(i>=*numOfRow)
- {
- array=(int**)realloc(array,(i+1)*sizeof(int*));
- for(m=*numOfRow;m<i+1;m++)
- array[m]=(int*)calloc(*numOfCol,sizeof(int));
- *numOfRow=i+1
- }
- if(j>=*numOfCol) {
- for(m=0;m<*numOfRow;m++)
- {
- array[m]=(int*)realloc(array[m],(j+1)*sizeof(int));
- for(n=*numOfCol;n<=j;n++)
- array[m][n]=0; }
- }
- array[i][j]=value;
- return array;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement