问题描述
我试图学习如何使用函数通过菜单传递变量。问题是,从来没有人教过如何去做。正如你可以想象的,任何变量,我会进入第一个菜单功能,通常用于我的例子/其他功能,如
C $ C>。它很好地执行它的任务,即从用户那里获得一个数字并返回它。现在让我们使用这个数字。在 case 1 之后,我们来写下
arr [numCount] = function1();
numCount + = 1;
这将数组中第一个未使用的条目设置为输入的数字,我们拥有的元素数量。不要执行不必要的计算 - 让我们考虑如何实现我们的最高和最低功能。 一种方法是在每次调用函数时遍历整个数组,并跟踪我们所看到的最大数。这可行,但我们最终会重复很多工作。如果每次我们得到一个新的数字,我们会更新当前的最高和最低数字? / strong> - 要计算平均值,我们将使用for循环。如果你不理解它,请查阅文档。
我认为你可以看到如何将这种思想扩展到计算输入值的平均值。以下是修改后的内容 - 。
I am attempting to learn how to pass variables through a menu using functions. Problem is, at no point was taught how to do so. As you can imagine, any variables I would enter in the first menu function to normally utilize in my cases/other functions such as
if (count==0) { low = number; high = number; count++; sum = number; } else { if (number < low) number = low; if (number > high) high = number; count ++; sum += number; }Will not pass through function 2, as anyone more knowledgeable with C would realize. It will not work in int main either. How would one go about defining user entered numbers,highest,lowest,etc. To the other functions? Here is what I have so far, the loop and menu works fine.
#include<stdlib.h> #include<stdio.h> int menuChoice() { int choice; printf("1.Enter a number\n"); printf("2.Display Highest Number Entered\n"); printf("3.Display Lowest Number entered\n"); printf("4.Display Average of Numbers Entered\n"); printf("5.Quit\n"); printf("Enter your choice: "); scanf("%i", &choice); return choice; } int function1() { int number; printf("Enter a number:\n"); scanf("%i", &number); return number; } int function2() { } int function3() { } int function4() { } int main() { int quit = 0; while (quit != 1) { int menu; menu = menuChoice(); switch (menu) { case 1: function1(); break; case 2: function2(); break; case 3: function3(); break; case 4: function4(); break; case 5: quit = 1; break; default: printf("Please enter 1 through 5\n"); } } return 0; }解决方案Let's look at some ways we can improve this code.
Naming functions - Functions should be given clear, descriptive names that allow someone not familiar with the program to easily understand what they do. Names like function1 and function2 don't achieve this.
Use appropriate data structures - It sounds like you're trying to keep track of all of the numbers the user entered, but you don't currently have any way of doing that. The best way to do this would be with an array, which is a container that holds other values.To keep track of the numbers you have so far, let's make two variables - an array that stores the numbers, and an integer that keeps track of how many numbers have been entered. For now, let's let the user enter up to 100 numbers. We do this by writing int arr[100]; and int numCount = 0; at the top of the program. Quick side note - this is called a global variable - usually it's not a good idea, but we won't worry about it for now.
Separate code into appropriate functions - You do a good job of this in your function1. It performs its task well, which is to get a number from the user and return it. Now let's use that number. After the case 1, let's write
arr[numCount] = function1();numCount += 1;
This sets the first unused entry in the array to the entered number, then increased the counter for the number of elements that we have.
Don't perform unnecessary computation - Let's think about how we can implement our highest and lowest functions. One way to do it would be to go through the entire array each time the function is called and keep track of the highest number we have seen. This would work, but we would end up repeating a lot of work. What if each time we get a new number, we update the current highest and lowest number we have seen?
Use loops to scan through arrays - To calculate the average, we will use a for loop. Look up the documentation for this if you don't understand it.
I think you can see how to extend this thinking to calculating the average of the entered values. Here's what I have after modifying - http://pastie.org/10285795.
这篇关于通过switch语句和函数传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!