IS PROGRAMMING NOT FOR ME:(

So I've been working with the basics in programming for quite a while now. I stumbled upon the The Sieve of Eratosthenes algorithm and wanted to convert it into code. But after countless variables and loops I realized it's not even close to an efficient code and doesn't even work the way it should; I gave up without completing it. I wouldn't ask you to fix the code but provide me your honest opinion on "IS PROGRAMMING NOT FOR ME" or is this just a phase of being a nooby .
It feels like i only focus on only one thing and can't see the entire picture, like not planning beforehand and trying to make something up along the way .
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
  #include <iostream>


using namespace std;

int main() { 

int l;int a;
 
 int p=2;int i=0;int array[100];int k=0;
 cout<<"prime numbers : ";

 	
 	for(p;p*p<=100;) {     
 		cout<<p<<endl;
 		for(i=2;l<=100;i++) {    //storing in array the multiples of p
 			 l=p*i;
			 array[k]=p*i;
 			k++; 			
		 }
		 l=0;
		p++;
		for(int j=0;j<=k;j++){      //finding the new p by checking if its inside the array
			if(array[j]==p) {
				p++;
				j=0;
			}
			
		} 
 	 }
 	for(int z=0;z<=k;z++){
 		
    //part where YET ANOTHER LOOP COMES TO PRINT THE REST OF THE PRIME INTEGERS 
	//i suckkkkkkkkkkkkkkkkkkkkkkkkkkk aaaaaaaaaaaaaaaaahhhhhhh
	 }
	 
	 }
	 
	 
	 }
 	
Last edited on
Lets be optimistic and say "just a phase".

Do you dare to look at https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
It has pseudocode of the algorithm. Converting pseudocode into C++ code is obviously different challenge than deciphering logic from mere textual description of the sieve.
Also, take a deep breath and don't forget to hit the <Enter> and <Spacebar> keys. A consistently indented bit of code is a happy bit of code! Not knowing the algorithm and forging one is definitely hard, for sure, as you'll find when you try some of the stuff at ProjectEuler. Some will come from experience, when you naturally start finding ways to reduce nesting. Some ideas will come from just reading articles on algorithms, like the wiki article there. That article in particular even gives a visual representation of how it works.
what is the problem?

you want to find all the primes to a certain user input?
or do you want to find n many primes?
Hello Blazinteen,

Hang around here and read as much as you can. There is a lot of information to be had.

I reworked your program a bit. Look at the overall format to see what can help you when writing the code. A proper format and consistency make the code easier to follow and read:
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
#include <iostream>

using namespace std;

int main()
{

	int l; int a;

	int p = 2; int i = 0; int array[100]; int k = 0;

	cout << "prime numbers : ";

	for (p; p * p <= 100;)
	{
		cout << p << endl;
		for (i = 2; l <= 100; i++)
		{    //storing in array the multiples of p
			l = p * i;
			array[k] = p * i;
			k++;
		}

		l = 0;  // <--- Is this a letter or a number. It is best not to use a lower case "L".

		p++;

		for (int j = 0; j <= k; j++)
		{      //finding the new p by checking if its inside the array
			if (array[j] == p)
			{
				p++;  // <--- Careful this could increment p one or more times than you want.
				j = 0;
			}

		}
	}

	for (int z = 0; z <= k; z++)
	{

		//part where YET ANOTHER LOOP COMES TO PRINT THE REST OF THE PRIME INTEGERS 
		//i suckkkkkkkkkkkkkkkkkkkkkkkkkkk aaaaaaaaaaaaaaaaahhhhhhh
	}

}


	 }  // <--- When the {}s line up in the same column and indenting is proper you can easily see this is an extra. 


Sometimes it is better to concentrate and practice on the basics a bit longer rather than jump into something yo are not ready for.

There are many examples here that deal with prime numbers that might be worth searching for. The examples of code would be worth looking at.

As it has been said here many times take small steps and work on small parts until you get it working before you move on to the next. If you try to think and work on the whole program at one time you will be overwhelmed.

Hope that helps,

Andy
Keskiverto , icy1 and Handy Andy

Thank you for your kind words and spending a big chunk of your time on this. I swear I'll not give up programming atleast until I finish this problem. The decision to pursue programming or something else has to be made soon (for 11th and 12th grade). I hope I find some light before the tunnel ends. Again thank you for showing me an example of a well indented code.


EDIT : finally finished the code with the help of the pseudo-code, guess that's not something to be proud of though. So I think i get now what people mean by this "problem-solving" ..its not the code but how you use code to formulate the required logic out of it that's hard.
Last edited on
Hello Blazinteen,

At your age putting more time into math like algebra and calculus. It is not so much about the math, but a way of thinking that is helpful in programming.

Andy
@Andy

Will do sir.


(Not related stuff :
Also, I blame the current education system and my lack of enthusiasm for me being the way I am now. As long as you get good marks and please your parents your future's gonna be hella good was the mindset of the school I went to. This made me HATE MATH as I couldn't by-heart it like every other subjects. The concept of KNOWING HOW TO THINK OVER KNOWLEDGE NEVER EVEN CROSSED MY MIND until a friend of mine brought it up unknowingly through his actions. I still have trouble reminding myself to think when I come across a problem in life. Programming's the only thing i spend time on outside of school that is related to "PRODUCTIVITY" and i sure hope I could get the hang of it. )

Once again,
Thank you!
Maybe a certificate would help to build your confidence? http://cppinstitute.org/
Last edited on
Topic archived. No new replies allowed.