0

I am writing a program that creates two vectors to practice comparing elements in a vector. The first vector is a list of all numbers that are not prime numbers between 1-100 (not prime numbers), the second vector is a list of all numbers between 1-100 (vector number_bank). I am using loops to compare each element in the vectors. Lets say the number 5 comes up from the number_bank (which is a prime number), if this number does not match any number in the not_prime_numbers vector then it is pushed back into a third vector called prime numbers. If a number like 4 were to be compared to the elements of the not_prime_number list it is supposed to match with the number 4 in this list and break the loop without pushing back the number into the prime_numbers vector. What is happening is all numbers between 1-100 are being pushed back. 4 in the number_bank vector is never equal to the 4 in the not_prime_numbers vector so the cycle continues pushing back 4 into the prime numbers vector.

There is no math involved changing the elements in these vectors so there shouldn't be an incremental margin of error (epsilon) usually associated with comparing floats. Is there a better way to compare the elements in these vectors?

int main()
{
    float i = 1.0;
    unsigned int n = 0;
    std::vector<float>not_prime_numbers;
    std::vector<float>number_bank;
    std::vector<float>prime_numbers;
    while (i < 101.0)
    {
        for (float j = 1.0;j<(i);++j)
        {
            float p = i / j;
            if (abs(floor(p)) == p&&j!=1.0)
            {
                not_prime_numbers.push_back(i);
                break;
            }
        }
        ++i;
    }

    for (float k = 1.0; k < 101.0; ++k)
    {
        number_bank.push_back(k);
    }

    for (unsigned int m = 0; m < number_bank.size(); ++m)
    {
        while (n < not_prime_numbers.size())
        {
            if (not_prime_numbers[n] == number_bank[m])  // here is where i try to break the loop
            {                                            
            break;                                   
            }
            if (n == (not_prime_numbers.size()-1))
            {                   // here is where element is pushed back when compared to all loop elements
                prime_numbers.push_back(number_bank[m]);
                break;
            }
            if (not_prime_numbers[n] != number_bank[m])
            {
                ++n;
            }

        }
    }


    std::cout << "All prime numbers between 0 and 100 are as follows:\n";
    for (unsigned int j = 0; j < prime_numbers.size(); ++j)
    {
        std::cout << prime_numbers[j] << "\n";
    }
    return 0;
}

I read about .compare and tried it but it states that there needs to be a class structure to the left (perhaps a vector has no class structure?). I'm new to C++ and would greatly appreciate the help.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
FuzMunkey
  • 17
  • 4
  • ***it but it states that there needs to be a class structure to the left*** Please always post the exact text of the error message. If this is Visual Studio copy that from the Output Tab ( Alt+2 ) – drescherjm Jan 16 '19 at 22:19
  • 2
    Why are you using floating point types? Prime numbers are only integers and you can solve if a integer is prime or not by using `%` to check the remainder. You should really switch your data types. – NathanOliver Jan 16 '19 at 22:21
  • @NathanOliver I used floats due to the way I check if its not a prime number. If I used ints the rounding would cause all numbers between 1-100 to be defined as not prime numbers. The purpose is more so comparing vector elements than the prime number aspect. – FuzMunkey Jan 16 '19 at 23:19
  • Sidenote: Comparisons of `float`s can get messy because `float`s can't contain exact representations of many numbers and even when it can, sometimes the result of an arithmetic operation slightly "breaks" the number. In other words, you think you're looking at 25, but what the `float` is actually holding is `24.999999999999997` and isn't exactly equal to 25. More on this topic: [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – user4581301 Jan 17 '19 at 00:03
  • @user4561301 hmmm, what would then be the proper syntax of comparing two float values? One vector a number bank and the second vector a series of calculated values ? – FuzMunkey Jan 17 '19 at 00:27

1 Answers1

0

You never reset n, so once you get a prime number (when n == (not_prime_numbers.size()-1)) the rest of the numbers will also be added to the prime list.

Why are you checking that condition within the loop? You should set a flag variable if you find the number is not prime, then check that after your loop to possibly add it to your prime list.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • switching to a for loop worked thanks! For arguments sake lets say the issue was you think you're looking at 25, but what the float is actually holding is 24.999999999999997 and isn't exactly equal to 25.what would then be the proper syntax of comparing two float values? One vector a number bank and the second vector a series of calculated values ? – FuzMunkey Jan 17 '19 at 00:28
  • @FuzMunkey See [What is the most effective way for float and double comparison?](https://stackoverflow.com/questions/17333/what-is-the-most-effective-way-for-float-and-double-comparison) – 1201ProgramAlarm Jan 17 '19 at 00:48