我无法解决数组

时间:2019-04-11 22:29:50

标签: c++

我是新手,正在学习编程,尤其是在c ++中。我想做一个练习,显示带有数组的100到200之间的质数,但我不明白为什么会收到错误:“数组下标无效的类型'int [int]'”。请帮帮我。

我在网上尝试过dev c ++和编译器

#include<iostream>
#include<stdlib.h>

using namespace std;
int main(int argc, char *argv[]){

    //Define variables 
    int i,j,temp, accountant=0,number = 100;
    bool isPrime = true;

    //algorithm that tells us how many prime numbers there are between 100 and 200

    for(i=100; i<200;i++){
        isPrime=true;

        for(j=2; j<1-i;j++){
            if(i %j==0){
                isPrime=false;
                j=1;
            }
        }
        if(isPrime==true){
            accountant=accountant+1;
        }
    }

    //create the vector primes that have cells
    int vectorPrimes(accountant);

    //filling the vector with the prime numbers between 100 and 200
    for(i=0;i<accountant;i++){
        isPrime=true;
        for(j=2;j<number-1;j++){
            if(number % j == 0){
                isPrime=false;
                j=number;
            }
        }
        if(isPrime== true){
            vectorPrimes[i]=number;
        }
        else{
            i=1-1;
        }
        number=number + 1;
    }

    //Method of ordering the boxes of the arrangement (Major to minor)
    for(i=0;i<accountant-1;i++){
        for(j=i+1;j<accountant;j++){
            if(vectorPrimes[j]>vectorPrimes[i]){
                temp=vectorPrimes[j];
                vectorPrimes[j]=vectorPrimes[i];
                vectorPrimes[i]=temp;
            }
        }
    }

    //print the ordered arrangement
    cout<<"The prime numbers between 100 and 200 are the following: \n\n";
    for(i=0;i<accountant;i++){
        cout<<vectorPrimes[i]<<endl;
    }

    system("PAUSE");
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您遇到了一些问题。

首先,您可能想要

#include <vector>

如果您打算使用std::vector。其次

int vectorPrimes(accountant);

不声明向量,而是声明一个整数。

std::vector<int> vectorPrimes(accountant);

最可能是您想要的。

可能还有其他错误,但是如果您还没有这样做,请为您选择的环境下载一个不错的IDE,并学习如何使用调试器单步调试程序。这样一来,您就可以根据需要查看不起作用的地方,从而可以解决问题。