This my attempt at creating a dynamic 2d array. I am able to store the values into the array and read them out. Is there a more elegant way of inputing the values?
instead of having to multiplying i*4.Code:array[(i*4)+j]=count++;
Code:#include<stdio.h> int main(void){ int *array; int column,row; int i,j,count; printf("Please Enter the dimensions of the array:\n"); printf("row="); scanf("%d",&row); printf("\n"); printf("column="); scanf("%d",&column); printf("\n"); printf("array[%d][%d]\n",row,column); array=(int *)malloc(row*column*sizeof(int)); count=0; for(i=0;i<row;i++){ for(j=0;j<column;j++) array[(i*4)+j]=count++; } for(i=0;i<row;i++){ for(j=0;j<column;j++) printf("%d ",array[(i*4)+j]); printf("\n"); } free(array); return 0; }



LinkBack URL
About LinkBacks



