簡體   English   中英

我正在制作一個菜單驅動程序,我必須在其中創建、刪除和插入一個數組

[英]I am making a Menu driven program in which i have to create, delete and insert an array

我正在制作一個菜單驅動程序,我必須在其中創建、刪除和插入一個數組,但是當我調用刪除或插入 function 因為我的代碼沒有運行時,請幫忙。 請告訴我如何調用 function 以便代碼可以運行。 如果我刪除注釋,調用 indDelete 和 IndInsert 我的代碼運行良好,但是當我取消注釋它們時,代碼不會運行。

#include <stdio.h>
#include <stdbool.h>
//printing the array
void display(int arr[], int size){
    for(int i = 0; i < size; i++){          
        printf("%d ",arr[i]);
    }
    printf(" /n");
}

//insertion function
int indInsert(int arr[], int capacity, int size, int element, int index){

    if(size>=capacity){                 
        return -1;
    }
    for(int i = size-1; i >= index; i--){              
        arr[i+1] = arr[i];
    }
    arr[index] = element;
    return 1;
}

//deletion function
int indDelete(int arr[], int capacity, int size, int index){
    if(index > capacity || index < 0){
          return -1;
    }
    for(int i = index; i < size-1; i++){
        arr[i] = arr[i+1];
    }
    return 1;
}

//driver code 
int main(){
    int capacity, arr[capacity], size;
    printf("Enter the capacity of an array \n");
    scanf("%d", &capacity);
    printf("Enter the number of elements you want to enter \n");
    scanf("%d", &size);

    printf("Enter the elements:- \n");
    for(int i = 0; i < size; i++){
        scanf("%d", &arr[i]);
    }
    bool check = false;
    int exit = 0, element = 0, index = 0;;
    while(check == false){
        printf("If you want to insert an element press 1 \n");
        printf("If you want to delete an element press 2 \n");
        printf("If you want to exit press 3 \n");
        scanf("%d", &exit);

        switch(exit){
        case 1:
            printf("Enter the element:- \n");
            scanf("%d", &element);
            printf("Enter the index:- \n");
            scanf("%d", &index);

            printf("The present Array \n");
            display(arr, size);
            printf("The insterted array \n");
            indInsert(arr,capacity,size,element,index);
            size +=1;
            display(arr, size);
            break;

        case 2:
            printf("Enter the index:- \n");
            scanf("%d", &index);

            printf("The present Array \n");
            display(arr, size);
            printf("The insterted array \n");
            indDelete(arr,capacity,size,index);
            size -= 1;
            display(arr, size);
            break;
            
        case 3:
            printf("Thank you for using the program");
            check = 111;
            break;

        default:
            printf("Error: Wrong cmd");
            break;
        }
    }
    
    return 0;
}

在 main 的第一行,你有

int capacity, arr[capacity], size;

這意味着arr是一個未知大小的數組,因為capacity尚未定義。 即使您稍后更改capacity ,陣列大小也不會改變。

如果您打開編譯器警告,您可能會收到通知,例如 gcc 的-Wall或 msvc 的/Wall

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM