动态生成二维数组

来源:互联网 发布:淘宝网的开店流程 编辑:程序博客网 时间:2024/06/11 14:57

         开始着手写PCA算法,这是机器学习一个简单的算法。当自己动手写某一种算法时,才发现里面更多的问题,才收获更多。自己的编程能力不是很强,只能从小处做起了,一步一步,踏踏实实。今天晚上写的程序是用来保存样本特征数目和数据数目的问题。

#include <stdio.h>#include <malloc.h>double** newArray(int m,int n){int i=0;int j=0;double **p=NULL;p = (double **)malloc(sizeof(double *) * m); //已为p分配内存,p不为NULLfor (i = 0; i < m; i++) *(p + i) = (double *)malloc(sizeof(double) * n);   printf("input data:\n");for (i = 0; i < m; i++) {    for(j = 0; j < n; j++)    {scanf("%lf",&p[i][j]);    } }return p;}void main(){int line = 0; int row = 0; int i=0;int j=0;double **q = NULL; printf("input the line of the array:"); scanf("%d", &line); printf("input the row of the array:"); scanf("%d", &row); q=newArray(line,row);for (i = 0; i < line; i++) {    for(j = 0; j < row; j++)    {    printf("%lf ", q[i][j]);    } printf("\n");} /*free every line point*/ for (i = 0; i < line; i++) {    free(*(q + i));    q[i] = NULL; }free(q); q = NULL; }

结果:

 

 

 

 

原创粉丝点击