请检查代码是否正确 [英] Please review if the code is correct

查看:59
本文介绍了请检查代码是否正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FlavourArrayInt[10] = array[] 
SauceArrayBool[] 
NutsArrayBool []  
SprinklesArrayBool[] 
 
Print "How many Customers would you like to cater for at once:" 
Set input as Count_Value {Default assumption is 10, as per question} 
 
While( index <= Count_value ) Do {assume first value is 1} 
 
display "Customer" + count_value 
count_value = count_value + 1 
print "What is your flavour of ice-cream (f1, f2, f3)" 
	get input 
  set FlavourArrayStr[count_Value] to input 
display "1. Vanilla 2. Chocolate 3. Strawberry  Which one you prefer (1, 2, 3) : "  
                    read input

print  "Would you like sauce" 
	get input 
if input equals "Y" 
then set SauceArrayBool[count_value] =	"Y" 
else set SauceArrayBool[count_value] ="N" 
set SauceArrayInt[count_value] 
 
print  "Would you like nuts?" 
	get input 
if input equals "Y" 
then set NutsArrayBool [count_value] =	"Y" 
else set NutsArrayBool[count_value] ="N" 
 

print  "Would you like sprinkles" 
	get input 
if input equals "Y" 
then set SprinklesArrayBool [count_value] = "Y" 
else set SprinklesArrayBool [count_value] ="N" 
set SprinklesArrayBool [count_value] 
 
Print "For Customer : " + index 
Print "Your Flavour was" + FlavourArrayInt[index] 
Print "Your sauce was" + SauceArrayInt [index] 
Print "Your Nuts Option was" + NutsArrayBool[index] 
Print "Your Sprinkles Option was" + SprinklesArrayBool[index] 
 
end while loop



这是任务:
共识算法":十个人一组,需要从三种选择中决定他们都将订购哪种口味的冰淇淋.该算法可以向参与者提问和再提问,并向参与者呈现答案,直到达成共识为止.这个练习有些开放.如有必要,添加您的假设.显然,该算法可能永远不会导致答案.



This is the task:
Consensus algorithm": A group of ten people need to decide which one flavor of ice cream they will all order, out of three options. The algorithm can question and re-question the participants, and present the answers to the participants, until a consensus is reached. This exercise is somewhat more open-ended. Add your assumptions if necessary. Obviously, this algorithm might never result in an answer. Deal with that too.

推荐答案

该代码是100%正确的.唯一的问题是,不存在可以编译此特定代码段的C ++编译器版本.您需要编写自己的C ++编译器来执行它并检查输出.

此处 [
The code is 100% correct. The only thing is that there does not exist a version of C++ compiler which can compile this particular piece of code. You need to write your own C++ compiler to execute it and check the output.

Check the rules for posting the questions on CP here[^]


不,不是. (正如您没有告诉我们的那样,代码应该做什么,这个答案与其他任何代码一样好:-)

正如格里夫(Griff)"已经告诉您的那样,这看起来像一些伪代码,无法编译或测试.

通过查看您的伪代码,我会说在循环中使用count_value变量是不正确的.我假设您打算改用索引",因为它指定了您的当前客户. (您在某些地方对count_value变量的拼写有所不同,例如Count_Value和Count_value;这是故意的吗?)

伪代码的某些行不清楚,例如:
No, it is not. (As you didn''t tell us, what the code is supposed to do, this answer is as good as any other :-)

As "the Griff" already told you, this looks like some pseudo code, which cannot be compiled nor tested.

From looking at your pseudo code, I would say that the use of your count_value variable in the loop is incorrect. I assume you meant to use "index" instead, because it designates your current customer. (You spelled the count_value variable differently in some places, like Count_Value and Count_value; was that intentional?)

Some of the lines of your pseudo code are unclear, like:
set SauceArrayInt[count_value] 



您的代码的主要逻辑很简单.您希望我们在一个简单的提示和输出循环上进行检查吗?



The principal logic of your code is mostly trivial. What would you like us to check on a simple prompt and output loop?


一些问题:

0.您代码中的注释表示还有更多有关您尚未共享的任务的信息.

1.您问题中的标记为C ++,但是代码看起来像BASIC.虽然显然它只是伪代码,但是代码的整个结构甚至都不是C风格的.对于一个变量,您至少应定义所使用的变量的类型,并对块(''{''''}'')和注释(''/*''''*/'')使用正确的语法.伪代码看起来像实际的C代码越少,实现它的可能性就越大. (对于像我们这样的其他人来说,更难理解)

2.我看到"count_value","Count_value","count_Value"和"Count_Value".虽然伪代码没有问题,但是在实现此功能时,您应该真正注意拼写,因为编译器区分大小写.

3.您的一半代码(坚果,撒酱)与您的实际任务无关.但是,大多数实际任务(寻找共识)尚未完成.为什么不把注意力集中在自己需要的东西上,然后在不必要地使它复杂化之前解决它?

4.一些算法问题:
-您定义了一个大小为10的数组,然后要求输入所需的大小?由于任务告诉您假设10,所以不要打扰.或者,如果您要求,请在以后定义数组.
-您可以在循环的早期就增加索引(或错误地使用的count_value),但以后仍要引用它.这样,您将在循环的第一部分访问一个数组元素,然后再访问另一个.不要那样做!总是在循环结束时或开始时增加. C/C ++样式的循环使此操作非常容易,因此也可以将其用于伪代码!
-关于比较,尤其是用户输入的警告:永远不要期望用户输入有效的选项,或尊重区分大小写.同样,C/C ++中的比较运算符为''==''.运算符''=''表示值的增加,即使这是伪代码,您也应区分两者!
Some problems:

0. The comments in your code indicate there is more information about your task that you haven''t shared yet.

1. The tag on your question says C++, but the code looks like BASIC. While it is obviously only pseudo-code, the entire structure of the code isn''t even remotely C-style. For one you should at the very least define the type of variables that you are using, and should use proper syntax for blocks (''{'' and ''}'') and comments (''/*'' and ''*/''). The less your psuedo code looks like actual C code, the more likely you''ll introduce mistakes when implementing it. (and the harder it is for others - like us - to understand it)

2. I see ''count_value'', ''Count_value'', ''count_Value'' and ''Count_Value''. While unproblematic in pseudo code, you should really take care of your spelling when implementing this, as the compiler is case-sensitive.

3. Half of your code (nuts, sprinkles sauce) isn''t related to your actual task. However, most of the actual task (finding a consensus) isn''t done yet. Why don''t you just focus on what you need and get it solved before needlessly complicating it?

4. Some algorithmic problems:
- you defined an array of size 10, and afterwards asked for the required size? Since the task told you to assume 10, don''t bother asking. Or if you ask, define the array afterwards.
- you increment your index (or count_value that you erroneously used instead) rather early in the loop, but still reference it later. That way you''re accessing one array element in the first part of the loop and another later. Don''t do that! Always increment either at the end of the loop, or at the start. C/C++ style for loops make it really easy to do this, so use those for pseudo code too!
- a word of warning about comparing, and specifically user input: don''t ever expect the user will enter a valid option, or respect case-sensitivity. Also, the comparison operator in C/C++ is ''==''. The operator ''='' means assgnment of a value, and even if this is pseudo code, you should distinguish the two!


这篇关于请检查代码是否正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆