C++中怎么将一维数组输出为二维数组

Tlw_999 2016-10-18 06:09:36
比如:在C++中,有一个数组a[]={1,2,3,4,5.......100};
现在我想将其输出为一个固定长度的二维数组,比如5×20的
b[1][20]={1,2,3,4,5.....20}
b[2][20]={21,22,23,24,....30}
.....
b[5][20]={81,82,83,84......100}

请问用C++语言怎么实现,我是个小白,请各位大神帮帮我,比较急。非常感谢。
...全文
1292 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-10-29
  • 打赏
  • 举报
回复
仅供参考:
//输出PROG中有但LIST中没有的文本行,即集合PROG-LIST
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <search.h>
#define MAXCHARS 512
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
char PROG[256]="PROG";//程序Program需要的文件列表
char LIST[256]="LIST";//dir /b /s生成的实际文件列表List
FILE *fp,*fl;
int i,c,n,L,hh;
int ignore_case=0;
char ln[MAXCHARS];
int icompare(const void *arg1,const void *arg2) {
   return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
   return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
    if (argc>1) strcpy(PROG,argv[1]);//命令行参数1覆盖PROG
    if (argc>2) strcpy(LIST,argv[2]);//命令行参数2覆盖LIST
    if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
    if ((fl=fopen(LIST,"rt"))==NULL) {
        fprintf(stderr,"Can not open %s\n",LIST);
        fprintf(stderr,"Usage: %s [PROG] [LIST] [-i]\n",argv[0]);
        return 1;
    }
    if ((fp=fopen(PROG,"rt"))==NULL) {
        fclose(fl);
        fprintf(stderr,"Can not open %s\n",PROG);
        fprintf(stderr,"Usage: %s [PROG] [LIST] [-i]\n",argv[0]);
        return 2;
    }
    buf=(char *)malloc(MAXLINES*MAXCHARS);
    if (NULL==buf) {
        fclose(fl);
        fclose(fp);
        fprintf(stderr,"Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
        return 4;
    }
    n=0;
    hh=0;
    i=0;
    while (1) {
        if (fgets(ln,MAXCHARS,fl)==NULL) break;//
        hh++;
        L=strlen(ln)-1;
        if ('\n'!=ln[L]) {//超长行忽略后面内容
            fprintf(stderr,"%s Line %d too long(>%d),spilth ignored.\n",LIST,hh,MAXCHARS);
            while (1) {
                c=fgetc(fl);
                if ('\n'==c || EOF==c) break;//
            }
        }
        while (1) {//去掉行尾的'\n'和空格
            if ('\n'==ln[L] || ' '==ln[L]) {
                ln[L]=0;
                L--;
                if (L<0) break;//
            } else break;//
        }
        if (L>=0) {
            strcpy(buf+i,ln);i+=MAXCHARS;
            n++;
            if (n>=MAXLINES) {
                MAXLINES2=MAXLINES*2;
                if (MAXLINES2==1280000) MAXLINES2=2500000;
                buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);
                if (NULL==buf2) {
                    free(buf);
                    fclose(fl);
                    fclose(fp);
                    fprintf(stderr,"Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);
                    return 5;
                }
                buf=buf2;
                MAXLINES=MAXLINES2;
            }
        }
    }
    fclose(fl);
    if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
    else qsort(buf,n,MAXCHARS,compare);
    hh=0;
    while (1) {
        if (fgets(ln,MAXCHARS,fp)==NULL) break;//
        hh++;
        L=strlen(ln)-1;
        if ('\n'!=ln[L]) {//超长行忽略后面内容
            fprintf(stderr,"%s Line %d too long(>%d),spilth ignored.\n",PROG,hh,MAXCHARS);
            while (1) {
                c=fgetc(fp);
                if ('\n'==c || EOF==c) break;//
            }
        }
        while (1) {//去掉行尾的'\n'和空格
            if ('\n'==ln[L] || ' '==ln[L]) {
                ln[L]=0;
                L--;
                if (L<0) break;//
            } else break;//
        }
        if (L>=0) {
            if (ignore_case) {
                if (NULL==bsearch(ln,buf,n,MAXCHARS,icompare)) printf("%s\n",ln);
            } else {
                if (NULL==bsearch(ln,buf,n,MAXCHARS,compare)) printf("%s\n",ln);
            }
        }
    }
    fclose(fp);
    free(buf);
    return 0;
}
ID870177103 2016-10-29
  • 打赏
  • 举报
回复
int main () {
	int a[100] ;
	for (int i = 0 ; i < 100 ; i++)
		a[i] = i ;
	//动态计算
	for (int i = 0 ; i < 5 ; i++) {
		for (int j = 0 ; j < 20 ; j++)
			cout << a[i * 20 + j] << " " ;
		cout << endl ;
	}
 	return 0 ;
}
ID870177103 2016-10-29
  • 打赏
  • 举报
回复
int main () {
	int a[100] ;
	for (int i = 0 ; i < 100 ; i++)
		a[i] = i ;
	//静态绑定
	int (&b)[5][20] = reinterpret_cast<int (&)[5][20]> (a) ;
	for (int i = 0 ; i < 5 ; i++) {
		for (int j = 0 ; j < 20 ; j++)
			cout << b[i][j] << " " ;
		cout << endl ;
	}
 	return 0 ;
}
  • 打赏
  • 举报
回复
#include <iostream> using namespace std; int main() { int a[100]; for (int i = 0; i < 100; i++) a[i] = i + 1; for (int i = 0; i < 100; i++) { if( i%20 == 0 ) cout << endl << a[i] << " "; else cout << a[i] << " "; } return 0; }
lin5161678 2016-10-19
  • 打赏
  • 举报
回复
输出???? 输出20个数输出一个\n
赵4老师 2016-10-19
  • 打赏
  • 举报
回复
小灸舞 2016-10-19
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

int main()
{
int a[100];
for (int i = 0; i < 100; i++)
a[i] = i + 1;
int *b[5] = {&a[0], &a[20], &a[40], &a[60], &a[80]};

for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 20; j++)
cout << b[i][j] << " ";
cout << endl;
}

return 0;
}

paschen 版主 2016-10-18
  • 打赏
  • 举报
回复
引用 2 楼 Tlw_999 的回复:
需要代码,请详细一点好吗,谢谢!

	int a[100] = {1,2,3,4,5,6};
	int* b[5];
	b[0] = &a[0];
	b[1] = &a[20];
	b[2] = &a[40];
	b[3] = &a[60];
	b[4] = &a[80];
Tlw_999 2016-10-18
  • 打赏
  • 举报
回复
需要代码,请详细一点好吗,谢谢!
ipqtjmqj 2016-10-18
  • 打赏
  • 举报
回复
c++请用std::vector

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧