C++ 使用数组(C+;+;)打印前100个素数

C++ 使用数组(C+;+;)打印前100个素数,c++,arrays,C++,Arrays,我对这方面还不太熟悉,我想知道我现在所做的是否正确。我必须用C++中的数组打印前100个素数。我的代码除了需要的素数外,还输出许多数字。我想知道到底出了什么问题。先谢谢你 int myPrimes[100]; int i,j,count=1,b=0; for(i=3;i>0;++i) { for(j=2;j<=i/2;++j) { if(i%j==0){//checking if composite b=1;//if comp

我对这方面还不太熟悉,我想知道我现在所做的是否正确。我必须用C++中的数组打印前100个素数。我的代码除了需要的素数外,还输出许多数字。我想知道到底出了什么问题。先谢谢你

int myPrimes[100];
int i,j,count=1,b=0;

for(i=3;i>0;++i)
{
    for(j=2;j<=i/2;++j)
    {
        if(i%j==0){//checking if composite
            b=1;//if composite, goes to the next number
            break;
        }
    }

    if(b==0)//if prime
    {
        //cout<<"\n"<<i;//print the number
        myPrimes[count]=i;
        count++;//counter goes up 1 to go to the next number
    }

    b=0;//resets b so you can run the loop again
    if(count==101)//after the counter reaches 100, exits the program
        break;
}

for(int i=0;i<=sizeof(myPrimes);i++){
  cout << myPrimes[i] << endl;
}

return 0;
int-myPrimes[100];
int i,j,count=1,b=0;
对于(i=3;i>0;++i)
{

对于(j=2;j尝试将(myPrimes)
的大小替换为
100
,并且还包括myPrimes[0]=2,因为它是素数


sizeof
运算符提供操作数的大小(以字节为单位)

// Recursive program to check if a given linked list is palindrome
#include <iostream>
#include <stdio.h>
#include <stdlib.h>


int main()
{
    int myPrimes[100];
int i,j,count=1,b=0;
myPrimes[0]=2; // 2 is also prime numer include this in the myPrimes array
for(i=3;i>0;++i)
{
    for(j=2;j<=i/2;++j)
    {
        if(i%j==0){//checking if composite
            b=1;//if composite, goes to the next number
            break;
        }
    }

    if(b==0)//if prime
    {
        //cout<<"\n"<<i;//print the number
        myPrimes[count]=i;
        count++;//counter goes up 1 to go to the next number
    }

    b=0;//resets b so you can run the loop again
    if(count==101)//after the counter reaches 100, exits the program
        break;
}

for(int i=0;i<100;i++){ // use 100 instead of sizeof(myPrimes)
  cout << myPrimes[i] << endl;
}

return 0;
}
for(int i=0;i<100;i++){ 
  cout << myPrimes[i] << endl;
}

//检查给定链表是否为回文的递归程序
#包括
#包括
#包括
int main()
{
int-myPrimes[100];
int i,j,count=1,b=0;
myPrimes[0]=2;//2也是质数,包括在myPrimes数组中
对于(i=3;i>0;++i)
{

对于(j=2;j使用数组打印前100个素数

int素数[100]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,
67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157,
163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257,
263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367,
373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 
479, 487, 491, 499, 503, 509, 521, 523, 541};
对于(int i=0;i<100;i++){

不能使用100而不是
sizeof(myPrimes)

sizeof不应用于获取此处的元素数。应使用单独的数组大小(或长度)参数。因为sizeof运算符以字节为单位提供操作数的大小

// Recursive program to check if a given linked list is palindrome
#include <iostream>
#include <stdio.h>
#include <stdlib.h>


int main()
{
    int myPrimes[100];
int i,j,count=1,b=0;
myPrimes[0]=2; // 2 is also prime numer include this in the myPrimes array
for(i=3;i>0;++i)
{
    for(j=2;j<=i/2;++j)
    {
        if(i%j==0){//checking if composite
            b=1;//if composite, goes to the next number
            break;
        }
    }

    if(b==0)//if prime
    {
        //cout<<"\n"<<i;//print the number
        myPrimes[count]=i;
        count++;//counter goes up 1 to go to the next number
    }

    b=0;//resets b so you can run the loop again
    if(count==101)//after the counter reaches 100, exits the program
        break;
}

for(int i=0;i<100;i++){ // use 100 instead of sizeof(myPrimes)
  cout << myPrimes[i] << endl;
}

return 0;
}
for(int i=0;i<100;i++){ 
  cout << myPrimes[i] << endl;
}


for(int i=0;我想知道
sizeof(myPrimes)
实际给你的是什么。
sizeof
运算符给你操作数的大小(字节)。如果
int
是32位类型(最常见)那就是4个字节。你的数组
myPrimes
将是
4*100
字节大,或者
400
。现在想想在你的循环中打印
myPrimes
的值。还要记住数组索引是零基的。因此
100
元素的数组将具有从
0
的索引e> 99
(包括在内)。这意味着您的条件
if(count==101)
将在您已写入数组中的索引
100
时中断循环。超出数组的边界将导致未定义的行为。