user_390 2020-08-15 10:42 采纳率: 0%
浏览 143
已采纳

c++遍历数组是,下标变量不能被赋值

问题:

找一个数组中最小元素时数组下表变量不能被赋值

注:InitList,InsertList,PrintValue函数正常;

#include <stdio.h>
#define MAXSIZE 10
typedef int ElemType;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct List{
    ElemType data[MAXSIZE];
    int length;
}sqList; 
void InitList(sqList &l);
bool InsertList(sqList &l,int i,ElemType e);
bool DeleteList(sqList &l,int i);

void PrintValue(sqList l);
int custom(sqList l){
    if(l.length=0){
        printf("空表\n");
    }
    int min=0;
    for(int i=0;i<l.length;i++){
        if(l.data[i]<l.data[i+1]){
            min=l.data[i];
        }
    } 
    return min;
}
int main() {
    sqList l;
    InitList(l);
    InsertList(l,1,3);
    InsertList(l,2,5);
    InsertList(l,3,2);
    InsertList(l,4,7);
    PrintValue(l);
    printf("%d",custom(l));
    return 0;
}
//init
void InitList(sqList &l){
    l.length=0;
}
//insert
bool InsertList(sqList &l,int i,ElemType e){
    if(i<1 || i>l.length+1){
        return false;
    }
    if(l.length>=MAXSIZE){
        return false; 
    }
    for(int j=l.length;j>=i-1;j--){
        l.data[j+1]=l.data[j];
    }
    l.data[i-1]=e;
    l.length++;
    return true;
}

//print
void PrintValue(sqList l){
    for(int i=0;i<l.length;i++){
        printf("%d\n",l.data[i]);
    }
}

图片说明

  • 写回答

1条回答 默认 最新

  • ex22333 2020-08-15 12:42
    关注

    第21行那里的i+1越界了吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探