数组操作菜单C编程



我有一个菜单,用户必须输入一个数字,下面的情况将对数组进行操作。然而,菜单必须按顺序完成。例如,用户必须首先设置数组的大小,然后在单独的模块中将元素传递到数组中。

  1. 1-输入数组的大小2-输入数组元素3-排序4-在数组中找到一个数字5-打印数组
    7-退出

请输入您的选择您的数组尚未初始化!

这是我到目前为止的代码。我在初始化数组和输入元素时遇到了麻烦。一旦我完成了这些,我相信我可以做剩下的:

int sizeArray();
int *enterElements(int size, int *anArray);

int main(){
    int choice; 
    int tempSize;
    int mArray[100]; //initialize the Array
    do{
        printf("1 - Enter the size of the arrayn");
        printf("2 - Enter the array elementsn");
        printf("3 - Sort the arrayn");
        printf("4 - Find a number within the arrayn");
        printf("5 - Print the Arrayn");
        printf("6 - Reverse print the arrayn");
        printf("7 - Quitn");
        printf("n");
        printf("Please Enter your choice: ");
        scanf("%d", &choice);
        switch (choice){
        case 1:
            int tempSize = sizeArray(); 
        case 2: 
            if (tempSize != 0){
                enterElements(tempSize); 
            }
            else{
                printf("You should first set the size of the arrayn"); 
            }
        default:
            if (choice < 1 || choice > 7)
                printf("Invalid choice please choose againn");
        };
    } while (choice != 7);
}
int sizeArray(){
    int size = 0; 
    printf("What is the size of your array(1-20)? "); 
    scanf_s("%d", size); 
    if (size > 20 || size < 1){
        printf("Array size should be between 1 and 20 "); 
    }
    return size; 
}
int *enterElements(int size){
    int i = 0; 
    for (i = 0; i < size; i++)
    {
        printf("Enter Array Element %d :", i);
        scanf_s("%d", &anArray[i]); 
    }
}

我认为enterElements函数应该是这样的:

void enterElements(int *anArray, int size)
{
 //Your code here
}

并将函数调用为:

enterElements(mArray, tempSize);

如果你不应该声明新的int tempSize变量,它会覆盖one before循环。

你有以下错误:

  1. 在switch语句中使用tempSize变量前删除int;
  2. 在原型中,你声明enterElements函数接受2(!)个参数并返回一个指针。但是在函数实现中,你只有一个参数,没有返回值。

最新更新