白鸽9F 2022-06-15 16:25 采纳率: 100%
浏览 34
已结题

关于一维数组与二维数组的输入输出问题

关于一维数组与二维数组的输入输出问题
写了一个排序的代码但我发现了一个问题

#include <bits/stdc++.h>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
int maxn[100];
int n;
int c=0;
int a[100];
scanf("%d", &n);
for(int i=1;i<=n;i++){
scanf("%d", &a[i]);
}
for(int i=1;i<=n;i++){
maxn[i]=i;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n-1;j++){
if(a[maxn[j]]<a[maxn[j+1]]){
c=maxn[j];
maxn[j]=maxn[j+1];
maxn[j+1]=c;
}
for(int q=1;q<=n;q++){
cout<<a[maxn[q]]<<' ';
}
cout<<endl;
}
}
return 0;
}

上面的代码能正常输出但把啊a[x]换成a[1][x]后就输出不了

#include <bits/stdc++.h>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
int maxn[100];
int n;
int c=0;
int a[1][100];
scanf("%d", &n);
for(int i=1;i<=n;i++){
scanf("%d", &a[1][i]);
}
for(int i=1;i<=n;i++){
maxn[i]=i;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n-1;j++){
if(a[1][maxn[j]]<a[1][maxn[j+1]]){
c=maxn[j];
maxn[j]=maxn[j+1];
maxn[j+1]=c;
}
for(int q=1;q<=n;q++){
cout<<a[1][maxn[q]]<<' ';
}
cout<<endl;
}
}
return 0;
}

  • 写回答

3条回答 默认 最新

  • 暴风雨中的白杨 2022-06-15 16:46
    关注

    C中数组的下标是从0开始的

    img

    修改后的程序
    #include <bits/stdc++.h>
    using namespace std;
    
    
    int main() {
    int maxn[100];
    int n;
    int c=0;
    int a[1][100];
    scanf("%d", &n);
    for(int i=1;i<=n;i++){
        scanf("%d", &a[0][i]);
    }
    for(int i=1;i<=n;i++){
        maxn[i]=i;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n-1;j++){
            if(a[0][maxn[j]]<a[0][maxn[j+1]]){
                c=maxn[j];
                maxn[j]=maxn[j+1];
                maxn[j+1]=c;
            }
            for(int q=1;q<=n;q++){
                cout<<a[0][maxn[q]]<<' ';
            }
            cout<<endl;
        }
    }
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • HWsir 2022-06-15 16:35
    关注

    a[0][]

    评论
  • 树下等苹果 2022-06-15 16:47
    关注
    
    int a[1][100];
    

    该数组定义一个一行100列的矩阵,在C语言中,数组下标从0开始,所以第一行的下标为0,即a[0][i],表示第一行,a[1][i]表示第二行,a只有一行,所以a[1][i]会造成数组越界

    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月23日
  • 已采纳回答 6月15日
  • 创建了问题 6月15日

悬赏问题

  • ¥20 python 3des pyDes库
  • ¥15 关于#mysql#安装失败的问题。MySQL
  • ¥15 想问一下for循环计算表达式的方法,第一次接触
  • ¥15 如何在VA框架上面加功能,去读取框架内任何app数据功能
  • ¥15 关于#c语言#的问题:用c或c++写一个计算下列问题有关软件工程的代码并加上分析
  • ¥15 Zeppelin0.10.0版本升级lib包下的shiro-web
  • ¥15 链表入队的指针内存问题
  • ¥20 vba如何写本地html文件执行js
  • ¥15 VS2022的C#如何创建
  • ¥20 关于#用户注册#的问题,如何解决?