我有一个菜单,用户必须在其中输入一个数字,以下情况将对数组进行操作。然而,菜单必须按顺序完成。例如,用户必须首先设置数组的大小,然后将元素传递到单独模块中的数组中。
- 1- 输入数组的大小 2- 输入数组元素 3- 排序
数组 4- 在数组中查找一个数字 5- 打印数组
6-反向打印数组 7-退出
请输入您的选择:3 您的数组尚未初始化!
这是我到目前为止的代码。我在初始化数组和输入元素时遇到问题。一旦我完成了这些,我确信我可以完成剩下的事情:
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 array\n");
printf("2 - Enter the array elements\n");
printf("3 - Sort the array\n");
printf("4 - Find a number within the array\n");
printf("5 - Print the Array\n");
printf("6 - Reverse print the array\n");
printf("7 - Quit\n");
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 array\n");
}
default:
if (choice < 1 || choice > 7)
printf("Invalid choice please choose again\n");
};
} 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 变量,它会覆盖循环之前的变量。
关于c - 数组操作菜单 C 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31476800/