is my compiler bad?!

Apr 22, 2014 at 5:17pm
first of all: i use code blocks with gnu compiler, is that bad?! and creating problems?!
so i was just starting to make a program (my teacher says i am too dumb to make anything, so i challenged him to make a program way ahead of my time, as we now study f###ing qbasic!!) but then strange results happened! i tried with like 10 different ways and to no avail... i used for, changed it with while, i used x++, changed it with x += 1, and nothing different, try writing like 10 and 30.... help please!!!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
using namespace std;

int main()
{
    int N1, N2;
    cout << "This program will find prime numbers between two of your specified number!" << endl << "please enter the first number: ";
    cin >> N1;
    cout << "please enter the second number: ";
    cin >> N2;

    int x = N1;
    while (x < N2)
    {
        cout << x;
        x += 1;`
    }

}
Last edited on Apr 22, 2014 at 5:40pm
Apr 22, 2014 at 5:19pm
oh and still, i was just checking if it would work before going on.... this is not even 1%
Apr 22, 2014 at 5:41pm
help please?!
Apr 22, 2014 at 5:45pm
just checking if it would work before going on

That is a good approach

then strange results happened!

Define "strange results". What did you expect? what did you observe?

As written (except for the stray apostrophe), it runs as you can see at http://ideone.com/nQ23fz , and outputs 10111213....2829 (all numbers from 10 to 29 with no other output)
Last edited on Apr 22, 2014 at 5:46pm
Apr 22, 2014 at 6:04pm
maybe my teacher wasn't wrong after all! i forgot to use endl! thank you so much :)
Apr 22, 2014 at 6:20pm
When you actually look for the primes you should use a sieve. The easiest is a sieve of Eratosthenes. Using a std::vector<bool> is also suggested. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
http://www.cplusplus.com/forum/beginner/129775/#msg700669

Apr 24, 2014 at 1:03pm
I knew there is an easy way, but part of the challenge is to not use the internet, i used it once, i wont cheat again!
Apr 24, 2014 at 7:56pm
Using resources is not cheating. Also the a prime sieve is actually really common :P
Apr 24, 2014 at 8:02pm
Kinda silly requirement. I imagine they want you to come up with some creative solution, but if they want that then they need to come up with a creative problem. +1 for using the sieve.
Last edited on Apr 24, 2014 at 8:02pm
Apr 25, 2014 at 4:28am
i am still trying, and i will never use you cheaty resources :P but i have a problem in my code so far... take a look:
(once again, this is not even 40% i am still trying to test before going on! try inputting 10 and 12 and it will stop working!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int main()
{
    int N1, N2;
    cout << "This program will find prime numbers between two of your specified number!" << endl << "please enter the first number: ";
    cin >> N1;
    cout << "please enter the second number: ";
    cin >> N2;
    N2++;
    int x = N1;

    for (x; x < N2; x++)
    {
        for (int y = 0; y < 1000; y++  )
        {
            int z;
            z %= x/y;
            if (z == 0)
            {
                cout << x << endl;
            }
        }

    }

}
Apr 25, 2014 at 4:38am
You need better variable names. What are x y, z, N1, and N2 supposed to be?

Also, z is uninitialized on line 18 so how can you do z = z % (x/y);?

If you really want to do it the way you are (very slow and ineffective, will probably take several hours for finding about a billion primes instead of a matter of seconds). You probably want something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//lower and upper bounds
int const lower = 1;
int const upper = 100;

int lowerOdd = lower;
if(lowerOdd == 2) //it is 2 (the first prime)
{
    std::cout << 2 << ' ';
    lowerOdd = 3;
}
else if(lowerOdd > 2 && lowerOdd % 2 == 0) //greater than 2 and even
{
    ++lowerOdd;
}
for(int i = lowerOdd; i < upper; i += 2) //2 is the only even prime
{
    bool prime = i % 2; //if it is odd it will be true
    for(int j = 3; j <= std::sqrt(i); j += 2) //only need to check until the sqrt
    {
        if(i % j == 0)
        {
            prime = false;
            break;
        }
    }
    if(prime)
    {
        std::cout << i << ' ';
    }
}
Again though, this method is very slow.
Topic archived. No new replies allowed.