prime numbers help

Feb 2, 2008 at 6:30pm
I have this code and whenever it runs it just shows 1. It is supposed to spit out all prime numbers until it hits 1000.
Any help would be appreciated.



#include <cstdlib>
#include <iostream>

using namespace std;
int x=1;
int y;

int main()
{
loop:
int Prime [1000] = {x};
int n=1;
signed int m=1;

if (n/Prime[y]!=1) {
n=n+2;
x++;
x++;
goto loop;
}

else if (n/Prime[y]==1) {
cout << n;
n=n+2;
x++;
x++;
goto loop;
}
else if (n/Prime[y]>=1) {
n=n+2;
x++;
x++;
goto loop;
}
else {n=n+2;
goto loop;
}
system("PAUSE");
}
Feb 2, 2008 at 6:56pm
First thing to note is that your inside an infinite loop. I don't believe anyone should use goto's, labels, continue, break (except in switch statments) because it hurts readability for you and others.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#include <cstdlib>
#include <iostream>

using namespace std;
int x=1;
int y;

   int main()
   {
      loop:  //<----
      int Prime [1000] = {x};
      int n=1;
      signed int m=1;

      if (n/Prime[y]!=1)
      {
         n=n+2;
         x++;
         x++;
         goto loop;  //<----
      }
      else if (n/Prime[y]==1)
      {
         cout << n;
         n=n+2;
         x++;
         x++;
         goto loop;  //<----
      }
      else if (n/Prime[y]>=1)
      {
         n=n+2;
         x++;
         x++;
         goto loop;  //<----
      }
      else
      {
         n=n+2;
         goto loop;  //<----
      }

      system("PAUSE");
}




One way you can remove the labels and the infinite loop is 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <cstdlib>
#include <iostream>

#define MAX_PRIME_NUMBER 1000

using namespace std;
int x=1;
int y;

   int main()
   {
     //goes through all the numbers
      for( int i = 1; i < MAX_PRIME_NUMBER; i++)
      {
         //from here down to the end of the for loop you should
         //figure out if the number is prime or not before the end of the for loop
         //and print or save them
         int Prime [1000] = {x};
         int n=1;
         signed int m=1;

         if (n/Prime[y]!=1)
         {
            n=n+2;
            x++;
            x++;
         }
         else if (n/Prime[y]==1)
         {
            cout << n << " ";
            n=n+2;
            x++;
            x++;
         }
         else if (n/Prime[y]>=1)
         {
            n=n+2;
            x++;
            x++;
         }
         else
         {
            n=n+2;
         }
      }

      system("PAUSE");
   }

Last edited on Feb 2, 2008 at 7:04pm
Feb 2, 2008 at 7:03pm
ok ty, my problem comes up where i need to add a number to the array. I tried to use a while loop, but i get runtime error. Any suggestions how i could improve this?
Dexter
Feb 2, 2008 at 7:05pm
i get it nvm
Feb 2, 2008 at 7:08pm
The only number it outputs is "1 Press any key to close..." thanks on the infinite loop help!
Dexter
Feb 2, 2008 at 7:12pm

This is how you can assign values to an array.

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 <cstdlib>
#include <iostream>

#define MAX_ARRAY 10

using namespace std;

int main(int argc, char *argv[])
{
    int primeNumbers[MAX_ARRAY];
    
    //assign values to the array
    for( int i = 0; i < MAX_ARRAY; i++)
    {
         primeNumbers[i] = i;
    }
    
    //print array
    for( int i = 0; i < MAX_ARRAY; i++)
    {
         cout << primeNumbers[i] << endl;
    }    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



Prints
0
1
2
3
4
5
6
7
8
9
Feb 2, 2008 at 7:12pm
...
Last edited on Feb 2, 2008 at 7:13pm
Feb 2, 2008 at 7:22pm
i still get a runtime error... never mind thanks for the help. I'll have my friend come and help out.
Feb 2, 2008 at 7:23pm
I still learned a lot though thanks
Topic archived. No new replies allowed.