如何输入二维数组的维数,然后实现输出

马小橙 2014-12-10 05:48:42
#include<iostream>
#include"windows.h"
using namespace std;
int mian()
{
int i,j,k=0,ival;
cout<<"输入行数,列数"<<endl;
cin>>i>>j;
/*将 i j转变成const int 类型*/
static_cast<const int> (i);
static_cast<const int> (j);
int a[i][j];
cout<<"依次输入矩阵的值"<<endl;
while(cin>>ival)
a[k++]=ival;
for(m=0;m<i;m++)
{
for(n=0;n<j;n++)
{
cout<<a[m][n]<<endl;
}
cout<<"\n"<<endl;
}
systems("pause");
return 0;

}
出现的错误:shuzu11.cpp(8): error C2065: 'j' : undeclared identifier
1>shuzu11.cpp(11): error C2065: 'j' : undeclared identifier
1>shuzu11.cpp(12): error C2057: expected constant expression
1>shuzu11.cpp(12): error C2466: cannot allocate an array of constant size 0
1>shuzu11.cpp(12): error C2065: 'j' : undeclared identifier
1>shuzu11.cpp(12): error C2133: 'a' : unknown size
1>shuzu11.cpp(15): error C2065: 'k' : undeclared identifier
1>shuzu11.cpp(16): error C2065: 'm' : undeclared identifier
1>shuzu11.cpp(16): error C2065: 'm' : undeclared identifier
1>shuzu11.cpp(16): error C2065: 'm' : undeclared identifier
1>shuzu11.cpp(18): error C2065: 'n' : undeclared identifier
1>shuzu11.cpp(18): error C2065: 'n' : undeclared identifier
1>shuzu11.cpp(18): error C2065: 'j' : undeclared identifier
1>shuzu11.cpp(18): error C2065: 'n' : undeclared identifier
1>shuzu11.cpp(20): error C2065: 'm' : undeclared identifier
1>shuzu11.cpp(20): error C2065: 'n' : undeclared identifier
1>shuzu11.cpp(24): error C3861: 'systems': identifier not found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.59
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
...全文
736 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-12-12
  • 打赏
  • 举报
回复
不要纠结各种常量了,这个世界上唯一不变的就是变化。用API WriteProcessMemory还能修改正运行的其它进程的内存里面的所谓常量呢!
马小橙 2014-12-12
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
仅供参考
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int **newarr2d(int rows,int cols) {
    int **p,i;

    p=(int **)malloc(rows*sizeof(int *));
    if (NULL==p) exit(1);
    for (i=0;i<rows;i++) {
        p[i]=(int *)malloc(cols*sizeof(int));
        if (NULL==p[i]) exit(1);
    }
    return p;
}
void deletearr2d(int **p,int rows) {
    int i;

    for (i=0;i<rows;i++) {
        free(p[i]);
    }
    free(p);
}
int main() {
    int **arr2d,i,j,r,c;

    r=4;
    c=5;
    //在堆中开辟一个4×5的二维int数组
    arr2d=newarr2d(r,c);
    for (i=0;i<r;i++) {
        for (j=0;j<c;j++) {
            arr2d[i][j]=i*c+j;
        }
    }
    for (i=0;i<r;i++) {
        for (j=0;j<c;j++) {
            printf(" %2d",arr2d[i][j]);
        }
        printf("\n");
    }
    deletearr2d(arr2d,r);

    r=6;
    c=3;
    //在堆中开辟一个6×3的二维int数组
    arr2d=newarr2d(r,c);
    for (i=0;i<r;i++) {
        for (j=0;j<c;j++) {
            arr2d[i][j]=i*c+j;
        }
    }
    for (i=0;i<r;i++) {
        for (j=0;j<c;j++) {
            printf(" %2d",arr2d[i][j]);
        }
        printf("\n");
    }
    deletearr2d(arr2d,r);

    return 0;
}
//  0  1  2  3  4
//  5  6  7  8  9
// 10 11 12 13 14
// 15 16 17 18 19
//  0  1  2
//  3  4  5
//  6  7  8
//  9 10 11
// 12 13 14
// 15 16 17
//
怎么把整数转变成常整数???
赵4老师 2014-12-11
  • 打赏
  • 举报
回复
仅供参考
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int **newarr2d(int rows,int cols) {
    int **p,i;

    p=(int **)malloc(rows*sizeof(int *));
    if (NULL==p) exit(1);
    for (i=0;i<rows;i++) {
        p[i]=(int *)malloc(cols*sizeof(int));
        if (NULL==p[i]) exit(1);
    }
    return p;
}
void deletearr2d(int **p,int rows) {
    int i;

    for (i=0;i<rows;i++) {
        free(p[i]);
    }
    free(p);
}
int main() {
    int **arr2d,i,j,r,c;

    r=4;
    c=5;
    //在堆中开辟一个4×5的二维int数组
    arr2d=newarr2d(r,c);
    for (i=0;i<r;i++) {
        for (j=0;j<c;j++) {
            arr2d[i][j]=i*c+j;
        }
    }
    for (i=0;i<r;i++) {
        for (j=0;j<c;j++) {
            printf(" %2d",arr2d[i][j]);
        }
        printf("\n");
    }
    deletearr2d(arr2d,r);

    r=6;
    c=3;
    //在堆中开辟一个6×3的二维int数组
    arr2d=newarr2d(r,c);
    for (i=0;i<r;i++) {
        for (j=0;j<c;j++) {
            arr2d[i][j]=i*c+j;
        }
    }
    for (i=0;i<r;i++) {
        for (j=0;j<c;j++) {
            printf(" %2d",arr2d[i][j]);
        }
        printf("\n");
    }
    deletearr2d(arr2d,r);

    return 0;
}
//  0  1  2  3  4
//  5  6  7  8  9
// 10 11 12 13 14
// 15 16 17 18 19
//  0  1  2
//  3  4  5
//  6  7  8
//  9 10 11
// 12 13 14
// 15 16 17
//
jacksonfan 2014-12-11
  • 打赏
  • 举报
回复
我勒个去 错误真不少,自己编译看看 除了楼上说的还有 1、a[k++]=ival;//定义的是二维的 2、2个for循环变量没有定义,int m; 3、system不是systems
zacharyLiu 2014-12-11
  • 打赏
  • 举报
回复

int main( )//不是  int mian( )
是不是吃面吃多了?
cutmelon 2014-12-10
  • 打赏
  • 举报
回复
还有这行 int a[i][j]; 这样写能行吗。。。new一下吧
cutmelon 2014-12-10
  • 打赏
  • 举报
回复
你好好看看这行代码 int i,j,k=0,ival; 用j前面的分号替换它后面的分号就行了

64,637

社区成员

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

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