c中的菜单驱动程序,用于对阵列执行各种操作

时间:2014-01-27 14:23:49

标签: c arrays

以下是我的c程序,它对数组执行各种操作。当我删除任何元素时,delete_ele函数删除并正确显示数组,但遍历函数显示一些垃圾值。例如。如果最初我有4个元素并删除第一个元素,则delete_ele函数将显示剩余的3个元素,但遍历函数显示4个元素(剩余的3个元素以及末尾的垃圾值)。请告诉我错误。

#include<stdio.h>
#include<conio.h>
void insert_ele(int i,int ar[],int len){
    int ele, loc;
    printf("\nEnter element to be inserted : ");
    scanf("%d",&ele);
    printf("\nEnter location : ");
    scanf("%d",&loc);
    if(i==19){
        printf("\nOverflow");
    }
    else{
        while(i>=loc){
            ar[i+1]=ar[i];
            i--;
        }
    ar[loc]=ele;
    len++;
    printf("\nNew Array : ");
    for(i=0;i<len;i++){
        printf("\t%d",ar[i]);
    }
    }
}
void delete_ele(int i,int ar[],int len){
    int loc;
    printf("\nEnter location of element to be deleted : ");
    scanf("%d",&loc);
    if(len==0){
        printf("\nUnderflow");
    }
    else{
        for(i=loc;i<len;i++){
            ar[i]=ar[i+1];
        }
        len--;
        printf("\nNew Array : ");
        for(i=0;i<len;i++){
        printf("\t%d",ar[i]);
        }
    }
}
void find_ele(int i,int ar[],int len){
    int ele, count=0;
    printf("\nEnter element to be searched : ");
    scanf("%d",&ele);
    for(i=0;i<len;i++){
        if(ar[i]==ele){
            count++;
        }
    }
    if(count == 0){
        printf("\nElement does not exist");
    }
    else{
        printf("\nElement found %d times",count);
    }
}
void traverse(int i, int ar[], int len){
    printf("\nTotal number of Elements are : %d",len);
    printf("\nElements are : ");
    for(i=0; i<len; i++){
        printf("\t%d",ar[i]);
    }
}
void main(){
    int ar[20], i=0, len=0, ch;
    char choice;
    clrscr();
    printf("\nEnter number of elements you want to insert : ");
    scanf("%d",&len);
    for(i=0; i<len; i++){
        printf("Enter element %d : ",i);
        scanf("%d",&ar[i]);
    }
    i--;
    label:
    printf("\nPress 1 to insert, 2 to delete, 3 to find any element");
    printf("\n or 4 to traverse the array : ");
    scanf("%d",&ch);
    switch(ch){
        case 1:insert_ele(i, ar, len);
        break;
        case 2:delete_ele(i, ar, len);
        break;
        case 3:find_ele(i, ar, len);
        break;
        case 4:traverse(i, ar, len);
        break;
        default:printf("\nInvalid Option");
        break;
    }
    printf("\nPress y to continue or any other key to quit : ");
    scanf("%s",&choice);
    if(choice=='y' || choice=='Y'){
        goto label;
    }
    getch();
}

3 个答案:

答案 0 :(得分:2)

因为您正在递减len

中的局部变量(delete_ele()

尝试:

void delete_ele(int i, int ar[], int *len){
    int loc;
    printf("\nEnter location of element to be deleted : ");
    scanf("%d", &loc);
    if(*len == 0){
        printf("\nUnderflow");
    }
    else{
        for(i=loc; i< (*len - 1); i++){
            ar[i] = ar[i + 1];
        }
        (*len)--;
        printf("\nNew Array : ");
        for(i=0; i<*len; i++){
           printf("\t%d", ar[i]);
        }
    }
}

为了分享len

insert_ele()

相同

答案 1 :(得分:1)

一个错误是:delete_ele()函数调用未定义的行为,因为当len值为20时,for循环中的数组索引超出范围(读取注释):

for(i = loc; i < len; i++){
   ar[i] = ar[i + 1]; 
}  //       ^^^^^^^ out of index for `i = len - 1` 
   // max index can be `len - 1` but when `i = len - 1` 
   // then ar[i + 1] == ar[len] that causes array out-of-index 

应该是:

for(i = loc; i < len-1; i++){ # replaced `len` by `len - 1` 
    ar[i] = ar[i + 1]; 
}

看起来你在其他功能上也有类似的错误。

答案 2 :(得分:1)

问题是len main中的delete_ele未被insert_elelen更新。 您可以按值将len传递给这些函数。然后在相应的函数内更新本地副本,但main中的len保持不变。

解决问题的一种方法是将delete_ele引用(作为指针)传递给insert_ele和{{1}}。