【资料结构】读档相关 12/18更

二维阵列的一维读入法

#include <math.h>#include <stdio.h>#include <stdlib.h>#define MAX 50int main() {  FILE *f = fopen("new.txt", "r");  if (f == NULL) {    printf("no file");    return -1;  }
基本的读入判断,利用fopen将位置存入f,并判断档案是否存在
  int count=0, top = -1, arr[MAX] = {0};  while (EOF != fscanf(f, "%d", &arr[++top])) {  }  fclose(f);
持续读档,将读到的资料放入阵列中,直到没有资料传入为止
  count = sqrt(top);  printf("top=%d\n", top);  printf("now=%d\n", count);
此时top为矩阵元素总数量,利用math中的sqrt函式取top的开根号,可得边长(限同边长矩阵)
  for (int i = 0; i < count; i++) {    for (int j = 0; j < count; j++) {      printf("%d ", arr[i * count + j]);    }    printf("\n");  }}
利用一维的方式印出二维矩阵

其他

待补


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章