C++ 将值赋值到数组c++;
我试图通过计算创建一个素数数组。作为一个学习编码的项目。最后,我要建立自己的数学库,这样我就可以在各种级别上添加,因为我学会了C++代码。 下面的代码非常适合根据搜索范围将素数打印到屏幕上,但我的totalPrimes迭代器的值为1。所以每次它把最后一个素数放在素数[1]的位置 任何建议都会很棒C++ 将值赋值到数组c++;,c++,arrays,sorting,C++,Arrays,Sorting,我试图通过计算创建一个素数数组。作为一个学习编码的项目。最后,我要建立自己的数学库,这样我就可以在各种级别上添加,因为我学会了C++代码。 下面的代码非常适合根据搜索范围将素数打印到屏幕上,但我的totalPrimes迭代器的值为1。所以每次它把最后一个素数放在素数[1]的位置 任何建议都会很棒 #include <iostream> #include <array> std::array<long, 10000000> PrimeNumbers={0};
#include <iostream>
#include <array>
std::array<long, 10000000> PrimeNumbers={0};
void isPrime(long x);
int main() {
for (long i = 1; i < 10; i++) {
isPrime(i);
}
for(int h = 0; h < 10; h++) {
std::cout << "\nSecond Prime is : " << PrimeNumbers[h];
}
}
void isPrime(long x) {
int count(0), totalPrimes(0);
for (long a = 1; a < x; a++) {
if ((x % a) == 0) {
count += 1;
}
}
if (count == 1) {
++totalPrimes;
std::cout << '\n' << x << " is a Prime number";
PrimeNumbers[totalPrimes] = x;
}
}
#包括
#包括
std::数组素数={0};
虚质(长x);
int main(){
用于(长i=1;i<10;i++){
优先权(i);
}
对于(int h=0;h<10;h++){
std::cout看起来您在变量作用域方面遇到了一些问题。出现问题的原因(正如我在评论中指出的)是totalPrimes
是本地变量,因此您正在创建一个新的整数变量,并在每次调用函数时将其设置为0
但是,您已经将素数
设置为全局,并让isPrime
函数对其进行修改,这看起来不是一个好的做法
所有这些都可以通过稍微重新构造来修复,以使代码更漂亮:
#include <iostream>
#include <array>
bool isPrime(long x);
int main() {
std::array<long, 10000000> PrimeNumbers={0};
int totalPrimes = 0;
for (long i = 1; i < 10; i++) {
if (isPrime(i)) {
std::cout << '\n' << i << " is a Prime number";
PrimeNumbers[totalPrimes++] = i;
}
}
for(int h = 0; h < 10; h++) {
std::cout << h << " Prime is : " << PrimeNumbers[h] << std::endl;
}
}
bool isPrime(long x) {
int count(0);
for (long a = 1; a < x; a++) {
if ((x % a) == 0) {
count += 1;
}
}
return count == 1;
}
#包括
#包括
bool-isPrime(长x);
int main(){
std::数组素数={0};
整数totalPrimes=0;
用于(长i=1;i<10;i++){
如果(i){
std::cout每次函数运行时,您都将totalPrimes初始化为0。您需要将totalPrimes作为全局变量,或者更好(因为全局变量可能会出现问题),将其设置为第一个可用的素数成员,然后再对该函数执行任何操作。跟踪素数数组的位置
size_t nLastPos=0;
...
for(size_t x = 0; 1000 > x; ++x)
{
if(isPrime(x))
{
PrimeNumbers[nLastPos++] = x;
}
}
for(size_t i = 0; nLastPos > n; ++n)
{/* print out number PrimeNumbers[n] */ }
您的程序可以稍微重新构造,使其更易于遵循和调试
除了决定一个数字是否为素数的逻辑之外,不要把东西放在isPrime
中。确保它返回一个bool
。这将使函数更简单,更容易调试
在调用函数中使用isPrime
的返回值来执行其他簿记任务
用于检查数字是否为素数的逻辑不正确。这需要修复
这是您发布的代码的更新版本
#include <iostream>
#include <array>
#include <cmath>
std::array<long, 10000000> PrimeNumbers={0};
bool isPrime(long x);
int main()
{
int totalPrimes = 0;
for (long i = 1; i < 10; i++)
{
if ( isPrime(i) )
{
std::cout << i << " is a Prime number" << std::endl;
PrimeNumbers[totalPrimes] = i;
++totalPrimes;
}
}
}
bool isPrime(long x) {
// 1, 2, and 3 are primes.
if ( x <= 3 )
{
return true;
}
// Even numbers are not primes.
if ( x % 2 == 0 )
{
return false;
}
// Check the rest.
long end = (long)std::sqrt(x);
for (long a = 3; a < end; a += 2) {
if ((x % a) == 0)
{
return false;
}
}
return true;
}
每个人都在谈论每次调用函数时totalPrimes变量是如何重置的,这显然是正确的。您可以从函数返回值并从main中递增它,您可以使用全局变量,在函数外部定义变量,这样就不会在函数内部或你可以用
一个静态变量!
看看这个简单的例子。我有一个名为up_two的函数,每次调用该函数时,它都会将的值增加2。静态变量int value有每次函数up_two()的内存被称为,每次递增两个值。如果我只使用一个整数,它将始终重置该值并使其为零,这就是我最初定义的值
使用静态变量的优点是,我可以计算函数被调用的次数,并且可以使计数器特定于特定函数
#include <iostream>
using namespace std;
void up_two();
int main()
{
for(int i = 0; i < 10; i++)
{
up_two();
}
return 0;
}
void up_two()
{
static int value = 0;
cout << value << endl;
value += 2;
}
这样,我的程序将打印以下内容:
0
2
4
6
8
10
12
14
16
18
如果没有静态声明,只会得到10行零
这很麻烦
希望这能帮助您优化您的程序。totalPrimes
是isPrime
函数的本地部分。您可以创建一个新的totalPrimes
并在每次调用该函数时将其设置为0
,因此您总是将素数设置为素数[1]
谢谢。现在看它就很清楚了。@DFittvoid isPrime(长x){
当然应该返回一个bool
值。否则该函数就没有意义。另一个问题是,在用作索引之前,您需要增加totalPrimes。应该是相反的方式。@user0042在这种情况下不是-函数填充全局数组。超级答案!感谢您花时间从中显示一个良好的实践代码块我的混乱。这是我的主要目标。@DFitt谢谢!如果这是你想要的答案,请
0
2
4
6
8
10
12
14
16
18