白鸽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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到