创建一个程序,将素数打印到N. [英] Create a program to print prime number upto N

查看:72
本文介绍了创建一个程序,将素数打印到N.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

e.g. if N=9 prime numbers upto N are 2,3,5,7



有人可以帮我识别我的代码有什么问题



我尝试过:




Can someone help me identify what is wrong with my code

What I have tried:

#include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
      short N,pc=0,num=2;
      bool prime;
      while(pc!=N)
      {
       prime=true;
        for(short i=2;i<=sqrt(N);i++)
        {
           if(N%i==0)
           {
               prime=false;
               break;
           }
        }
     }
      if(prime)
       {
          pc++;
          cout<<num;
       }
      return 0;}

推荐答案

第一个问题:

First problem:
Quote:

创建一个程序打印素数到N

Create a program to print prime number upto N



此代码中 N 的值是多少?



第二个问题:

学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。


What is the value of N in this code ?

Second problem:
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
  short N,pc=0,num=2;
  bool prime;
  while(pc!=N)
  {
    prime=true;
    for(short i=2;i<=sqrt(N);i++)
    {
      if(N%i==0)
      {
        prime=false;
        break;
      }
    }
  }  // this line is misplaced
  if(prime)
  {
    pc++;
    cout<<num;
  }
  // and should be here
  return 0;
}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]



第三个问题:

你的代码总是检查N是否为素数。这是错误的。



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么,你的任务是与之比较它应该做什么。

调试器中没有魔法,它不知道你的cpde应该做什么,它没有找到bug,它只是通过向你展示什么来帮助你正在进行。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试你的程序(踩踏和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

Third problem:
Your code is always checking if N is prime or not. It is wrong.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


您没有将变量N设置为任何值。

You have not set variable N to any value.
short N,pc=0,num=2;
bool prime;
while(pc!=N) // what is the value of N?
{


你应该做同样的基本输入/输出在你的代码中得到N. br />


不平等检查是一个非常坏主意因为N必须等于pc,否则它会永远运行。我会称之为设计缺陷,不要将其称为严重错误



你最好检查一下操作员较少,但您的代码现在还不清楚。
You should do same basic input/output in your code to get N.

And the inequality check is a very bad idea because than N has to be equal to pc, else it runs forever. I would call it a design flaw to not call it a severe bug.

You better check with the less operator, but your code is unclear for now.


这篇关于创建一个程序,将素数打印到N.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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