Why we are a bunch of tough lovers.

Pages: 123
Tee hee hee... I called us lovers! O_o

Hello females, males, transsexuals, natural hermaphrodites, and unsexed...

This is an article about our refusal to answer homework questions and solve homework problems.

I expect the majority of the people who have posted something along the lines of...
Hey pplz:
Write a program that implements Eratosthenes's sieve. The program should take in from the console the upper limit for the range 1-n to be searched for primes and output the number of prime numbers.


...were flat-out denied the problem's solution, and those who got the solution might have seen the solution giver reprimanded for giving the solution. If you are either a solution-giver or a solution-seeker, then I'm sorry but there's a difference between the easy way out and the right way out, and in this article I hope to explain why we are so set against giving out problem solutions.

Say that for the above problem, I posted the solution of:

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
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <math.h>

using namespace std;
int size;
int* primes;
bool checkprimeness(int i)
{
	for (int j = 0; j < size; j++) 
		if (i % primes[j] == 0) return false;
	return true;
}

void main()
{
    int max;
    printf("Enter your number: ");
    cin >> max;
	system("cls");
	
    if (max < 2) {
	printf("What the fortran do you take me for?\nThere are no positive primes here!\n");
		exit(0);
    }
	else {
		primes = (int*)malloc(4*max);
		size = 0;
		primes[size] = 2, size++;
	}
	
	int i;
    for (i = 2; i < max+1; i++) {
		if (checkprimeness(i))
			primes[size] = i, size++; 
	}
	
	cout << "\n";
    for (i = 0; i < size; i++) 
		cout << primes[i] << "\n";
	
    system("pause");
}


Off the top of my head, there are five problems with doing this.

1: The OP may not read through the code.
We have no guarantee that the OP will not do some really quick foot-kissing and run off with this solution, presenting it to the instructor as his/her own without examining it first. This is a problem because:

1.1: We will have wasted our time creating the solution (the above took me about 5 minutes, but it's still time wasted), as the OP will have learned nothing positive and unless it's good practice for you, you just spent some time writing a bit of code that won't teach anything positive. :)

1.2: Those of us who were paying close attention will notice that this code is not in fact the solution to the problem. Misreads happen, and if the student doesn't pay close enough attention, he/she won't notice that I "accidentally" made the program output the list of prime numbers instead of the demanded number of prime numbers.
Also, as tition correctly noticed 3 months later, the algorithm I used is not Eratosthenes's sieve; it never strikes out any numbers from a complete list. Though I deliberately implemented the wrong algorithm and did not state it, if this were in a solution I gave to the OP it might easily have gone unnoticed, and he/she might lose a substantial amount of credit on the problem if this piece of code was given in as a solution.

1.3: In the event that someone tried to turn this example in for credit, they might lose stunning amounts of credit just because of the ugly style I wrote it in. More on this in point 5.

2: This enforces laziness during college.
The OP might in some cases learn that others will do his/her work for him/her, possibly leading to some... unfortunate changes, as well as reinforcing laziness. This is only a possibility, like the above, but it still exists; I've seen it happen.

3: No comment(s).
If the OP actually did try to learn from my example, it could be hard to follow due to the lack of explanatory comments. Some of us will put in the comments, but I recognize that others may not, so when the given solution doesn't have explanatory comments it's time wasted for the OP, and possibly simply doing the problem would have taken less time to do and would have lead to a better understanding.

4: This enforces laziness after college.
(inspired by a response by cnoeval) If somehow the OP managed to get through college cheating and obtain a reasonable degree to work in the computer science industry, then he/she would have both wasted at least two good years of his/her life, and also have great trouble being able to actually work. Some people might help them in that area as well, possibly for years, but everyone has a limit and eventually that limit would be reached if the pattern of laziness were to continue for all those years.

5: It spreads bad coding habits like a disease.
The style of this piece of code is actually quite horrific, but what's worse is that I could see someone without much experience in C++ giving this to someone as a solution to turn in. I'd congratulate the effort, but in the event that the OP does try to learn from the code, he/she might develop habits that in C++ are not only quite bad, but also very hard to unlearn. Usually we try to check these habits, but we aren't everywhere and we aren't active all the time. Please don't give out solutions, just in case!

NOTE: The example won't even compile if one closely follows the C++ standard. In addition, the program has a memory leak, as a result of the memory primes points to never being freed. I do NOT recommend running this program without fixing the memory leak first!

If you learn better by seeing a problem and its solution side by side, ask a forum member if they can create a similar problem and solve it so that you can see the problem and the solution side by side. If one of us has the time, we will happily do that or link to an already done example. :)

For a few guidelines in creating a good question, see this article here:
http://www.cplusplus.com/forum/articles/1295/

Happy coding!

-Albatross

EDIT1: Made a slight change to the problem description to make it somewhat more realistic and added point #4. Also made the console pause slightly more elegant but slightly harder to understand.
EDIT2: Added point 1.2.1 (now a part of 1.2)
EDIT3: Made a few minor changes.
EDIT4: Doused the example with a bucket of unadulterated evil, and added point 5.
EDIT5: A few anti-refinements to the example were added.
Last edited on
These are pretty much my views on the homework situation.

Albatross++
closed account (S6k9GNh0)
Bravo!
This is good.
1.1: We will have wasted our time creating the solution (the above took me about 5 minutes, but it's still time wasted), as the OP will have learned nothing positive.

The OP may not have learned anything but you certainly have! It can be a great way to stay sharp answering questions.

Regardless, the questions that really get me are the ones that are plastered all over the first page of a Google search... Many of the posters should google "How to Google" and learn how to find their answers.
Last edited on
Hah, stupid Albatross. The most obvious solution is of course:
1
2
3
4
#include <cstdio>
bool _(unsigned __, unsigned ___=2) {return ___<__?__%___==___-___?___-___:_(___,__+___/___):__/__+___-___;}
unsigned i(unsigned ii, unsigned iii=2) {return iii<ii?_(iii)+i(ii,iii+ii/ii):_(ii);}
main(){unsigned m(NULL);printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",69,110,116,101,114,32,121,111,117,114,32,110,117,109,98,101,114,58);scanf("%u",&m);printf("%u",i(m));return 0;}
What the fortran is that demonic piece of code? Out! OUT! OUT!!!

No, but seriously. If I wanted to write a better piece of code than that, I would've. ;)

-Albatross
4. The student that you help to cheat today... May become the co-worker you have to coddle for years.
More obvious codes:
1
2
3
#include <cstdio>
float _(float*__,unsigned ___,unsigned ____=NULL){return ____<___?__[____]+_(__,___,____+___):__[___];}
main(){float* l = new float[010>>1>>1];scanf("%f",l),printf("%c",0x2B),scanf("%f",l+(010>>(01<<01)>>01)),printf("%c",0x3D),printf("%f",_(l,(010>>(01<<01)>>01)));delete [] l;}

1
2
#include <cstdio>
unsigned _(unsigned ___,unsigned __=0.f){__<___?printf("%d\n",__),_(___,__+___/___):__-__;}main(){_(011);for(;,;);}


Shouldn't be too hard to figure out that they do.
Shouldn't be too hard to figure out that they do.

The simplest way is to compile and run....
Which is my whole point. ;)

Off-topic: Getting my iMac next week. :D
I took my sweet time replying to this thread! O_o

Kyon: I see your point, but the fact is that even after compilation, you would need to look at least somewhat carefully at the problem description to see the difference in what the code does and what it should do per assignment, and some people rush. :)

-Albatross
What if someone posted a code like Kyon's with malicious content, I mean something like a virus or a prank. I just wonder what the reaction of that student would be after compiling and running it.
That's another problem, but I kinda hope that even the complete beginners will have some amount of common sense.

-Albatross
@ moorecm
I was bored and tried that only to find this http://www.googleguide.com/ it is, and I qoute a
"online interactive tutorial and reference for experienced users, novices, and everyone in between"
While compiling to give to my instructor without reading the code (Joke intended), I have come to the conclusion that my compler is retarded... It says
 `cout' is not a member of `std' 
and also a similar error message for all the cin lines.
That happened to me once when I accidentally put #incluse<iostream>
Actually, that was today.
Umm, actually your code is *not* eratosthenes' sieve; in fact, eratosthenes sieve method works *much* faster than your code, *however* consumes *much* more RAM.

To make erathosthenes sieve, you make a boolean vector. A 0 entry in the vector indicates that the number with the corresponding index in the vector is prime, a 1 entry - that it is composite.

You pick the first number P marked as prime that is not 1.

Then you mark in a cycle all numbers divisible by P (no division here: just a for cycle is good).

Then you pick the next number larger than P that is not marked as prime and repeat, and so on.

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Last edited on
Look again at point 1.2, tition. This code is not necessarily the solution to the problem and I deliberately meant it not to be, although I admit I didn't state that I took some liberties with the primes algorithm. ;)

I guess the cat's out of the bag, though. Time to mention it in the article.

-Albatross
Last edited on
Pages: 123